Skip to content

03-summary-memory.ts

对话摘要记忆示例,用摘要替代完整历史。

功能介绍

这个示例演示了摘要记忆的概念:将长对话历史总结成一段摘要,然后将摘要作为上下文。这样既能保留重要信息,又不会占用太多 token。

使用场景

  • 极长对话场景(如几天的连续对话)
  • 需要长期记忆的应用
  • 减少 token 消耗
  • 会话历史总结

学习要点

  1. 创建专门的摘要提示模板
  2. 让 AI 将对话历史总结成简洁的摘要
  3. 将摘要放入系统提示中供后续使用
  4. 平衡摘要的详细程度和 token 成本
  5. 进行环境变量检查和错误处理

源码

typescript
import "dotenv/config";
import { ChatOpenAI } from "@langchain/openai";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { StringOutputParser } from "@langchain/core/output_parsers";

if (!process.env.ZHIPUAI_API_KEY) {
  throw new Error("ZHIPUAI_API_KEY is not set in environment variables");
}

async function main() {
  try {
    console.log("=== ConversationSummaryMemory 示例 ===\n");

    const model = new ChatOpenAI({
      model: "DeepSeek-V4-Pro",
      temperature: 0.7,
      configuration: {
        baseURL: "https://ark.cn-beijing.volces.com/api/coding/v3/",
        apiKey: process.env.ZHIPUAI_API_KEY,
      },
    });

    // 模拟历史对话
    const conversationHistory = [
      { human: "你好,我叫小明,我是一名程序员。" },
      { ai: "你好小明!很高兴认识你,程序员是一个很棒的职业。" },
      { human: "我喜欢用 JavaScript 和 TypeScript 编程。" },
      { ai: "JavaScript 和 TypeScript 都是很流行的语言!" },
      { human: "最近我在学习 LangChain。" },
      { ai: "LangChain 是一个强大的框架,可以让你构建很棒的 LLM 应用。" },
    ];

    // 将历史格式化为对话文本
    const conversationText = conversationHistory
      .map((msg) => {
        if ("human" in msg) return `Human: ${msg.human}`;
        return `AI: ${msg.ai}`;
      })
      .join("\n");

    // 创建摘要提示
    const summaryPrompt = ChatPromptTemplate.fromTemplate(`
      请将以下对话历史总结成一段简洁的摘要:

      {conversation}

      摘要:
    `);

    const summaryChain = summaryPrompt.pipe(model).pipe(new StringOutputParser());

    console.log("--- 原始对话 ---");
    console.log(conversationText);
    console.log("\n--- 对话摘要 ---");

    const summary = await summaryChain.invoke({ conversation: conversationText });
    console.log(summary);
    console.log("\n");

    // 使用摘要进行对话
    const chatPrompt = ChatPromptTemplate.fromMessages([
      ["system", "你是一个友好的 AI 助手。以下是对话摘要:{summary}"],
      ["human", "{input}"],
    ]);

    const chatChain = chatPrompt.pipe(model).pipe(new StringOutputParser());

    console.log("--- 使用摘要对话 ---");
    console.log("用户: 我叫什么名字?我是做什么的?");
    const response = await chatChain.invoke({ summary, input: "我叫什么名字?我是做什么的?" });
    console.log("AI:", response);
  } catch (error) {
    console.error("Error during summary memory example:", error);
    process.exit(1);
  }
}

main().catch(console.error);

运行方式

bash
npm run dev src/06-memory/03-summary-memory.ts