Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Delete All Data Interface #314

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions src/cache_util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import * as tvmjs from "tvmjs";
import {
AppConfig,
prebuiltAppConfig,
} from "./config";

function findModelRecord (localId: string, appConfig?: AppConfig){
const matchedItem = appConfig?.model_list.find(
item => item.local_id == localId
);
if (matchedItem !== undefined) {
return matchedItem;
}
throw Error("Cannot find model_url for " + localId);
}

export async function hasModelInCache(localId: string, appConfig?: AppConfig): Promise<boolean> {
if (appConfig === undefined) {
appConfig = prebuiltAppConfig;
}
const modelRecord = await findModelRecord(localId, appConfig);
const modelUrl = modelRecord.model_url;
return tvmjs.hasNDArrayInCache(modelUrl, "webllm/model");
}

export async function deleteModelAllInfoInCache(localId: string, appConfig?: AppConfig) {
// function to delete model all information in cache
if (appConfig === undefined) {
appConfig = prebuiltAppConfig;
}
// delete model and tokenizer in Cache
await deleteModelInCache(localId, appConfig);
// delete wasm in cache
await deleteModelWasmInCache(localId, appConfig);
// delete chat config
await deleteChatConfigInCache(localId, appConfig);
}


export async function deleteModelInCache(localId: string, appConfig?: AppConfig){
// delete the model NDArray In Cache
if (appConfig === undefined) {
appConfig = prebuiltAppConfig;
}
const modelRecord = await findModelRecord(localId, appConfig);
tvmjs.deleteNDArrayCache(modelRecord.model_url, "webllm/model");
const modelCache = new tvmjs.ArtifactCache("webllm/model");
await modelCache.deleteInCache(new URL("tokenizer.model", modelRecord.model_url).href);
await modelCache.deleteInCache(new URL("tokenizer.json", modelRecord.model_url).href);
}

export async function deleteChatConfigInCache(localId: string, appConfig?: AppConfig){
// delete the chat configuration in Cache
if (appConfig === undefined) {
appConfig = prebuiltAppConfig;
}
const modelRecord = await findModelRecord(localId, appConfig);
const configCache = new tvmjs.ArtifactCache("webllm/config");
const configUrl = new URL("mlc-chat-config.json", modelRecord.model_url).href;
await configCache.deleteInCache(configUrl);
}


export async function deleteModelWasmInCache(localId: string, appConfig?: AppConfig){
// delete the wasm in Cache
if (appConfig === undefined) {
appConfig = prebuiltAppConfig;
}
const modelRecord = await findModelRecord(localId, appConfig);
const wasmCache = new tvmjs.ArtifactCache("webllm/wasm");
await wasmCache.deleteInCache(modelRecord.model_lib_url);
}
21 changes: 3 additions & 18 deletions src/chat_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
AppConfig,
prebuiltAppConfig,
GenerationConfig,
postInitAndCheckGenerationConfigValues
postInitAndCheckGenerationConfigValues,
ModelRecord
} from "./config";
import { LLMChatPipeline } from "./llm_chat"
import {
Expand Down Expand Up @@ -460,7 +461,7 @@ export class ChatModule implements ChatInterface {
message.content,
]);
} else {
throw new Error("Unsuppoerted role: " + message.role);
throw new Error("Unsupported role: " + message.role);
}
}
this.getPipeline().overrideConversationMessages(messages);
Expand Down Expand Up @@ -659,19 +660,3 @@ export class ChatRestModule implements ChatInterface {
});
}
}

export async function hasModelInCache(localId: string, appConfig?: AppConfig): Promise<boolean> {
if (appConfig === undefined) {
appConfig = prebuiltAppConfig;
}
const findModelRecord = () => {
const matchedItem = appConfig?.model_list.find(
item => item.local_id == localId
);
if (matchedItem !== undefined) return matchedItem;
throw Error("Cannot find model_url for " + localId);
}
const modelRecord = findModelRecord();
const modelUrl = modelRecord.model_url;
return tvmjs.hasNDArrayInCache(modelUrl, "webllm/model");
}
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ export {

export {
ChatModule,
ChatRestModule, hasModelInCache
ChatRestModule,
} from "./chat_module";

export {
hasModelInCache, deleteChatConfigInCache, deleteModelAllInfoInCache, deleteModelWasmInCache, deleteModelInCache,
} from "./cache_util";

export {
ChatWorkerHandler,
ChatWorkerClient,
Expand Down