跳到主要内容
Zep 是一种用于 AI 助手应用程序的长期记忆服务。通过 Zep,您可以让 AI 助手回忆过去的对话,无论它们有多遥远,同时还能减少幻觉、延迟和成本。
对 Zep 云感兴趣?请参阅 Zep 云安装指南
此示例展示了如何使用 Zep 检索器在检索链中从 Zep 开源内存存储中检索文档。

安装

请遵循 Zep 开源快速入门指南 来安装和开始使用 Zep。

设置

有关安装 LangChain 软件包的一般说明,请参阅此部分
npm
npm i @getzep/zep-js @langchain/community @langchain/core

用法

import { ZepRetriever } from "@langchain/community/retrievers/zep";
import { ZepMemory } from "@langchain/community/memory/zep";
import { Memory as MemoryModel, Message } from "@getzep/zep-js";
import { randomUUID } from "crypto";

function sleep(ms: number) {
  // eslint-disable-next-line no-promise-executor-return
  return new Promise((resolve) => setTimeout(resolve, ms));
}

export const run = async () => {
  const zepConfig = {
    url: process.env.ZEP_URL || "https://:8000",
    sessionId: `session_${randomUUID()}`,
  };

  console.log(`Zep Config: ${JSON.stringify(zepConfig)}`);

  const memory = new ZepMemory({
    baseURL: zepConfig.url,
    sessionId: zepConfig.sessionId,
  });

  // 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 = await memory.zepClientPromise;
  if (!zepClient) {
    throw new Error("ZepClient is not initialized");
  }

  // Add chat messages to memory
  for (const chatMessage of chatMessages) {
    let m: MemoryModel;
    if (chatMessage.role === "AI") {
      m = new MemoryModel({
        messages: [new Message({ role: "ai", content: chatMessage.message })],
      });
    } else {
      m = new MemoryModel({
        messages: [
          new Message({ role: "human", content: chatMessage.message }),
        ],
      });
    }

    await zepClient.memory.addMemory(zepConfig.sessionId, 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 ZepRetriever({ ...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 ZepRetriever({
    ...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 ZepRetriever({
    ...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 ZepRetriever({
    ...zepConfig,
    topK: 3,
    filter: {
      where: { jsonpath: '$.system.entities[*] ? (@.Label == "GPE")' },
    },
  });
  const filteredDocs = await filteredRetriever.invoke(query);
  console.log("Filtered search");
  console.log(JSON.stringify(filteredDocs, null, 2));
};

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.