跳到主要内容
兼容性仅在 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 的文档。 有关获取和准备 llama3 的建议,请参阅此模块的 LLM 版本的文档。 致 LangChain.js 贡献者:如果您想运行与此模块相关的测试,您需要将本地模型的路径放入环境变量 LLAMA_PATH 中。

用法

基本用法

我们需要提供本地 Llama3 模型的路径,此外,此模块中的 embeddings 属性始终设置为 true
import { LlamaCppEmbeddings } from "@langchain/community/embeddings/llama_cpp";

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

const embeddings = await LlamaCppEmbeddings.initialize({
  modelPath: llamaPath,
});

const res = embeddings.embedQuery("Hello Llama!");

console.log(res);

/*
	[ 15043, 365, 29880, 3304, 29991 ]
*/

文档嵌入

import { LlamaCppEmbeddings } from "@langchain/community/embeddings/llama_cpp";

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

const documents = ["Hello World!", "Bye Bye!"];

const embeddings = await LlamaCppEmbeddings.initialize({
  modelPath: llamaPath,
});

const res = await embeddings.embedDocuments(documents);

console.log(res);

/*
	[ [ 15043, 2787, 29991 ], [ 2648, 29872, 2648, 29872, 29991 ] ]
*/

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.