Skip to main content
兼容性仅适用于 Node.js。
此模块基于 llama.cppnode-llama-cpp Node.js 绑定,允许您使用本地运行的 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 ] ]
*/

相关