agentscope.memory¶
记忆模块。
- class MemoryBase[source]¶
基类:
StateModuleagentscope中智能体记忆模块的基础类。
- abstract async add(*args, **kwargs)[source]¶
向记忆库中添加项目。
- Parameters:
参数 (任意类型)
kwargs (任何)
- Return type:
无
- abstract async delete(*args, **kwargs)[source]¶
从记忆中删除项目。
- Parameters:
参数 (任意类型)
kwargs (任何)
- Return type:
无
- abstract async retrieve(*args, **kwargs)[source]¶
从记忆中检索项目。
- Parameters:
参数 (任意类型)
kwargs (任何)
- Return type:
无
- class InMemoryMemory[source]¶
基类:
MemoryBase用于存储消息的内存中记忆类。
- load_state_dict(state_dict, strict=True)[source]¶
从 JSON 数据加载记忆。
- Parameters:
state_dict (dict) – 要加载的状态字典,应该包含一个 "content" 字段。
strict (bool, 默认为 True) – 如果为 True,当模块中的任何键在state_dict中未找到时会抛出错误。如果为 False,则跳过缺失的键。
- Return type:
无
- async delete(index)[source]¶
按索引删除指定项。
- Parameters:
索引 (Union[Iterable, int]) – 要删除的索引。
- Return type:
无
- class LongTermMemoryBase[source]¶
基础类:
StateModule长期记忆基类,应当是一个时间序列记忆管理系统。
record_to_memory 和 retrieve_from_memory 方法是智能体用于自主管理长期记忆的两个工具函数。您可以选择不实现这两个函数。
record 和 retrieve 方法是供开发人员使用的。例如, 在每次回复开始时检索/记录记忆,并将检索到的记忆添加到系统提示中。
- async record(msgs, **kwargs)[source]¶
开发者设计的用于将给定输入消息中的信息记录到长期记忆的方法。
- Parameters:
msgs (list[Msg | None])
kwargs (任何)
- Return type:
无
- class Mem0LongTermMemory[source]¶
-
一个使用mem0实现LongTermMemoryBase接口的类。
- __init__(agent_name=None, user_name=None, run_name=None, model=None, embedding_model=None, vector_store_config=None, mem0_config=None, default_memory_type=None, **kwargs)[source]¶
初始化 Mem0LongTermMemory 实例
- Parameters:
agent_name (str | None, optional) – 智能体的名称。默认为 None。
user_name (str | None, optional) – 用户名称。默认为 None。
run_name (str | None, optional) – 运行/会话的名称。默认为 None。
model (ChatModelBase | None)
embedding_model (EmbeddingModelBase | None)
vector_store_config (Any | None)
mem0_config (Any | None)
default_memory_type (str | None)
kwargs (任何)
- Return type:
无
注意
agent_name、user_name 或 run_name 中至少需要提供一个。
在记忆记录过程中,这些参数会作为存储记忆的元数据。
在记忆检索过程中,只有具有匹配元数据值的记忆才会被返回。
- model (ChatModelBase | None, optional):
用于长期记忆的聊天模型。若提供了mem0_config,则此设置将覆盖LLM的配置。如果mem0_config为None,则此项必填。
- embedding_model (EmbeddingModelBase | None, optional):
用于长期记忆的嵌入模型。如果提供了mem0_config,这将覆盖嵌入器的配置。如果mem0_config为None,则此项为必需。
- vector_store_config (VectorStoreConfig | None, optional):
用于长期记忆的向量存储配置。 如果提供了mem0_config,它将覆盖向量存储配置。 如果mem0_config为None且未提供此配置,则默认为Qdrant且on_disk=True。
- mem0_config (内存配置 | 无, optional):
要用于长期记忆的mem0配置。 如果提供了该配置,单独的 模型/嵌入模型/向量存储配置参数将 覆盖mem0_config中对应的配置。如果 为None,则将使用提供的参数创建新的MemoryConfig。
- default_memory_type (字符串 | 无, optional):
要使用的记忆类型。默认为None,以创建一个语义记忆。
- Raises:
值错误 - 如果mem0_config为None且model或 embedding_model为None。
- Parameters:
agent_name (str | None)
user_name (str | None)
run_name (str | None)
model (ChatModelBase | None)
embedding_model (EmbeddingModelBase | None)
vector_store_config (Any | None)
mem0_config (任意 | 空)
default_memory_type (str | None)
kwargs (任何)
- Return type:
无
- async record_to_memory(thinking, content, **kwargs)[source]¶
使用该功能记录您可能稍后需要的重要信息。目标内容应具体且简洁,例如包含谁、何时、何地、做什么、为什么、如何等要素。
- Parameters:
thinking (str) – 您关于记录内容的思考与推理。
content (list[str]) – 需要记忆的内容,这是一个字符串列表。
kwargs (任何)
- Return type:
- async retrieve_from_memory(keywords, limit=5, **kwargs)[source]¶
根据给定关键词检索记忆。
- Parameters:
keywords (list[str]) – 在记忆中搜索的关键词,这些关键词应具体且简洁,例如人物姓名、日期、地点等。
limit (int, optional) – 每次搜索要检索的最大记忆数量。
kwargs (任何)
- Returns:
一个包含检索到的记忆作为JSON文本的ToolResponse。
- Return type:
- ToolResponse
- async record(msgs, memory_type=None, infer=True, **kwargs)[source]¶
将内容记录到长期记忆中。
- Parameters:
msgs (list[Msg | None]) – 需要记录到内存的消息。
memory_type (str | None, 可选项) – 要使用的记忆类型。默认为None,以创建一个语义记忆。“procedural_memory”明确用于过程记忆。
infer (bool, optional) – 是否从内容推断内存。默认为 True。
**kwargs (Any) – 用于mem0记录的其他关键字参数。
- Return type:
无