Skip to content

Memo 更新机制

启用 memo

以下任意方式都可启用 memo:

python
session.use_memo(chars=6000, messages=12, every_n_turns=2)
# 或
session.configure(mode="memo", limit={"chars": 6000, "messages": 12}, every_n_turns=2)
# 或
agent.enable_session_memo(chars=6000, messages=12, every_n_turns=2)

memo_update_handler

memo_update_handler 用于从历史消息中抽取稳定信息:

python
def memo_update_handler(memo, messages, attachments, settings):
    # memo: dict
    # messages: list[ChatMessage]
    # attachments: list
    # settings: Settings
    return memo

示例:用模型更新 memo

python
from agently.core import ModelRequest

def memo_update_handler(memo, messages, attachments, settings):
    requester = ModelRequest(
        agent.plugin_manager,
        agent_name=agent.name,
        parent_settings=settings,
    )
    prompt_input = {
        "current_memo": memo,
        "messages": [m.model_dump() for m in messages],
        "attachments": attachments,
    }
    instruct = [
        "You are a memory manager.",
        "Return the updated memo dictionary only.",
    ]
    output_schema = {"memo": (dict, "Updated memo dictionary")}
    data = requester.input(prompt_input).instruct(instruct).output(output_schema).get_data()
    if isinstance(data, dict) and isinstance(data.get("memo"), dict):
        return data["memo"]
    return memo

memo 何时更新

  • 发生 resize(lite/deep)时
  • 触发 every_n_turnsmax_messages_text_lengthmax_keep_messages_count
  • 手动 session.resize(force="deep")

memo 更新与是否“裁剪消息”无关;只要触发 resize,就会更新 memo。

memo 提示词

默认提示词位于:

session.memo.instruct

可覆盖为你的业务模板:

python
session.set_settings(
    "session.memo.instruct",
    [
        "Extract stable preferences and facts.",
        "Return memo as JSON dict only.",
    ],
)

附件与多模态摘要

如果消息中包含附件(图像、文件等),可以设置:

python
session.set_attachment_summary_handler(handler)

handler 会接收附件数据,返回结构化摘要,自动并入 memo 更新。