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

v3.22.3发布于 2026-11-05 15:44 UTC2,202star +2 / 7 天230fork同步于 2026-11-10 05:44 UTC

怎么上手

由 README 提炼,执行前请确认
npm install node-llama-cpp
npx -y node-llama-cpp chat
硬件要求按 README 说明

README 未说明具体显存要求;支持 Metal、CUDA 和 Vulkan,会自动适配硬件,无需手动配置。

需要 CUDA
下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

在 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
  • 支持函数调用、嵌入与重排序、结构化决策

容易踩的坑

模型文件需自行准备并以 gguf 路径传给 loadModel,包内不含模型;平台无预编译二进制时会自动下载 llama.cpp 并用 cmake 从源码构建,可设 NODE_LLAMA_CPP_SKIP_DOWNLOAD=true 关闭该行为。

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本v3.22.3
  1. 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 for minLength and maxLength checks (#675) (8030e0d)
    • correct riscv64 package metadata (#677) (8354dff)

    Shipped with llama.cpp release v0.9.0

    To use the latest llama.cpp release available, run npx -n node-llama-cpp source download --release latest. (learn more)

  2. 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.cpp release v0.7.0

    To use the latest llama.cpp release available, run npx -n node-llama-cpp source download --release latest. (learn more)

  3. 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


    Shipped with llama.cpp release v0.5.0

    To use the latest llama.cpp release available, run npx -n node-llama-cpp source download --release latest. (learn more)

  4. 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

    Bug Fixes


    Shipped with llama.cpp release v0.5.0

    To use the latest llama.cpp release available, run npx -n node-llama-cpp source download --release latest. (learn more)

  5. v3.21.12026-09-12 · 2026-09-12 22:09 UTC
    查看更新说明

    3.21.1 (2026-09-12)

    Bug Fixes

    • ci: wait for npm to serve the package before building electron examples (#649) (5b4ebc5)

    Shipped with llama.cpp release v0.4.0

    To use the latest llama.cpp release available, run npx -n node-llama-cpp source download --release latest. (learn more)

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    Build License Types Version

    ✨ New: Structured Decisions with any model ✨

    Features

    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