Re-use WebLLM worker

This commit is contained in:
Dustin Brett 2023-05-07 11:52:54 -07:00
parent 6ead64d8a0
commit b80dfc54a0
2 changed files with 17 additions and 11 deletions

View File

@ -390,18 +390,16 @@ const Chat: FC<ComponentProcessProps> = ({ id }) => {
}, [messages]);
useEffect(() => {
let cleanUp: (() => void) | undefined;
if (initRef.current) {
cleanUp = () => AI?.destroy?.();
} else if (AI) {
if (AI && !initRef.current) {
initRef.current = true;
AI.init().then(() => addMessage(AI.greeting));
inputRef.current?.focus(PREVENT_SCROLL);
}
return cleanUp;
return () => {
if (initRef.current) AI?.destroy?.();
};
}, [AI, addMessage, apiKey]);
useEffect(() => {

View File

@ -4,6 +4,12 @@ import type { Engine } from "hooks/useInference/useInference";
type Log = { message: string; type: string };
type WorkerMessage = { data: Log | string };
declare global {
interface Window {
webLLM?: Worker;
}
}
const DEFAULT_GREETING = {
text: "Hello, I am an AI assistant. How can I help you today?",
type: "assistant",
@ -18,14 +24,16 @@ export class WebLLM implements Engine {
public destroy(): void {
this.reset();
setTimeout(() => this.worker?.terminate(), 500);
}
public async init(): Promise<void> {
this.worker = new Worker(
new URL("hooks/useInference/WebLLM.worker.ts", import.meta.url),
{ name: "WebLLM" }
);
window.webLLM =
window.webLLM ||
new Worker(
new URL("hooks/useInference/WebLLM.worker.ts", import.meta.url),
{ name: "WebLLM" }
);
this.worker = window.webLLM;
this.worker.postMessage({ type: "init" });
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject