-
Notifications
You must be signed in to change notification settings - Fork 955
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
216 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# WebLLM App for Prefix Caching Demo | ||
|
||
This example demonstrates the use of `cachedPrefixes` in WebLLM. | ||
To try it out, you can do the following steps under this folder | ||
|
||
```bash | ||
npm install | ||
npm start | ||
``` | ||
|
||
Note if you would like to hack WebLLM core package. | ||
You can change web-llm dependencies as `"file:../.."`, and follow the build from source | ||
instruction in the project to build webllm locally. This option is only recommended | ||
if you would like to hack WebLLM core package. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "prefix-caching-example", | ||
"version": "0.1.0", | ||
"private": true, | ||
"scripts": { | ||
"start": "parcel src/prefix-caching.html --port 8888", | ||
"build": "parcel build src/prefix-caching.html --dist-dir lib" | ||
}, | ||
"devDependencies": { | ||
"buffer": "^5.7.1", | ||
"parcel": "^2.8.3", | ||
"process": "^0.11.10", | ||
"tslib": "^2.3.1", | ||
"typescript": "^4.9.5", | ||
"url": "^0.11.3" | ||
}, | ||
"dependencies": { | ||
"@mlc-ai/web-llm": "../.." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!doctype html> | ||
<html> | ||
<script> | ||
webLLMGlobal = {}; | ||
</script> | ||
<body> | ||
<h2>WebLLM Prefix Caching Test Page</h2> | ||
Open console to see output | ||
<br /> | ||
<br /> | ||
<label id="init-label"> </label> | ||
|
||
<h3>Prompt</h3> | ||
<label id="prompt-label"> </label> | ||
|
||
<h3>Response</h3> | ||
<label id="generate-label"> </label> | ||
<br /> | ||
<label id="stats-label"> </label> | ||
|
||
<script type="module" src="./prefix-caching.ts"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import * as webllm from "@mlc-ai/web-llm"; | ||
|
||
const SYSTEM_PROMPT_PREFIX = | ||
"You are a helpful assistant running in the user's browser, responsible for answering questions."; | ||
|
||
function setLabel(id: string, text: string) { | ||
const label = document.getElementById(id); | ||
if (label == null) { | ||
throw Error("Cannot find label " + id); | ||
} | ||
label.innerText = text; | ||
} | ||
|
||
async function testPrefix() { | ||
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, | ||
logLevel: "INFO", | ||
// Prefilling KV cache for efficiency | ||
cachedPrefixes: [[{ role: "system", content: SYSTEM_PROMPT_PREFIX }]], | ||
}, | ||
{ | ||
context_window_size: 2048, | ||
}, | ||
); | ||
|
||
const reply_using_prefix = await engine.chat.completions.create({ | ||
messages: [ | ||
{ role: "system", content: SYSTEM_PROMPT_PREFIX }, | ||
{ role: "user", content: "List three US states." }, | ||
], | ||
// below configurations are all optional | ||
n: 1, | ||
temperature: 1.5, | ||
max_tokens: 64, | ||
logprobs: true, | ||
top_logprobs: 2, | ||
}); | ||
console.log(reply_using_prefix); | ||
console.log(reply_using_prefix.usage); | ||
} | ||
|
||
async function testWithoutPrefix() { | ||
const initProgressCallback = (report: webllm.InitProgressReport) => { | ||
setLabel("init-label", report.text); | ||
}; | ||
|
||
const selectedModel = "Llama-3.1-8B-Instruct-q4f32_1-MLC"; | ||
// Engine Initialization without cachedPrefixes | ||
const engine: webllm.MLCEngineInterface = await webllm.CreateMLCEngine( | ||
selectedModel, | ||
{ | ||
initProgressCallback: initProgressCallback, | ||
logLevel: "INFO", | ||
}, | ||
{ | ||
context_window_size: 2048, | ||
}, | ||
); | ||
|
||
const reply_without_prefix = await engine.chat.completions.create({ | ||
messages: [ | ||
{ role: "system", content: SYSTEM_PROMPT_PREFIX }, | ||
{ role: "user", content: "List three US states." }, | ||
], | ||
// below configurations are all optional | ||
n: 1, | ||
temperature: 1.5, | ||
max_tokens: 64, | ||
logprobs: true, | ||
top_logprobs: 2, | ||
}); | ||
console.log(reply_without_prefix); | ||
console.log(reply_without_prefix.usage); | ||
} | ||
|
||
async function testMultiRound() { | ||
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, | ||
logLevel: "INFO", | ||
cachedPrefixes: [[{ role: "system", content: SYSTEM_PROMPT_PREFIX }]], // Prefilling KV cache for efficiency | ||
}, | ||
{ | ||
context_window_size: 2048, | ||
}, | ||
); | ||
|
||
// First Completion with cachedPrefixes | ||
const reply0 = await engine.chat.completions.create({ | ||
messages: [ | ||
{ role: "system", content: SYSTEM_PROMPT_PREFIX }, | ||
{ role: "user", content: "List three US states." }, | ||
], | ||
// below configurations are all optional | ||
n: 1, | ||
temperature: 1.5, | ||
max_tokens: 64, | ||
logprobs: true, | ||
top_logprobs: 2, | ||
}); | ||
console.log(reply0); | ||
console.log(reply0.usage); | ||
|
||
// Second Completion with cachedPrefixes | ||
const reply1 = await engine.chat.completions.create({ | ||
messages: [ | ||
{ role: "system", content: SYSTEM_PROMPT_PREFIX }, | ||
{ role: "user", content: "Where is the US capital?" }, | ||
], | ||
// below configurations are all optional | ||
n: 1, | ||
temperature: 1.5, | ||
max_tokens: 64, | ||
logprobs: true, | ||
top_logprobs: 2, | ||
}); | ||
console.log(reply1); | ||
console.log(reply1.usage); | ||
} | ||
|
||
async function main() { | ||
await testPrefix(); | ||
|
||
await testWithoutPrefix(); | ||
|
||
await testMultiRound(); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters