Zep 云
¥Zep Cloud
Zep 是一项面向 AI 助手应用的长期内存服务。使用 Zep,你可以为 AI 助手提供回忆过去对话的能力,无论对话有多久远,同时还能减少幻觉、延迟和成本。
¥Zep is a long-term memory service for AI Assistant apps. With Zep, you can provide AI assistants with the ability to recall past conversations, no matter how distant, while also reducing hallucinations, latency, and cost.
注意:ZepCloudVectorStore
与 Documents
配合使用,旨在用作 Retriever
。它为 Zep 的 ZepCloudMemory
类提供了独立的功能,该类旨在持久化、丰富和搜索用户的聊天记录。
¥Note: The ZepCloudVectorStore
works with Documents
and is intended to be used as a Retriever
.
It offers separate functionality to Zep's ZepCloudMemory
class, which is designed for persisting, enriching
and searching your user's chat history.
为什么选择 Zep 的 VectorStore?🤖🚀
¥Why Zep's VectorStore? 🤖🚀
Zep 使用 Zep 服务器本地的低延迟模型自动嵌入添加到 Zep 向量存储 (Zep Vector Store) 的文档。Zep TS/JS 客户端可以在非 Node 边缘环境中使用。这两个概念与 Zep 的聊天记忆功能相结合,使 Zep 成为构建会话式 LLM 应用的理想选择,因为在这些应用中延迟和性能至关重要。
¥Zep automatically embeds documents added to the Zep Vector Store using low-latency models local to the Zep server. The Zep TS/JS client can be used in non-Node edge environments. These two together with Zep's chat memory functionality make Zep ideal for building conversational LLM apps where latency and performance are important.
支持的搜索类型
¥Supported Search Types
Zep 支持相似性搜索和最大边际相关性 (MMR) 搜索。MMR 搜索对于检索增强生成 (RGA) 应用尤其有用,因为它会重新排序结果以确保返回文档的多样性。
¥Zep supports both similarity search and Maximal Marginal Relevance (MMR) search. MMR search is particularly useful for Retrieval Augmented Generation applications as it re-ranks results to ensure diversity in the returned documents.
安装
¥Installation
注册 Zep 云 并创建项目。
¥Sign up for Zep Cloud and create a project.
按照 Zep Cloud Typescript SDK 安装指南 的步骤安装并开始使用 Zep。
¥Follow the Zep Cloud Typescript SDK Installation Guide to install and get started with Zep.
用法
¥Usage
你需要 Zep Cloud Project API 密钥才能使用 Zep VectorStore。有关更多信息,请参阅 Zep Cloud 文档。
¥You'll need your Zep Cloud Project API Key to use the Zep VectorStore. See the Zep Cloud docs for more information.
Zep 默认自动嵌入所有文档,并且不期望从用户接收任何嵌入。由于 LangChain 需要传入 Embeddings
实例,因此我们传入了 FakeEmbeddings
。
¥Zep auto embeds all documents by default, and it's not expecting to receive any embeddings from the user.
Since LangChain requires passing in a Embeddings
instance, we pass in FakeEmbeddings
.
示例:从文档和查询创建 ZepVectorStore
¥Example: Creating a ZepVectorStore from Documents & Querying
- 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 Reference:
- ZepCloudVectorStore from
@langchain/community/vectorstores/zep_cloud
- FakeEmbeddings from
@langchain/core/utils/testing
- TextLoader from
langchain/document_loaders/fs/text
示例:将 ZepCloudVectorStore 与表达式语言结合使用
¥Example: Using ZepCloudVectorStore with Expression Language
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 Reference:
- ChatPromptTemplate from
@langchain/core/prompts
- ConsoleCallbackHandler from
@langchain/core/tracers/console
- ChatOpenAI from
@langchain/openai
- Document from
@langchain/core/documents
- RunnableLambda from
@langchain/core/runnables
- RunnableMap from
@langchain/core/runnables
- RunnablePassthrough from
@langchain/core/runnables
- ZepCloudVectorStore from
@langchain/community/vectorstores/zep_cloud
- StringOutputParser from
@langchain/core/output_parsers
相关
¥Related
向量存储 概念指南
¥Vector store conceptual guide
向量存储 操作指南
¥Vector store how-to guides