llama-cpp-wasm
llama.cpp的WebAssembly(Wasm)构建和绑定,由Tangled Group, Inc支持。
在线演示
https://tangledgroup.github.io/llama-cpp-wasm/
构建
git clone https://github.com/tangledgroup/llama-cpp-wasm.git
cd llama-cpp-wasm
./build-single-thread.sh
./build-multi-thread.sh
构建完成后,你可以在dist/llama-st
和dist/llama-mt
目录中找到构建好的llama.cpp
。
部署
基本上,你可以在构建后将dist/llama-st
或dist/llama-mt
目录复制粘贴到你的项目中,然后作为普通的JavaScript库/模块使用。
index.html
<!DOCTYPE html>
<html lang="en">
<body>
<label for="prompt">提示:</label>
<br/>
<textarea id="prompt" name="prompt" rows="25" cols="80">假设Alice最初有3个苹果,然后Bob给了Alice 7个苹果,之后Alice给了厨师5个苹果,接着Tim给了Alice她现有苹果数量的3倍。Alice现在有多少个苹果?让我们一步步思考。</textarea>
<br/>
<label for="result">结果:</label>
<br/>
<textarea id="result" name="result" rows="25" cols="80"></textarea>
<br/>
<script type="module" src="example.js"></script>
</body>
</html>
example.js
// import { LlamaCpp } from "./llama-st/llama.js";
import { LlamaCpp } from "./llama-mt/llama.js";
const onModelLoaded = () => {
console.debug('模型:已加载');
const prompt = document.querySelector("#prompt").value;
document.querySelector("#result").value = prompt;
app.run({
prompt: prompt,
ctx_size: 4096,
temp: 0.1,
no_display_prompt: true,
});
}
const onMessageChunk = (text) => {
console.log(text);
document.querySelector('#result').value += text;
};
const onComplete = () => {
console.debug('模型:已完成');
};
const models = [
'https://huggingface.co/Qwen/Qwen1.5-0.5B-Chat-GGUF/resolve/main/qwen2-beta-0_5b-chat-q8_0.gguf',
'https://huggingface.co/Qwen/Qwen1.5-1.8B-Chat-GGUF/resolve/main/qwen1_5-1_8b-chat-q8_0.gguf',
'https://huggingface.co/stabilityai/stablelm-2-zephyr-1_6b/resolve/main/stablelm-2-zephyr-1_6b-Q4_1.gguf',
'https://huggingface.co/TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF/resolve/main/tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf',
'https://huggingface.co/TheBloke/phi-2-GGUF/resolve/main/phi-2.Q4_K_M.gguf'
];
const model = models[2]; // stablelm-2-zephyr-1_6b
const app = new LlamaCpp(
model,
onModelLoaded,
onMessageChunk,
onComplete,
);
运行示例
首先生成自签名证书。
openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem
运行单线程示例
npx http-server -S -C cert.pem
运行多线程示例
将docs/server.js
复制到你的工作目录。
npm i express
node server.js
然后在浏览器中打开:https://127.0.0.1:8080/