更新时间:2026-09-14 GMT+08:00
分享

通过SDK检索

通过SDK可以在Agent代码中检索长期记忆,并将其注入到对话上下文中。

查询记忆列表

列出指定记忆库中的长期记忆记录:

memories = client.list_memories(space_id=space_id, limit=10)

集成记忆

from agentarts.sdk.memory.inner.config import MemorySearchFilter

search_results = client.search_memories(
    space_id=space_id,
    filters=MemorySearchFilter(query="机器学习", top_k=3)
)
# 遍历每条记录
for i, r in enumerate(search_results.results):
    record = r["record"]
    print(f"--- 结果 {i+1} ---")
    print(f"  内容: {record['content'][:50]}")
    print(f"  策略类型: {record['strategy_type']}")
    print(f"  时间: {record['created_at']}")
    print(f"  相关度分数: {r['score']}")

参数说明:

  • query:检索关键词,通常使用用户最新消息。
  • top_k:返回最相关的前N条记忆。
  • 返回结果:返回一个记忆记录列表(results)。列表中的每条记录包含记忆内容、策略类型、时间、相关度分数;列表按相关度分数从高到低排序。

集成模式

将检索到的长期记忆拼接到Agent的系统提示词中,作为上下文信息提供给大模型:

system_prompt = f"""你是一个智能助手。
以下是关于当前用户的历史记忆:
{search_results}
请基于这些记忆提供个性化的回复。"""

SDK检索的详细参数和返回字段说明,参见长期记忆操作。

相关文档