Skip to content

Commit

Permalink
Support multiple delete interface for WebLLM
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoCao committed Feb 26, 2024
1 parent 3b0cb09 commit 7d21d81
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 11 deletions.
82 changes: 72 additions & 10 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 @@ -660,18 +661,79 @@ export class ChatRestModule implements ChatInterface {
}
}

export async 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 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 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 NDArray
await deleteNDArrayInCache(localId, appConfig);
// delete tokenizer in cache
await deleteTokenizerInCache(localId, appConfig);
// delete wasm in cache
await deleteModelWasmInCache(localId, appConfig);
// delete chat config
await deleteChatConfigInCache(localId, appConfig);
}

export async function deleteNDArrayInCache(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");
}

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);
}

export async function deleteTokenizerInCache(localID: string, appConfig?: AppConfig){
// delete the model tokenizer in Cache
if (appConfig === undefined) {
appConfig = prebuiltAppConfig;
}
const modelRecord = await findModelRecord(localID, appConfig);
const modelUrl = modelRecord.model_url;
const modelCache = new tvmjs.ArtifactCache("webllm/model");
await modelCache.deleteInCache(new URL("tokenizer.model", modelUrl).href);
await modelCache.deleteInCache(new URL("tokenizer.json", modelUrl).href);
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export {

export {
ChatModule,
ChatRestModule, hasModelInCache
ChatRestModule, hasModelInCache, deleteChatConfigInCache, deleteModelAllInfoInCache, deleteModelWasmInCache, deleteNDArrayInCache, deleteTokenizerInCache
} from "./chat_module";

export {
Expand Down

0 comments on commit 7d21d81

Please sign in to comment.