Conversation Summary

Conversation Summary 是用 LLM 将旧对话消息压缩成摘要的技术。它在保留关键信息的同时大幅减少 token 消耗,是长对话场景的必备策略。

#type / concept #status / evergreen #tech / ai

[!info] related notes

Conversation Summary

一句话定义

Conversation Summary 是用 LLM 将旧对话消息压缩成摘要的技术。10 条消息可能有 2000 token,但摘要只需要 200 token,节省了 90% 的上下文空间。

核心原理

摘要触发条件

条件说明
消息数量超过 N 条消息时触发
Token 数量累计 token 超过阈值时触发
轮次每 N 轮对话触发一次

摘要 Prompt

SUMMARY_PROMPT = """
请用 100-200 字总结以下对话的要点。保留:
1. 用户的核心需求
2. 已经确定的信息
3. 待解决的问题
4. 用户的偏好

对话:
{messages}

摘要:
"""

Python 实现

class ConversationSummarizer:
    def __init__(self, llm):
        self.llm = llm

    async def summarize(self, messages: list[Message]) -> str:
        # 格式化消息
        formatted = "\n".join([
            f"{m.role}: {m.content}" for m in messages
        ])

        # 生成摘要
        summary = await self.llm.chat(
            SUMMARY_PROMPT.format(messages=formatted)
        )

        return summary

    async def incremental_summarize(self, existing_summary: str, new_messages: list[Message]) -> str:
        """增量摘要:基于已有摘要和新消息生成新摘要"""
        prompt = f"""
之前的摘要:
{existing_summary}

新的对话:
{format_messages(new_messages)}

请更新摘要,整合新信息:
"""
        return await self.llm.chat(prompt)

常见设计模式

1. 全量摘要

所有旧消息一次性生成摘要。

2. 增量摘要

已有摘要 + 新消息 → 更新摘要。避免重复处理旧消息。

3. 分层摘要

最近 5 轮保留原文,5-20 轮用简短摘要,20 轮前用一句话摘要。

常见坑

  1. 摘要太粗糙: 丢失关键细节
  2. 摘要太长: 摘要本身占太多 token
  3. 不做增量摘要: 每次都全量摘要,浪费 LLM 调用
  4. 摘要不更新: 新消息到达后没有重新摘要

参考资料

创建于 2026/6/30 更新于 2026/7/15