跳转到内容

本地大语言模型

LlamaIndex.TS 支持 OpenAI 和 其他远程 LLM API。您也可以在本地机器上运行本地 LLM!

运行本地LLM最简单的方式是通过我们朋友在Ollama所做的出色工作,他们提供了一个简单易用的客户端,可以为您下载、安装并运行不断增长的模型系列

他们在主页上为Mac、Linux和Windows提供了一键安装程序。

由于我们将要进行智能体工作,我们需要一个非常强大的模型,但最大的模型很难在笔记本电脑上运行。我们认为mixtral 8x7b在性能和资源之间取得了良好平衡,但llama3是另一个很好的选择。你可以通过运行以下命令来运行Mixtral:

Terminal window
ollama run mixtral:8x7b

首次运行时,它将自动为您下载并安装模型。

要在代码中切换LLM,首先需要确保安装Ollama模型提供者的软件包:

npm i @llamaindex/ollama

然后,要告诉LlamaIndex使用本地LLM,请使用Settings对象:

import { Settings } from "llamaindex";
import { ollama } from "@llamaindex/ollama";
Settings.llm = ollama({
model: "mixtral:8x7b",
});

如果你正在进行检索增强生成,LlamaIndex.TS 也会调用 OpenAI 来索引和嵌入你的数据。要完全实现本地化,你可以像这样使用 Huggingface 的本地嵌入模型:

首先安装 Huggingface 模型提供程序包:

npm i @llamaindex/huggingface

然后在你的代码中设置嵌入模型:

Settings.embedModel = new HuggingFaceEmbedding({
modelType: "BAAI/bge-small-en-v1.5",
quantized: false,
});

首次运行时,它将下载嵌入模型以运行。

在本地LLM和本地嵌入模型就位的情况下,您可以照常执行RAG操作,所有处理都将在您的机器上进行,无需调用API:

async function main() {
// Load essay from abramov.txt in Node
const path = "node_modules/llamaindex/examples/abramov.txt";
const essay = await fs.readFile(path, "utf-8");
// Create Document object with essay
const document = new Document({ text: essay, id_: path });
// Split text and create embeddings. Store them in a VectorStoreIndex
const index = await VectorStoreIndex.fromDocuments([document]);
// Query the index
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query({
query: "What did the author do in college?",
});
// Output response
console.log(response.toString());
}
main().catch(console.error);

您可以查看完整示例文件