Skip to main content

Zep 云服务

Zep 是一个面向 AI 助手应用的长期记忆服务。 使用 Zep,你可以让 AI 助手具备回忆过去对话的能力,无论这些对话多久以前发生过, 同时还能减少幻觉、延迟和成本。

注意: ZepCloudVectorStore 用于处理 Documents,并被设计为一个 Retriever(检索器)使用。 它的功能与 Zep 的 ZepCloudMemory 类不同,后者用于持久化、增强和搜索用户的聊天历史。

为什么选择 Zep 的 VectorStore? 🤖🚀

Zep 使用部署在 Zep 服务器本地的低延迟模型,自动对添加到 Zep 向量存储的文档进行嵌入处理。 Zep 的 TypeScript/JavaScript 客户端可以在非 Node 的边缘环境中使用。 这两者与 Zep 的聊天记忆功能结合,使 Zep 成为构建对延迟和性能要求较高的对话式 LLM 应用的理想选择。

支持的搜索类型

Zep 支持相似性搜索和最大边界相关性(MMR)搜索。 MMR 搜索在检索增强生成(RAG)应用中特别有用,因为它会对搜索结果进行重新排序,以确保返回的文档具有多样性。

安装

注册 Zep Cloud 并创建一个项目。

按照 Zep Cloud Typescript SDK 安装指南 安装并开始使用 Zep。

使用方法

要使用 Zep VectorStore,你需要 Zep Cloud 项目的 API 密钥。更多信息请参阅 Zep Cloud 文档

默认情况下,Zep 会自动对所有文档进行嵌入处理,它不会期望从用户那里接收任何嵌入向量。 由于 LangChain 需要传入一个 Embeddings 实例,因此我们传入 FakeEmbeddings

:::提示 请参阅安装集成包的一般说明部分。 :::

示例:通过 Documents 创建 ZepVectorStore 并进行查询

npm install @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 Reference:

示例:将 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,
model: "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 Reference:

相关内容


Was this page helpful?


You can also leave detailed feedback on GitHub.