chore: import upstream snapshot with attribution
Build / build (push) Has been cancelled
Tests / test (push) Has been cancelled
Build site and push to gh-pages / Build site (push) Has been cancelled
Linter / lint (push) Has been cancelled
Security / dependency-review (push) Has been cancelled
Security / npm-audit (push) Has been cancelled
Security / codeql (push) Has been cancelled
Build / build (push) Has been cancelled
Tests / test (push) Has been cancelled
Build site and push to gh-pages / Build site (push) Has been cancelled
Linter / lint (push) Has been cancelled
Security / dependency-review (push) Has been cancelled
Security / npm-audit (push) Has been cancelled
Security / codeql (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<script>
|
||||
webLLMGlobal = {};
|
||||
</script>
|
||||
|
||||
<body>
|
||||
<h2>WebLLM Test Page</h2>
|
||||
Open console to see output
|
||||
<br />
|
||||
<br />
|
||||
<label id="init-label"> </label>
|
||||
<h3>Response</h3>
|
||||
<label id="generate-label"> </label>
|
||||
<script type="module" src="./streaming.ts"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,53 @@
|
||||
import * as webllm from "@mlc-ai/web-llm";
|
||||
|
||||
function setLabel(id: string, text: string) {
|
||||
const label = document.getElementById(id);
|
||||
if (label == null) {
|
||||
throw Error("Cannot find label " + id);
|
||||
}
|
||||
label.innerText = text;
|
||||
}
|
||||
|
||||
/**
|
||||
* We demonstrate chat completion with streaming, where delta is sent while generating response.
|
||||
*/
|
||||
async function main() {
|
||||
const initProgressCallback = (report: webllm.InitProgressReport) => {
|
||||
setLabel("init-label", report.text);
|
||||
};
|
||||
const selectedModel = "Llama-3.1-8B-Instruct-q4f32_1-MLC";
|
||||
const engine: webllm.MLCEngineInterface = await webllm.CreateMLCEngine(
|
||||
selectedModel,
|
||||
{ initProgressCallback: initProgressCallback },
|
||||
);
|
||||
|
||||
const request: webllm.ChatCompletionRequest = {
|
||||
stream: true,
|
||||
stream_options: { include_usage: true },
|
||||
messages: [
|
||||
{
|
||||
role: "system",
|
||||
content:
|
||||
"You are a pirate chatbot who always responds in pirate speak!",
|
||||
},
|
||||
{ role: "user", content: "Who are you?" },
|
||||
],
|
||||
logprobs: true,
|
||||
top_logprobs: 2,
|
||||
};
|
||||
|
||||
const asyncChunkGenerator = await engine.chat.completions.create(request);
|
||||
let message = "";
|
||||
for await (const chunk of asyncChunkGenerator) {
|
||||
console.log(chunk);
|
||||
message += chunk.choices[0]?.delta?.content || "";
|
||||
setLabel("generate-label", message);
|
||||
if (chunk.usage) {
|
||||
console.log(chunk.usage); // only last chunk has usage
|
||||
}
|
||||
// engine.interruptGenerate(); // works with interrupt as well
|
||||
}
|
||||
console.log("Final message:\n", await engine.getMessage()); // the concatenated message
|
||||
}
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user