跳到主要内容
兼容性仅在 Node.js 上可用。
此模块基于 node-llama-cpp 的 Node.js 绑定,用于 llama.cpp,允许您使用本地运行的 LLM。这使您可以使用更小的量化模型,该模型能够在笔记本电脑环境中运行,非常适合测试和草拟想法,而无需支付费用!

设置

您需要安装 node-llama-cpp 模块的主要版本 3 才能与本地模型通信。
npm
npm install -S node-llama-cpp@3
有关安装 LangChain 软件包的一般说明,请参阅此部分
npm
npm install @langchain/community @langchain/core
您还需要一个本地 Llama 3 模型(或 node-llama-cpp 支持的模型)。您需要将此模型的路径作为参数传递给 LlamaCpp 模块(参见示例)。 开箱即用的 node-llama-cpp 针对 macOS 平台进行了优化,并支持 Apple M 系列处理器的 Metal GPU。如果您需要关闭此功能或需要 CUDA 架构支持,请参阅 node-llama-cpp 的文档。 LangChain.js 贡献者注意事项:如果您想运行与此模块相关的测试,您需要将本地模型的路径放入环境变量 LLAMA_PATH 中。

Llama3 安装指南

在您的机器上运行本地 Llama3 模型是先决条件,因此这是一个快速指南,介绍如何获取和构建 Llama 3.1-8B(最小的模型),然后对其进行量化,以便它可以在笔记本电脑上舒适地运行。为此,您的机器上需要安装 python3(建议使用 3.11),以及 gccmake,以便可以构建 llama.cpp

获取 Llama3 模型

要获取 Llama3 的副本,您需要访问 Meta AI 并请求访问他们的模型。一旦 Meta AI 授予您访问权限,您将收到一封包含唯一 URL 的电子邮件,用于访问文件,这将在接下来的步骤中需要。现在创建一个工作目录,例如
mkdir llama3
cd llama3
现在我们需要转到 Meta AI llama-models 存储库,它可以在这里找到。在该存储库中,有下载您选择的模型的说明,您应该使用在电子邮件中收到的唯一 URL。本教程的其余部分假设您已下载 Llama3.1-8B,但此后的任何模型都应该有效。下载模型后,请务必保存模型下载路径,这将在以后使用。

转换和量化模型

在此步骤中,我们需要使用 llama.cpp,因此我们需要下载该存储库。
cd ..
git clone https://github.com/ggerganov/llama.cpp.git
cd llama.cpp
现在我们需要构建 llama.cpp 工具并设置我们的 python 环境。在这些步骤中,假设您的 python 安装可以使用 python3 运行,并且虚拟环境可以命名为 llama3,请根据您自己的情况进行调整。
cmake -B build
cmake --build build --config Release
python3 -m venv llama3
source llama3/bin/activate
激活 llama3 环境后,您应该会看到 (llama3) 前缀出现在您的命令提示符前,以告知您这是活动环境。注意:如果您需要回来构建另一个模型或重新量化模型,请不要忘记再次激活环境;此外,如果您更新 llama.cpp,您将需要重新构建工具并可能安装新的或更新的依赖项!现在我们有了一个活动的 python 环境,我们需要安装 python 依赖项。
python3 -m pip install -r requirements.txt
完成此操作后,我们就可以开始转换和量化 Llama3 模型,以便通过 llama.cpp 在本地使用。需要先转换为 Hugging Face 模型,然后再转换为 GGUF 模型。首先,我们需要使用脚本 convert_llama_weights_to_hf.py 找到路径。将此脚本复制并粘贴到您的当前工作目录中。请注意,使用该脚本可能需要您通过 pip 安装额外的依赖项,请根据需要进行安装。然后,我们需要转换模型,在转换之前,让我们创建目录来存储我们的 Hugging Face 转换和最终模型。
mkdir models/8B
mkdir models/8B-GGUF
python3 convert_llama_weights_to_hf.py --model_size 8B --input_dir <dir-to-your-model> --output_dir models/8B --llama_version 3
python3 convert_hf_to_gguf.py --outtype f16 --outfile models/8B-GGUF/gguf-llama3-f16.bin models/8B
这应该会在我们创建的目录中创建一个转换后的 Hugging Face 模型和最终的 GGUF 模型。请注意,这只是一个转换后的模型,因此它的大小约为 16GB,在下一步中,我们将其量化到大约 4GB。
./build/bin/llama-quantize ./models/8B-GGUF/gguf-llama3-f16.bin ./models/8B-GGUF/gguf-llama3-Q4_0.bin Q4_0
运行此命令应在 `models\8B-GGUF` 目录中创建一个新模型,名为 `gguf-llama3-Q4_0.bin`,这就是我们可以与 LangChain 一起使用的模型。您可以通过使用 `llama.cpp` 工具进行测试来验证此模型是否有效。
./build/bin/llama-cli -m ./models/8B-GGUF/gguf-llama3-Q4_0.bin -cnv -p "You are a helpful assistant"
运行此命令将启动模型进行聊天会话。顺便说一句,如果您的磁盘空间不足,我们只需要这个小型模型,因此您可以备份和/或删除原始的和已转换的 13.5GB 模型。

用法

import { LlamaCpp } from "@langchain/community/llms/llama_cpp";

const llamaPath = "/Replace/with/path/to/your/model/gguf-llama3-Q4_0.bin";
const question = "Where do Llamas come from?";

const model = await LlamaCpp.initialize({ modelPath: llamaPath });

console.log(`You: ${question}`);
const response = await model.invoke(question);
console.log(`AI : ${response}`);

流式处理

import { LlamaCpp } from "@langchain/community/llms/llama_cpp";

const llamaPath = "/Replace/with/path/to/your/model/gguf-llama3-Q4_0.bin";

const model = await LlamaCpp.initialize({
  modelPath: llamaPath,
  temperature: 0.7,
});

const prompt = "Tell me a short story about a happy Llama.";

const stream = await model.stream(prompt);

for await (const chunk of stream) {
  console.log(chunk);
}

/*


 Once
  upon
  a
  time
 ,
  in
  the
  rolling
  hills
  of
  Peru
 ...
 */
```;

## Related


- [Models guide](/oss/javascript/langchain/models)

---

<Callout icon="pen-to-square" iconType="regular">
    [Edit the source of this page on GitHub.](https://github.com/langchain-ai/docs/edit/main/src/oss/javascript/integrations/llms/llama_cpp.mdx)
</Callout>
<Tip icon="terminal" iconType="regular">
    [Connect these docs programmatically](/use-these-docs) to Claude, VSCode, and more via MCP for    real-time answers.
</Tip>
© . This site is unofficial and not affiliated with LangChain, Inc.