模型文件需自行准备并以 gguf 路径传给 loadModel,包内不含模型;平台无预编译二进制时会自动下载 llama.cpp 并用 cmake 从源码构建,可设 NODE_LLAMA_CPP_SKIP_DOWNLOAD=true 关闭该行为。
node-llama-cpp
withcataiMIT活跃维护在 Node.js 里直接跑 llama.cpp 的绑定库,支持 GPU 加速和 GGUF 模型,还能在生成阶段强制模型按 JSON Schema 输出,方便做结构化抽取和函数调用。
Run AI models locally on your machine with node.js bindings for llama.cpp. Enforce a JSON schema on the model output on the generation level
npm install node-llama-cpp
npx -y node-llama-cpp chatREADME 未说明具体显存要求;支持 Metal、CUDA 和 Vulkan,会自动适配硬件,无需手动配置。
在 Node.js 里本地跑 llama.cpp 模型,还能强制模型按 JSON Schema 输出
在 Node.js 里直接跑 llama.cpp 的绑定库,支持 GPU 加速和 GGUF 模型,还能在生成阶段强制模型按 JSON Schema 输出,方便做结构化抽取和函数调用。
核心能力
- 用 Node.js 绑定调用 llama.cpp 在本地运行 LLM
- 支持 Metal、CUDA 和 Vulkan 三种 GPU 后端
- 提供预编译二进制,缺失时回退用 cmake 从源码构建
- 在生成层面强制模型输出 JSON 或遵循指定 JSON Schema
- 支持函数调用、嵌入与重排序、结构化决策
容易踩的坑
- v3.22.3最新正式版2026-11-05 · 2026-11-05 15:44 UTC
3.22.3 (2026-11-05)
Bug Fixes
LlamaJsonSchemaGrammar.parse: count Unicode code points forminLengthandmaxLengthchecks (#675) (8030e0d)- correct riscv64 package metadata (#677) (8354dff)
Shipped with
llama.cppreleasev0.9.0To use the latest
llama.cpprelease available, runnpx -n node-llama-cpp source download --release latest. (learn more) - v3.22.22026-10-17 · 2026-10-17 02:06 UTC
查看更新说明
✨ New: Jev-like structured decisions with any model
3.22.2 (2026-10-17)
Bug Fixes
Shipped with
llama.cppreleasev0.7.0To use the latest
llama.cpprelease available, runnpx -n node-llama-cpp source download --release latest. (learn more) - v3.22.12026-09-28 · 2026-09-28 03:16 UTC
查看更新说明
✨ New: Jev-like structured decisions with any model
3.22.1 (2026-09-28)
Bug Fixes
- binary-split gguf URL name extraction (#662) (df534ff)
- make score decision more precise (#664) (0ad4a86)
Shipped with
llama.cppreleasev0.5.0To use the latest
llama.cpprelease available, runnpx -n node-llama-cpp source download --release latest. (learn more) - v3.22.02026-09-27 · 2026-09-27 18:09 UTC
查看更新说明
✨ New: Jev-like structured decisions with any model
3.22.0 (2026-09-27)
Features
- structured decisions with any model (#654) (35a01d0) (documentation: Using Structured Decisions)
controlledEvaluate: rawlogitssupport (#654) (35a01d0)
Bug Fixes
- arm64 windows build optimizations (#657) (0555c0d)
- reasoning segments edge cases (#654) (35a01d0)
- stop promoting new line as a special token (#654) (35a01d0)
Shipped with
llama.cppreleasev0.5.0To use the latest
llama.cpprelease available, runnpx -n node-llama-cpp source download --release latest. (learn more) - v3.21.12026-09-12 · 2026-09-12 22:09 UTC
查看更新说明
3.21.1 (2026-09-12)
Bug Fixes
Shipped with
llama.cppreleasev0.4.0To use the latest
llama.cpprelease available, runnpx -n node-llama-cpp source download --release latest. (learn more)
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
✨ New: Structured Decisions with any model ✨
Features
- Run LLMs locally on your machine
- Metal, CUDA and Vulkan support
- Pre-built binaries are provided, with a fallback to building from source without
node-gypor Python - Adapts to your hardware automatically, no need to configure anything
- A Complete suite of everything you need to use LLMs in your projects
- Use the CLI to chat with a model without writing any code
- Up-to-date with the latest
llama.cpp. Download and compile the latest release with a single CLI command - Enforce a model to generate output in a parseable format, like JSON, or even force it to follow a specific JSON schema
- Provide a model with functions it can call on demand to retrieve information or perform actions
- Embedding and reranking support
- Structured decisions support with any model
- Safe against special token injection attacks
- Great developer experience with full TypeScript support, and complete documentation
- Much more
Documentation
Try It Without Installing
Chat with a model in your terminal using a single command:
npx -y node-llama-cpp chat
Installation
npm install node-llama-cpp
This package comes with pre-built binaries for macOS, Linux and Windows.
If binaries are not available for your platform, it'll fallback to download a release of llama.cpp and build it from source with cmake.
To disable this behavior, set the environment variable NODE_LLAMA_CPP_SKIP_DOWNLOAD to true.
Usage
import {fileURLToPath} from "url";
import path from "path";
import {getLlama, LlamaChatSession} from "node-llama-cpp";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const llama = await getLlama();
const model = await llama.loadModel({
modelPath: path.join(__dirname, "models", "Meta-Llama-3.1-8B-Instruct.Q4_K_M.gguf")
});
const context = await model.createContext();
const session = new LlamaChatSession({
contextSequence: context.getSequence()
});
const q1 = "Hi there, how are you?";
console.log("User: " + q1);
const a1 = await session.prompt(q1);
console.log("AI: " + a1);
const q2 = "Summarize what you said";
console.log("User: " + q2);
const a2 = await session.prompt(q2);
console.log("AI: " + a2);
For more examples, see the getting started guide
Contributing
To contribute to node-llama-cpp read the contribution guide.
Acknowledgements
- llama.cpp: ggml-org/llama.cpp