15-04 AgentScope Java特性:长期记忆
✅AgentScope Java特性:长期记忆
(虽然在ASJ的2.0的relesse note中提到:RAG (Knowledge / KnowledgeRetrievalTools / RAGMode) and long-term memory modules deprecated — being rewritten on the v2 architecture; don’t depend on them in new code`,但是截止目前,2.0还没正式发布,也没给出替代方案,我们先讲1.0的方案和用法,后续新版本更新了我能再单独讲方案和用法)
✅什么是长期记忆
前面我们讲过,Agent的记忆能力必不可少。Agent 的“记忆”是指其在与环境或用户交互过程中,用于存储、检索和利用信息的机制。这些信息可以包括: 没有记忆的 Agent 只能处理孤立的请求,无法实现上下文感知、个性化服务或持续改进。
LLMentor
前面讲过了长期记忆,也介绍过如何在spring ai alibaba中接入mem0做长期记忆,agent scope也是支持长期记忆的。
LongTermMemory
ASJ(AgentScope Java,后面都用ASJ缩写)中,提供了一个接口LongTermMemory来定义对长期记忆的支持:
1 | public interface LongTermMemory { |
record 接收的是消息列表,由实现方(如 Mem0)使用 LLM 自动从对话中提取有价值的事实。retrieve 返回的是拼接好的文本,框架直接注入到模型上下文。
这个接口在ASJ中有多种实现:

分别是BailianLongTermMemory、Mem0LongTermMemory和ReMeLongTermMemory。
1 | ReActAgent agent = ReActAgent.builder() .name("Assistant") .model(xxx) .longTermMemory(longTermMemory); |
BailianLongTermMemory
BailianLongTermMemory是通过对接阿里云百炼平台的记忆服务(https://help.aliyun.com/zh/model-studio/memory-library )来实现长期记忆的。
在百炼中专门提供了一个【记忆库】能力,开发者可以在上面创建一个记忆库来做长期记忆的存储。

1 | BailianLongTermMemory.builder() .apiKey(System.getenv("DASHSCOPE_API_KEY")) // 阿里云 DashScope Key .userId("user_001") // 必填 .memoryLibraryId("lib_xxx") // 记忆库 ID .projectId("proj_xxx") // 项目 ID .profileSchema("schema_xxx") // 用户画像 schema .topK(10) .minScore(0.5) // 提高阈值,只要高相关结果 .enableRerank(true) // 启用重排序 .enableJudge(true) // 启用 LLM 判断 .enableRewrite(true) // 启用查询改写 .metadata(Map.of("source", "mobile-app")) .build(); |
Mem0LongTermMemory
通过对接我们前面介绍过的mem0框架做长期记忆。在构造Mem0LongTermMemory的时候,指定apiBaseUrl、API_KEY(如果使用百炼的托管服务才需要)即可,
1 | Mem0LongTermMemory.builder() .agentName("Assistant") // 内部映射为 agentId(可选) .userId("user_123") // 用户隔离(核心维度) .runId("session_456") // 单次会话隔离(可选) .apiBaseUrl("https://api.mem0.ai") // PLATFORM默认地址 .apiKey(System.getenv("MEM0_API_KEY")) // 平台模式必填 .apiType(Mem0ApiType.PLATFORM) // 或 SELF_HOSTED .metadata(Map.of("category", "travel", "lang", "zh")) .build(); |
ReMeLongTermMemory
这种实现,是对接ReMe这个长期记忆框架的,ReMe是agentscope团队开发的一个长期记忆框架,对标的就是mem0。
1 | ReMeLongTermMemory.builder() .userId("task_workspace") // 必填,映射为 workspaceId .apiBaseUrl("http://localhost:8002")// ReMe服务地址 .timeout(Duration.ofSeconds(120)) // 可选,HTTP 超时 .build(); |
LongTermMemoryMode
1 | ReActAgent agent = ReActAgent.builder() .name("Assistant") .model(xxx) .longTermMemory(longTermMemory).longTermMemoryMode(LongTermMemoryMode.STATIC_CONTROL) .build(); |
ASJ中的长期记忆支持三种工作模型:
1 | public enum LongTermMemoryMode { |
AGENT_CONTROL模式,由 LongTermMemoryTools 实现,框架向 Toolkit 注册两个工具:
recordToMemory(thinking, content) — Agent 主动调用记录关键信息
retrieveFromMemory(keywords) — Agent 主动按关键词检索
Agent 通过 Function Calling 决定何时记忆/回忆。适合需要精细控制的场景,比如只在用户明确说”记住XXX”时才存。
STATIC_CONTROL 模式, StaticLongTermMemoryHook 实现,关键流程:
PreCallEvent(推理前):取出最后一条用户消息 → 调用 retrieve() → 用 <long_term_memory>…</long_term_memory> 标签包装结果 → 作为 USER role 消息(name=”long_term_memory”)追加到输入消息末尾
PostCallEvent(回复后):把 Memory 中所有消息传给 record() → 由后端(如 Mem0)自动提取关键信息并向量化存储
异步记录:可选 asyncRecord=true,使用专用的 boundedElastic scheduler(1 worker,队列 3),避免阻塞响应。
BOTH 模式,同时启用上述两种机制:框架自动后台记录全量对话 + Agent 在需要时主动召回。这是推荐的默认模式。
基于Mem0实现STATIC_CONTROL 模式记忆
1 |
|
基于Mem0实现AGENT_CONTROL 模式记忆
1 | import io.agentscope.core.ReActAgent; |
STATIC_CONTROL和AGENT_CONTROL对比
| Column 1 | Column 2 | Column 3 |
|---|---|---|
| 维度 | STATIC_CONTROL | AGENT_CONTROL |
| 触发时机 | 每轮对话自动触发 | 由 LLM 决定调用工具 |
| 控制粒度 | 全部对话内容 | 精确到具体事实 |
| Token 消耗 | 较高(每轮都检索) | 较低(按需检索) |
| 模型依赖 | 不依赖工具调用能力 | 需要支持 Function Calling |
| 适合场景 | 简单 Agent、信息密集 | 智能 Agent、信息稀疏 |
| 召回率 | 高(只要语义相关就召回) | 中(依赖 LLM 判断) |
| 精确率 | 中(可能召回无关内容) | 高(LLM 主动选择) |
自定义实现 LongTermMemory
如果不想依赖 内置的长期记忆方案,可以自己实现,只需要实现LongTermMemory就行
1 | import io.agentscope.core.memory.LongTermMemory; |
