核心内容摘要
InstructPix2Pix进阶实战:打造个人AI修图工作流与创意自动化指南
DeepWiki QA with Code Context for Repository: agentscope-ai/agentscopeQ1memoryInMemoryMemory(),long_term_memorylong_term_memory, 介绍一下这两个Answer简要说明memoryInMemoryMemory()这是短期记忆用于在单次对话中存储和管理消息历史支持标记mark过滤、压缩摘要等操作默认使用内存中的列表存储。
agentscope:
agentscope:
long_term_memorylong_term_memory这是长期记忆用于跨会话的语义化记忆存储与检索通常基于向量数据库如 Mem0/ReMe支持两种控制模式agent_control/static_control/both。
agentscope:
agentscope:
短期记忆InMemoryMemory定义与职责存储当前对话的消息对象Msg提供增删改查与按标记过滤的能力。
agentscope:
支持压缩摘要update_compressed_summary以在上下文窗口接近上限时压缩旧消息。
agentscope:
实现类InMemoryMemory基于内存列表的简单实现适合开发与单进程测试。
agentscope:
其他实现AsyncSQLAlchemyMemory关系型数据库、RedisMemoryRedis支持多租户与 TTL。
agentscope:
使用示例memoryInMemoryMemory()awaitmemory.add(Msg(user,我喜欢住民宿,user))awaitmemory.add(Msg(system,提示先收集信息,system),markshint)msgsawaitmemory.get_memory(markhint)awaitmemory.delete_by_mark(hint)agentscope:
---## 长期记忆LongTermMemoryBase### 定义与职责-跨会话、语义化的记忆存储与检索通常基于向量检索如 Mem0/ReMe。
agentscope:
-提供两种控制模式-static_control框架在每次 reply 开始时自动检索、结束时自动记录。
agentscope:
-agent_control将 retrieve_from_memory 与 record_to_memory 注册为工具由 Agent 自主调用。
agentscope:
-both同时启用上述两种方式。
agentscope:
### 实现类-Mem0LongTermMemory基于 mem0 库的向量记忆实现。
agentscope:
-ReMePersonalLongTermMemory基于 ReMe 的个人长期记忆实现提供工具接口与直接接口。
agentscope:
### 使用示例Mem0python long_term_memoryMem0LongTermMemory(agent_nameFriday,user_nameuser_123,modelDashScopeChatModel(...),embedding_modelDashScopeTextEmbedding(...),)awaitlong_term_memory.record([Msg(user,我喜欢住民宿,user)])resultsawaitlong_term_memory.retrieve([Msg(user,我的住宿偏好,user)])agentscope:
### 与 ReActAgent 集成python agentReActAgent(nameFriday,memoryInMemoryMemory(),long_term_memorylong_term_memory,long_term_memory_modestatic_control,# 或 agent_control/both...)agentscope:
---## 对比与使用建议|维度|短期记忆memory|长期记忆long_term_memory||------|-------------------|------------------------------||用途|单次对话的消息历史与提示管理|跨会话的语义化记忆存储与检索||范围|会话内|跨会话、用户级||实现类|InMemoryMemory、AsyncSQLAlchemyMemory、RedisMemory|Mem0LongTermMemory、ReMePersonalLongTermMemory 等||控制方式|直接调用 memory.add/get_memory/delete_by_mark 等|static_control自动、agent_control工具、both||典型场景|对话上下文、压缩摘要、标记过滤|用户偏好、长期知识、个性化记忆|-开发/测试使用 InMemoryMemory 作为短期记忆。
agentscope:
-生产多租户短期可用 RedisMemory长期用 Mem0LongTermMemory 或 ReMePersonalLongTermMemory。
agentscope:
-需要跨会话记忆配置 long_term_memory 并选择合适的 long_term_memory_mode。
agentscope:
---## Notes-短期记忆的压缩功能由 ReActAgent 的 CompressionConfig 控制不在 MemoryBase 中实现。
agentscope:
-长期记忆的两种控制模式可以共存both此时既会自动检索/记录也会注册工具供 Agent 调用。
agentscope:
-示例代码来自中文教程英文教程在 docs/tutorial/en/src/task_long_term_memory.py 也有对应内容。
agentscope:
Wiki pages you might want to explore:-[Memory Systems(agentscope-ai/agentscope)](/wiki/agentscope-ai/agentscope#
2.
### Citations**File:**src/agentscope/agent/_react_agent.py(L216-
python memory(MemoryBase|None,optional):The memory used to store the dialogue history.Ifnotprovided,a default InMemoryMemory will be created,which stores messagesinalistinmemory.File:src/agentscope/agent/_react_agent.py (L220-
long_term_memory(LongTermMemoryBase|None,optional):The optionallong-term memory,which will provide two tool functions:retrieve_from_memoryandrecord_to_memory,andwill attach the retrieved information to the system prompt before each reply.enable_meta_tool(bool,defaults to False):If True,a meta tool function reset_equipped_tools will be added to the toolkit,which allows the agent to manage its equipped tools dynamically.long_term_memory_mode(Literal[agent_control,static_control,\both],defaults to both):The mode of thelong-term memory.If agent_control,two tool functions retrieve_from_memoryandrecord_to_memory will be registeredinthe toolkit to allow the agent to manage thelong-term memory.If static_control,retrievingandrecording will happeninthe beginningandend of each reply respectively.parallel_tool_calls(bool,defaults to False):File:src/agentscope/agent/_react_agent.py (L289-
self._static_controllong_term_memoryandlong_term_memory_modein[static_control,both,]self._agent_controllong_term_memoryandlong_term_memory_modein[agent_control,both,]# -------------- Tool management --------------# If None, a default Toolkit will be createdself.toolkittoolkitorToolkit()ifself._agent_control:# Adding two tool functions into the toolkit to allow self-controlself.toolkit.register_tool_function(long_term_memory.record_to_memory,)self.toolkit.register_tool_function(long_term_memory.retrieve_from_memory,)File:src/agentscope/agent/_react_agent.py (L528-
ifself._static_control:awaitself.long_term_memory.record([*awaitself.memory.get_memory(exclude_mark_MemoryMark.COMPRESSED,),],)File:docs/tutorial/zh_CN/src/task_memory.py (L10-
-存储消息对象Msg-利用标记mark管理消息**标记**是与记忆中每条消息关联的字符串标签可用于根据消息的上下文或目的对消息进行分类、过滤和检索。
可用于实现进阶的记忆管理功能例如在 ReActAgent 类中使用hint标签标记一次性的提示消息 以便在使用完成后将其从记忆中删除。
File:docs/tutorial/zh_CN/src/task_memory.py (L17-
..note::AgentScope 中的记忆模块仅提供消息存储和管理的原子功能记忆压缩等算法逻辑在 智能体agent_ 中实现。
File:docs/tutorial/zh_CN/src/task_memory.py (L26-
*-InMemoryMemory-简单的内存记忆存储实现。
File:docs/tutorial/zh_CN/src/task_memory.py (L28-
*-AsyncSQLAlchemyMemory-基于异步 SQLAlchemy 的记忆存储实现支持如 SQLite、PostgreSQL、MySQL 等多种关系数据库。
*-RedisMemory-基于 Redis 的记忆存储实现。
File:docs/tutorial/zh_CN/src/task_memory.py (L66-
*-update_compressed_summary(summary:str,)-None-更新存储在记忆中的摘要属性。
File:docs/tutorial/zh_CN/src/task_memory.py (L94-
asyncdefin_memory_example():使用InMemoryMemory在内存中存储消息的示例。
memoryInMemoryMemory()awaitmemory.add(Msg(Alice,生成一份关于AgentScope的报告,user),)# 添加一条带有标记hint的提示消息awaitmemory.add([Msg(system,system-hint首先创建一个计划来收集信息然后逐步生成报告。
/system-hint,system,),],markshint,)msgsawaitmemory.get_memory(markhint)print(带有标记hint的消息)formsginmsgs:print(f-{msg})# 所有存储的消息都可以通过 state_dict 和 load_state_dict 方法导出和加载。
statememory.state_dict()print(记忆的状态字典)print(json.dumps(state,indent2,ensure_asciiFalse))# 通过标记删除消息deleted_countawaitmemory.delete_by_mark(hint)print(f删除了{deleted_count}条带有标记hint的消息。
)File:docs/tutorial/zh_CN/src/task_long_term_memory.py (L8-
AgentScope 为长期记忆提供了一个基类 LongTermMemoryBase 和一个基于 mem0https://github.com/mem0ai/mem0_ 的具体实现 Mem0LongTermMemory。
结合:ref:agent 章节中 ReActAgent 类的设计我们提供了两种长期记忆模式-agent_control智能体通过工具调用自主管理长期记忆。
-static_control开发者通过编程显式控制长期记忆操作。
当然开发者也可以使用 both 参数将同时激活上述两种记忆管理模式。
File:docs/tutorial/zh_CN/src/task_long_term_memory.py (L37-
# 创建 mem0 长期记忆实例long_term_memoryMem0LongTermMemory(agent_nameFriday,user_nameuser_123,modelDashScopeChatModel(model_nameqwen-max-latest,api_keyos.environ.get(DASHSCOPE_API_KEY),streamFalse,),embedding_modelDashScopeTextEmbedding(model_nametext-embedding-v2,api_keyos.environ.get(DASHSCOPE_API_KEY),),on_diskFalse,)# %%# Mem0LongTermMemory 类提供了两个操作长期记忆的方法record 和 retrieve。
# 它们接收消息对象的列表作为输入分别记录和检索长期记忆中的信息。
## 例如下面的例子中我们先存入用户的一条偏好然后在长期记忆中检索相关信息。
## 基本使用示例asyncdefbasic_usage():基本使用示例# 记录记忆awaitlong_term_memory.record([Msg(user,我喜欢住民宿,user)])# 检索记忆resultsawaitlong_term_memory.retrieve([Msg(user,我的住宿偏好,user)],)print(f检索结果:{results})File:docs/tutorial/zh_CN/src/task_long_term_memory.py (L91-
agentReActAgent(nameFriday,sys_prompt你是一个具有长期记忆功能的助手。
,modelDashScopeChatModel(api_keyos.environ.get(DASHSCOPE_API_KEY),model_nameqwen-max-latest,),formatterDashScopeChatFormatter(),toolkitToolkit(),memoryInMemoryMemory(),long_term_memorylong_term_memory,long_term_memory_modestatic_control,# 使用 static_control 模式)File:docs/tutorial/zh_CN/src/task_long_term_memory.py (L172-