Zep Cloud
Zep 是一款为 AI 助手应用设计的长期记忆服务。借助 Zep,您可以为 AI 助手提供回忆过去对话的能力,无论对话发生在多久之前,同时还能减少幻觉、延迟和成本。
注意: ZepCloudVectorStore
与 Documents
一起使用,旨在用作 Retriever
。它提供与 Zep 的 ZepCloudMemory
类不同的功能,后者专为持久化、丰富化和搜索用户的聊天记录而设计。
为什么选择 Zep 的 VectorStore?🤖🚀
Zep 使用 Zep 服务器本地的低延迟模型自动嵌入添加到 Zep Vector Store 的文档。Zep TS/JS 客户端可用于非 Node 边缘环境。这两者与 Zep 的聊天记忆功能相结合,使 Zep 成为构建注重延迟和性能的对话式 LLM 应用的理想选择。
支持的搜索类型
Zep 支持相似性搜索和最大边际相关性 (MMR) 搜索。MMR 搜索对于检索增强生成应用尤其有用,因为它会对结果进行重新排序,以确保返回文档的多样性。
安装
注册 Zep Cloud 并创建一个项目。
按照 Zep Cloud Typescript SDK 安装指南 进行安装并开始使用 Zep。
使用方法
您需要您的 Zep Cloud 项目 API 密钥才能使用 Zep VectorStore。有关更多信息,请参阅 Zep Cloud 文档。
Zep 默认自动嵌入所有文档,并且不期望从用户那里接收任何嵌入。由于 LangChain 需要传入 Embeddings
实例,我们传入 FakeEmbeddings
。
提示
有关安装集成包的一般说明,请参阅此部分。
示例:从文档创建 ZepVectorStore 并进行查询
- npm
- Yarn
- pnpm
npm install @getzep/zep-cloud @langchain/openai @langchain/community @langchain/core
yarn add @getzep/zep-cloud @langchain/openai @langchain/community @langchain/core
pnpm add @getzep/zep-cloud @langchain/openai @langchain/community @langchain/core
import { ZepCloudVectorStore } from "@langchain/community/vectorstores/zep_cloud";
import { FakeEmbeddings } from "@langchain/core/utils/testing";
import { TextLoader } from "langchain/document_loaders/fs/text";
import { randomUUID } from "crypto";
const loader = new TextLoader("src/document_loaders/example_data/example.txt");
const docs = await loader.load();
const collectionName = `collection${randomUUID().split("-")[0]}`;
const zepConfig = {
// Your Zep Cloud Project API key https://help.getzep.com/projects
apiKey: "<Zep Api Key>",
collectionName,
};
// We're using fake embeddings here, because Zep Cloud handles embedding for you
const embeddings = new FakeEmbeddings();
const vectorStore = await ZepCloudVectorStore.fromDocuments(
docs,
embeddings,
zepConfig
);
// Wait for the documents to be embedded
// eslint-disable-next-line no-constant-condition
while (true) {
const c = await vectorStore.client.document.getCollection(collectionName);
console.log(
`Embedding status: ${c.documentEmbeddedCount}/${c.documentCount} documents embedded`
);
// eslint-disable-next-line no-promise-executor-return
await new Promise((resolve) => setTimeout(resolve, 1000));
if (c.documentEmbeddedCount === c.documentCount) {
break;
}
}
const results = await vectorStore.similaritySearchWithScore("bar", 3);
console.log("Similarity Results:");
console.log(JSON.stringify(results));
const results2 = await vectorStore.maxMarginalRelevanceSearch("bar", {
k: 3,
});
console.log("MMR Results:");
console.log(JSON.stringify(results2));
API 参考
- ZepCloudVectorStore 来自
@langchain/community/vectorstores/zep_cloud
- FakeEmbeddings 来自
@langchain/core/utils/testing
- TextLoader 来自
langchain/document_loaders/fs/text
示例:将 ZepCloudVectorStore 与表达式语言一起使用
import { ZepClient } from "@getzep/zep-cloud";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { ConsoleCallbackHandler } from "@langchain/core/tracers/console";
import { ChatOpenAI } from "@langchain/openai";
import { Document } from "@langchain/core/documents";
import {
RunnableLambda,
RunnableMap,
RunnablePassthrough,
} from "@langchain/core/runnables";
import { ZepCloudVectorStore } from "@langchain/community/vectorstores/zep_cloud";
import { StringOutputParser } from "@langchain/core/output_parsers";
async function combineDocuments(docs: Document[], documentSeparator = "\n\n") {
const docStrings: string[] = await Promise.all(
docs.map((doc) => doc.pageContent)
);
return docStrings.join(documentSeparator);
}
// Your Zep Collection Name
const collectionName = "<Zep Collection Name>";
const zepClient = new ZepClient({
// Your Zep Cloud Project API key https://help.getzep.com/projects
apiKey: "<Zep Api Key>",
});
const vectorStore = await ZepCloudVectorStore.init({
client: zepClient,
collectionName,
});
const prompt = ChatPromptTemplate.fromMessages([
[
"system",
`Answer the question based only on the following context: {context}`,
],
["human", "{question}"],
]);
const model = new ChatOpenAI({
temperature: 0.8,
modelName: "gpt-3.5-turbo-1106",
});
const retriever = vectorStore.asRetriever();
const setupAndRetrieval = RunnableMap.from({
context: new RunnableLambda({
func: (input: string) => retriever.invoke(input).then(combineDocuments),
}),
question: new RunnablePassthrough(),
});
const outputParser = new StringOutputParser();
const chain = setupAndRetrieval
.pipe(prompt)
.pipe(model)
.pipe(outputParser)
.withConfig({
callbacks: [new ConsoleCallbackHandler()],
});
const result = await chain.invoke("Project Gutenberg?");
console.log("result", result);
API 参考
- ChatPromptTemplate 来自
@langchain/core/prompts
- ConsoleCallbackHandler 来自
@langchain/core/tracers/console
- ChatOpenAI 来自
@langchain/openai
- Document 来自
@langchain/core/documents
- RunnableLambda 来自
@langchain/core/runnables
- RunnableMap 来自
@langchain/core/runnables
- RunnablePassthrough 来自
@langchain/core/runnables
- ZepCloudVectorStore 来自
@langchain/community/vectorstores/zep_cloud
- StringOutputParser 来自
@langchain/core/output_parsers