Slide 9
Slide 9 text
import {ChatCompletionMessageParam, CreateMLCEngine, MLCEngine}
from '@mlc-ai/web-llm';
const model = 'Llama-3.2-3B-Instruct-q4f32_1-MLC';
this.engine = await CreateMLCEngine(model, {
initProgressCallback: ({progress}) =>
this.progress.set(progress)
});
this.ready.set(true);
await this.engine!.resetChat();
const systemPrompt = `
The user will ask questions about their todo list.
Here's the user's todo list:
${this.todos().map(todo => `* ${todo.text} (${todo.done ? 'done' : 'not
done'})`).join('\n')}`;
const messages: ChatCompletionMessageParam[] = [
{role: "system", content: systemPrompt},
{role: "user", content: userPrompt}
];
const chunks = await this.engine!.chat.completions.create({messages, stream:
true});
let reply = '';
for await (const chunk of chunks) {
reply += chunk.choices[0]?.delta.content ?? '';
this.reply.set(reply);