-
Notifications
You must be signed in to change notification settings - Fork 873
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OpenAI] Support text completion via engine.completions.create() (#534)
`completions.create()`, as opposed to `chat.completions.create()`, is something we have not supported prior to this PR. Compared to `chat.completions`, `completions` is pure text completion with no conversation. That is, given the user's input prompt, the model autoregressively generates, ignoring any chat template. For more, see `examples/text-completion` and https://platform.openai.com/docs/api-reference/completions/object
- Loading branch information
1 parent
5472977
commit 41e786b
Showing
20 changed files
with
1,388 additions
and
358 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
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 Get Started App | ||
|
||
This folder provides a minimum demo to show WebLLM API in a webapp setting. | ||
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": "text-completion", | ||
"version": "0.1.0", | ||
"private": true, | ||
"scripts": { | ||
"start": "parcel src/text_completion.html --port 8888", | ||
"build": "parcel build src/text_completion.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 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="./text_completion.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,58 @@ | ||
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; | ||
} | ||
|
||
async function main() { | ||
const initProgressCallback = (report: webllm.InitProgressReport) => { | ||
setLabel("init-label", report.text); | ||
}; | ||
|
||
// Unlike "Llama-3.1-8B-Instruct-q4f32_1-MLC", this is a base model | ||
const selectedModel = "Llama-3.1-8B-q4f32_1-MLC"; | ||
|
||
const appConfig: webllm.AppConfig = { | ||
model_list: [ | ||
{ | ||
model: "https://huggingface.co/mlc-ai/Llama-3.1-8B-q4f32_1-MLC", // a base model | ||
model_id: selectedModel, | ||
model_lib: | ||
webllm.modelLibURLPrefix + | ||
webllm.modelVersion + | ||
"/Llama-3_1-8B-Instruct-q4f32_1-ctx4k_cs1k-webgpu.wasm", | ||
overrides: { | ||
context_window_size: 2048, | ||
}, | ||
}, | ||
], | ||
}; | ||
const engine: webllm.MLCEngineInterface = await webllm.CreateMLCEngine( | ||
selectedModel, | ||
{ | ||
appConfig: appConfig, | ||
initProgressCallback: initProgressCallback, | ||
logLevel: "INFO", | ||
}, | ||
); | ||
|
||
const reply0 = await engine.completions.create({ | ||
prompt: "List 3 US states: ", | ||
// below configurations are all optional | ||
echo: true, | ||
n: 2, | ||
max_tokens: 64, | ||
logprobs: true, | ||
top_logprobs: 2, | ||
}); | ||
console.log(reply0); | ||
console.log(reply0.usage); | ||
|
||
// To change model, either create a new engine via `CreateMLCEngine()`, or call `engine.reload(modelId)` | ||
} | ||
|
||
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
Oops, something went wrong.