|
| 1 | +import * as webllm from "@mlc-ai/web-llm"; |
| 2 | + |
| 3 | +const SYSTEM_PROMPT_PREFIX = |
| 4 | + "You are a helpful assistant running in the user's browser, responsible for answering questions."; |
| 5 | + |
| 6 | +function setLabel(id: string, text: string) { |
| 7 | + const label = document.getElementById(id); |
| 8 | + if (label == null) { |
| 9 | + throw Error("Cannot find label " + id); |
| 10 | + } |
| 11 | + label.innerText = text; |
| 12 | +} |
| 13 | + |
| 14 | +async function testPrefix() { |
| 15 | + const initProgressCallback = (report: webllm.InitProgressReport) => { |
| 16 | + setLabel("init-label", report.text); |
| 17 | + }; |
| 18 | + |
| 19 | + const selectedModel = "Llama-3.1-8B-Instruct-q4f32_1-MLC"; |
| 20 | + const engine: webllm.MLCEngineInterface = await webllm.CreateMLCEngine( |
| 21 | + selectedModel, |
| 22 | + { |
| 23 | + initProgressCallback: initProgressCallback, |
| 24 | + logLevel: "INFO", |
| 25 | + // Prefilling KV cache for efficiency |
| 26 | + cachedPrefixes: [[{ role: "system", content: SYSTEM_PROMPT_PREFIX }]], |
| 27 | + }, |
| 28 | + { |
| 29 | + context_window_size: 2048, |
| 30 | + }, |
| 31 | + ); |
| 32 | + |
| 33 | + const reply_using_prefix = await engine.chat.completions.create({ |
| 34 | + messages: [ |
| 35 | + { role: "system", content: SYSTEM_PROMPT_PREFIX }, |
| 36 | + { role: "user", content: "List three US states." }, |
| 37 | + ], |
| 38 | + // below configurations are all optional |
| 39 | + n: 1, |
| 40 | + temperature: 1.5, |
| 41 | + max_tokens: 64, |
| 42 | + logprobs: true, |
| 43 | + top_logprobs: 2, |
| 44 | + }); |
| 45 | + console.log(reply_using_prefix); |
| 46 | + console.log(reply_using_prefix.usage); |
| 47 | +} |
| 48 | + |
| 49 | +async function testWithoutPrefix() { |
| 50 | + const initProgressCallback = (report: webllm.InitProgressReport) => { |
| 51 | + setLabel("init-label", report.text); |
| 52 | + }; |
| 53 | + |
| 54 | + const selectedModel = "Llama-3.1-8B-Instruct-q4f32_1-MLC"; |
| 55 | + // Engine Initialization without cachedPrefixes |
| 56 | + const engine: webllm.MLCEngineInterface = await webllm.CreateMLCEngine( |
| 57 | + selectedModel, |
| 58 | + { |
| 59 | + initProgressCallback: initProgressCallback, |
| 60 | + logLevel: "INFO", |
| 61 | + }, |
| 62 | + { |
| 63 | + context_window_size: 2048, |
| 64 | + }, |
| 65 | + ); |
| 66 | + |
| 67 | + const reply_without_prefix = await engine.chat.completions.create({ |
| 68 | + messages: [ |
| 69 | + { role: "system", content: SYSTEM_PROMPT_PREFIX }, |
| 70 | + { role: "user", content: "List three US states." }, |
| 71 | + ], |
| 72 | + // below configurations are all optional |
| 73 | + n: 1, |
| 74 | + temperature: 1.5, |
| 75 | + max_tokens: 64, |
| 76 | + logprobs: true, |
| 77 | + top_logprobs: 2, |
| 78 | + }); |
| 79 | + console.log(reply_without_prefix); |
| 80 | + console.log(reply_without_prefix.usage); |
| 81 | +} |
| 82 | + |
| 83 | +async function testMultiRound() { |
| 84 | + const initProgressCallback = (report: webllm.InitProgressReport) => { |
| 85 | + setLabel("init-label", report.text); |
| 86 | + }; |
| 87 | + |
| 88 | + const selectedModel = "Llama-3.1-8B-Instruct-q4f32_1-MLC"; |
| 89 | + const engine: webllm.MLCEngineInterface = await webllm.CreateMLCEngine( |
| 90 | + selectedModel, |
| 91 | + { |
| 92 | + initProgressCallback: initProgressCallback, |
| 93 | + logLevel: "INFO", |
| 94 | + cachedPrefixes: [[{ role: "system", content: SYSTEM_PROMPT_PREFIX }]], // Prefilling KV cache for efficiency |
| 95 | + }, |
| 96 | + { |
| 97 | + context_window_size: 2048, |
| 98 | + }, |
| 99 | + ); |
| 100 | + |
| 101 | + // First Completion with cachedPrefixes |
| 102 | + const reply0 = await engine.chat.completions.create({ |
| 103 | + messages: [ |
| 104 | + { role: "system", content: SYSTEM_PROMPT_PREFIX }, |
| 105 | + { role: "user", content: "List three US states." }, |
| 106 | + ], |
| 107 | + // below configurations are all optional |
| 108 | + n: 1, |
| 109 | + temperature: 1.5, |
| 110 | + max_tokens: 64, |
| 111 | + logprobs: true, |
| 112 | + top_logprobs: 2, |
| 113 | + }); |
| 114 | + console.log(reply0); |
| 115 | + console.log(reply0.usage); |
| 116 | + |
| 117 | + // Second Completion with cachedPrefixes |
| 118 | + const reply1 = await engine.chat.completions.create({ |
| 119 | + messages: [ |
| 120 | + { role: "system", content: SYSTEM_PROMPT_PREFIX }, |
| 121 | + { role: "user", content: "Where is the US capital?" }, |
| 122 | + ], |
| 123 | + // below configurations are all optional |
| 124 | + n: 1, |
| 125 | + temperature: 1.5, |
| 126 | + max_tokens: 64, |
| 127 | + logprobs: true, |
| 128 | + top_logprobs: 2, |
| 129 | + }); |
| 130 | + console.log(reply1); |
| 131 | + console.log(reply1.usage); |
| 132 | +} |
| 133 | + |
| 134 | +async function main() { |
| 135 | + await testPrefix(); |
| 136 | + |
| 137 | + await testWithoutPrefix(); |
| 138 | + |
| 139 | + await testMultiRound(); |
| 140 | +} |
| 141 | + |
| 142 | +main(); |
0 commit comments