LangChain 1.0-Agent中间件-汇总消息

发布时间:2026-03-02 22:53:25编辑:123阅读(21)

    LangChain 1.0-Agent中间件-汇总消息

    汇总是最"智能"的压缩策略。与简单的修剪不同,它不是直接删除历史,而是调用一个轻量模型自动总结过去的对话内容,把长历史浓缩成短摘要,再拼接到当前上下文中。LangChain 1.0 提供了现成的SummarizationMiddleware中间件,只需指定:

    触发阈值(max_tokens_before_summary):超出后自动触发摘要;

    摘要模型(model):通常使用成本更低的小模型;

    保留的原始消息数量(messages_to_keep):例如只留最近20条原始消息。

    这种方式兼顾了上下文连续性与效率,适合多轮对话、长期交互、或多Agent系统中做会话记忆压缩。

    示例代码:

    from langchain.agents import create_agent
    from langchain.agents.middleware import SummarizationMiddleware
    from langgraph.checkpoint.memory import InMemorySaver
    from langchain_core.runnables import RunnableConfig
    import requests
    import time
    from lxml import etree
    from langchain.tools import tool
    from langchain_ollama import ChatOllama
    
    # 主模型(执行任务)
    main_model = ChatOllama(
        model="qwen3:8b",
        temperature=0.2,
        top_p=0.95,
    )
    
    # 摘要模型(用于生成会话总结)
    summary_model = ChatOllama(
        model="qwen3-vl:8b",
        temperature=0.2,
        top_p=0.95,
    )
    
    checkpointer = InMemorySaver()
    
    @tool
    def get_weather(city):
        """
        查询即时天气函数
        :param city: 必要参数,字符串类型。用于表示查询天气的具体城市名称
        :return: 返回即时天气的结果,dict类型
        """
        today_time = time.strftime("%Y-%m-%d",time.localtime())
        url = f'https://www.ks121.com/history/?location={city}&startdate={today_time}&enddate={today_time}'
        headers = {
            "sec-ch-ua": '"Not(A:Brand";v="8", "Chromium";v="144", "Google Chrome";v="144"',
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
             Chrome/144.0.0.0 Safari/537.36"
        }
        response = requests.get(url=url, headers=headers, timeout=10)
        html = etree.HTML(response.text)
        res = html.xpath("//div[@class='box']/div[2]/div[1]/div/table/tbody/tr")
        ret = {}
        for i in res:
            one = i.xpath("./td[1]/p[2]/text()")[0]
            two = i.xpath("./td[2]/p[2]/span/text()")[0]
            three = i.xpath("./td[3]/p/text()")[0]
            four = i.xpath("./td[4]/p/text()")[0]
            ret.setdefault('日期', one)
            ret.setdefault('气象', two)
            ret.setdefault('温度', three)
            ret.setdefault('风级', four)
        print(f"工具调用:{today_time}:{city}天气信息:{ret}")
        return ret
    
    agent = create_agent(
        model=main_model,
        tools=[get_weather],
        middleware=[SummarizationMiddleware(
            model=summary_model,
            max_tokens_before_summary=1000, # 超过1000token触发摘要
            messages_to_keep=3,             # 摘要后保留最近3条原始消息
            summary_token_limit=50,        # 摘要最多多少token
        )],
        checkpointer=checkpointer,
    )
    
    config = {
        "configurable":{
            "thread_id":"summary-demo"
        }
    }
    
    resp1 = agent.invoke(
        {"messages":"今天上海的天气? 你好,我叫张三。"},
        config=config,
    )
    
    resp2 = agent.invoke(
        {"messages":"写一首关于春天的诗"},
        config=config,
    )
    
    resp3 = agent.invoke(
        {"messages":"写一篇关于夏天的作文"},
        config=config,
    )
    
    resp4 = agent.invoke(
        {"messages":"写一篇关于秋天的随笔"},
        config=config,
    )
    
    
    resp5 = agent.invoke(
        {"messages":"写一篇关于冬天的散文"},
        config=config,
    )
    
    resp6 = agent.invoke(
        {"messages":"你能总结一下四节的特点吗?"},
        config=config,
    )
    print(resp6["messages"][-1].content)
    
    # 查看总结效果
    state = agent.get_state(config)
    for msg in state.values["messages"]:
        print(msg.type, ":", msg.content)


关键字

上一篇: LangChain 1.0-Agent中间件-删除消息

下一篇: 没有了