智能体记忆装饰器
智能体记忆装饰器(代理记忆装饰器)
相关源文件
本章引用的主要源码文件:
cognee-mcp/src/strip_vectors.pycognee/infrastructure/llm/LLMGateway.pycognee/modules/agent_memory/__init__.pycognee/modules/agent_memory/decorator.pycognee/modules/agent_memory/runtime.pycognee/tests/integration/modules/agent_memory/test_agent_memory_integration.pycognee/tests/test_agent_memory_e2e.pycognee/tests/unit/modules/agent_memory/test_agent_memory.pyexamples/guides/agent_memory_quickstart.pyexamples/guides/recall_core.pyexamples/guides/temporal_recall.py
@agent_memory 装饰器是 Cognee 中的高级工具,旨在为 AI 代理提供持久化记忆和自动化可观测性。它在函数执行前管理从知识图谱中检索相关上下文,并确保执行轨迹(输入、输出和错误)被保存回记忆系统中 cognee/modules/agent_memory/decorator.py:23-49。
核心组件
AgentMemoryConfig
AgentMemoryConfig 数据类定义了特定装饰函数的记忆系统行为。它在装饰时被校验和规范化,以确保 session_memory_last_n 和 memory_top_k 等参数在有效范围内 cognee/modules/agent_memory/runtime.py:28-46,cognee/modules/agent_memory/runtime.py:98-116。
| 参数 | 类型 | 描述 |
|---|---|---|
with_memory | bool | 如果为 True,则在调用函数前从 Cognee 知识图谱检索上下文 cognee/modules/agent_memory/runtime.py:31。 |
with_session_memory | bool | 启用从会话缓存中检索最近的交互历史 cognee/modules/agent_memory/runtime.py:32。 |
save_session_traces | bool | 如果为 True,则将函数调用详情(轨迹)持久化到会话管理器 cognee/modules/agent_memory/runtime.py:33。 |
memory_query_fixed | str | 用于查询记忆系统的静态字符串 cognee/modules/agent_memory/runtime.py:34。 |
memory_query_from_method | str | 函数参数的名称,其值将用作记忆查询 cognee/modules/agent_memory/runtime.py:35。 |
session_id | str | 代理会话的唯一标识符,用于隔离轨迹历史 cognee/modules/agent_memory/runtime.py:40。 |
persist_session_trace_after | int | 交互次数阈值,超过此次数后会话轨迹将被移至长期记忆(图数据库) cognee/modules/agent_memory/runtime.py:44。 |
AgentScope
AgentScope 表示已授权的数据集上下文,用于 Cognee 支持的记忆检索。它将 User 链接到特定的 dataset_id 和 dataset_name cognee/modules/agent_memory/runtime.py:50-56。
AgentMemoryContext
每次调用时的执行状态,用于在检索、包装调用和轨迹持久化之间共享数据。它存储在名为 _agent_memory_context_var 的 contextvars.ContextVar 中,以确保在整个异步执行链中可访问 cognee/modules/agent_memory/runtime.py:59-75。
来源:cognee/modules/agent_memory/runtime.py:28-75,cognee/modules/agent_memory/decorator.py:23-41,cognee/modules/agent_memory/runtime.py:98-116
执行流程
该装饰器包装异步函数,并管理基于 contextvars 的生命周期,以确保即使在深层调用栈中也能访问记忆上下文 cognee/modules/agent_memory/decorator.py:86-115。
记忆注入过程
- 作用域解析:系统通过
resolve_agent_user识别User,通过resolve_agent_dataset_scope识别AgentScopecognee/modules/agent_memory/decorator.py:91-93。 - 上下文检索:如果启用了
with_memory或with_session_memory,则调用retrieve_memory_context获取图数据和会话历史cognee/modules/agent_memory/decorator.py:104。 - 大语言模型(LLM)注入:当代理调用
LLMGateway.acreate_structured_output时,_inject_agent_memory辅助函数会自动将检索到的记忆上下文前置到text_input中cognee/infrastructure/llm/LLMGateway.py:14-21,cognee/infrastructure/llm/LLMGateway.py:65。 - 轨迹持久化:函数执行完成(或失败)后,调用
persist_trace保存AgentMemoryContext状态cognee/modules/agent_memory/decorator.py:115。
数据流图:记忆注入
此图展示了装饰器如何将高级函数调用桥接到低级大语言模型(LLM)网关。
"自然语言空间"到"代码实体空间"
来源:cognee/modules/agent_memory/decorator.py:87-115,cognee/infrastructure/llm/LLMGateway.py:14-21,cognee/infrastructure/llm/LLMGateway.py:59-65,cognee/modules/agent_memory/runtime.py:74-81
代理轨迹与持久化
轨迹捕获了代理调用的生命周期。它们由 SessionManager 管理,可以作为图节点持久化,也可以存储在会话缓存中 cognee/modules/agent_memory/decorator.py:115,cognee/modules/agent_memory/runtime.py:69-71。
轨迹状态
执行轨迹捕获以下内容:
origin_function:装饰函数的__qualname__cognee/modules/agent_memory/runtime.py:62。method_params:传递给函数的参数字典,通过build_method_params构建cognee/modules/agent_memory/runtime.py:64。status:从 "running" 转换为 "success" 或 "error"cognee/modules/agent_memory/runtime.py:70。method_return_value:包装函数返回的结果cognee/modules/agent_memory/runtime.py:69。error_message:捕获到的任何异常的字符串表示cognee/modules/agent_memory/runtime.py:71。
轨迹持久化图
此图映射了保存轨迹所涉及的代码实体。
"代码实体空间"到"持久化层"
来源:cognee/modules/agent_memory/decorator.py:113-115,cognee/modules/agent_memory/runtime.py:59-72,cognee/infrastructure/session/session_manager.py,cognee/tests/integration/modules/agent_memory/test_agent_memory_integration.py:142-168
安全与权限
该装饰器通过在执行前解析 User 和 AgentScope 来强制实施多租户 cognee/modules/agent_memory/decorator.py:91-93。
- 用户解析:
resolve_agent_user从配置中获取用户,或默认使用系统默认用户cognee/modules/agent_memory/runtime.py:12,cognee/modules/agent_memory/decorator.py:91。 - 数据集权限:如果启用了
with_memory,系统会通过resolve_agent_dataset_scope验证用户对dataset_name拥有必要的权限cognee/modules/agent_memory/runtime.py:14-15,cognee/modules/agent_memory/decorator.py:93。 - 校验:无效配置(例如同时提供
memory_query_fixed和memory_query_from_method)会导致CogneeValidationErrorcognee/modules/agent_memory/runtime.py:177-181。 - 权限约束:对于共享记忆检索,用户通常需要对数据集同时拥有读写权限
cognee/tests/test_agent_memory_e2e.py:99-102。
来源:cognee/modules/agent_memory/runtime.py:12-20,cognee/modules/agent_memory/decorator.py:75-84,cognee/tests/test_agent_memory_e2e.py:99-102