Zep Cloud 检索器
Zep 是一个面向 AI 助手应用的长期记忆服务。 使用 Zep,你可以让 AI 助手具备回忆过去对话的能力,无论这些对话多久以前发生过, 同时还能减少幻觉、延迟和成本。
此示例展示了如何在检索链中使用 Zep 检索器,从 Zep 开源记忆存储中检索文档。
安装
注册 Zep Cloud 并创建一个项目。
按照 Zep Cloud Typescript SDK 安装指南 安装并开始使用 Zep。
你需要使用 Zep Cloud 项目的 API 密钥来使用 ZepCloudRetriever。更多信息请参见 Zep Cloud 文档。
配置
:::提示 请参阅安装集成包的一般说明部分。 :::
- npm
- Yarn
- pnpm
npm i @getzep/zep-cloud @langchain/community @langchain/core
yarn add @getzep/zep-cloud @langchain/community @langchain/core
pnpm add @getzep/zep-cloud @langchain/community @langchain/core
使用方法
import { ZepCloudRetriever } from "@langchain/community/retrievers/zep_cloud";
import { randomUUID } from "crypto";
import { ZepClient, type Zep } from "@getzep/zep-cloud";
function sleep(ms: number) {
// eslint-disable-next-line no-promise-executor-return
return new Promise((resolve) => setTimeout(resolve, ms));
}
const zepConfig = {
// Your Zep Cloud Project API key https://help.getzep.com/projects
apiKey: "<Zep Api Key>",
sessionId: `session_${randomUUID()}`,
};
console.log(`Zep Config: ${JSON.stringify(zepConfig)}`);
// Generate chat messages about traveling to France
const chatMessages = [
{
role: "AI",
message: "Bonjour! How can I assist you with your travel plans today?",
},
{ role: "User", message: "I'm planning a trip to France." },
{
role: "AI",
message: "That sounds exciting! What cities are you planning to visit?",
},
{ role: "User", message: "I'm thinking of visiting Paris and Nice." },
{
role: "AI",
message: "Great choices! Are you interested in any specific activities?",
},
{ role: "User", message: "I would love to visit some vineyards." },
{
role: "AI",
message:
"France has some of the best vineyards in the world. I can help you find some.",
},
{ role: "User", message: "That would be great!" },
{ role: "AI", message: "Do you prefer red or white wine?" },
{ role: "User", message: "I prefer red wine." },
{
role: "AI",
message:
"Perfect! I'll find some vineyards that are known for their red wines.",
},
{ role: "User", message: "Thank you, that would be very helpful." },
{
role: "AI",
message:
"You're welcome! I'll also look up some French wine etiquette for you.",
},
{
role: "User",
message: "That sounds great. I can't wait to start my trip!",
},
{
role: "AI",
message:
"I'm sure you'll have a fantastic time. Do you have any other questions about your trip?",
},
{ role: "User", message: "Not at the moment, thank you for your help!" },
];
const zepClient = new ZepClient({
apiKey: zepConfig.apiKey,
});
// Add chat messages to memory
for (const chatMessage of chatMessages) {
let m: Zep.Message;
if (chatMessage.role === "AI") {
m = { role: "ai", roleType: "assistant", content: chatMessage.message };
} else {
m = { role: "human", roleType: "user", content: chatMessage.message };
}
await zepClient.memory.add(zepConfig.sessionId, { messages: [m] });
}
// Wait for messages to be summarized, enriched, embedded and indexed.
await sleep(10000);
// Simple similarity search
const query = "Can I drive red cars in France?";
const retriever = new ZepCloudRetriever({ ...zepConfig, topK: 3 });
const docs = await retriever.invoke(query);
console.log("Simple similarity search");
console.log(JSON.stringify(docs, null, 2));
// mmr reranking search
const mmrRetriever = new ZepCloudRetriever({
...zepConfig,
topK: 3,
searchType: "mmr",
mmrLambda: 0.5,
});
const mmrDocs = await mmrRetriever.invoke(query);
console.log("MMR reranking search");
console.log(JSON.stringify(mmrDocs, null, 2));
// summary search with mmr reranking
const mmrSummaryRetriever = new ZepCloudRetriever({
...zepConfig,
topK: 3,
searchScope: "summary",
searchType: "mmr",
mmrLambda: 0.5,
});
const mmrSummaryDocs = await mmrSummaryRetriever.invoke(query);
console.log("Summary search with MMR reranking");
console.log(JSON.stringify(mmrSummaryDocs, null, 2));
// Filtered search
const filteredRetriever = new ZepCloudRetriever({
...zepConfig,
topK: 3,
filter: {
where: { jsonpath: '$[*] ? (@.foo == "bar")' },
},
});
const filteredDocs = await filteredRetriever.invoke(query);
console.log("Filtered search");
console.log(JSON.stringify(filteredDocs, null, 2));
API Reference:
- ZepCloudRetriever from
@langchain/community/retrievers/zep_cloud
相关内容
Related
- Retriever conceptual guide
- Retriever how-to guides