From c21b69da11b937d0cc43f9ffdd7e8d57aa964595 Mon Sep 17 00:00:00 2001 From: "jinli.yl" Date: Tue, 27 Jan 2026 01:26:17 +0800 Subject: [PATCH] refactor(memory): update memory management and retrieval implementation --- .../memory/default/personal_retriever.yaml | 4 +-- .../memory/default/personal_summarizer.py | 6 +---- reme/agent/memory/default/reme_retriever.py | 2 +- reme/reme.py | 27 +++++++++++-------- .../memory/user_profile/read_user_profile.py | 3 ++- reme/tool/memory/vector/add_memory.py | 1 + reme/tool/memory/vector/retrieve_memory.py | 2 +- 7 files changed, 23 insertions(+), 22 deletions(-) diff --git a/reme/agent/memory/default/personal_retriever.yaml b/reme/agent/memory/default/personal_retriever.yaml index 7b81e4cc..9a882c48 100644 --- a/reme/agent/memory/default/personal_retriever.yaml +++ b/reme/agent/memory/default/personal_retriever.yaml @@ -37,9 +37,7 @@ system_prompt: | ## Output Format When answering, structure your response as follows: - - - [timestamp][Relevant memory content from search results] - - [timestamp][Relevant user profile information] + - [timestamp][Relevant history/memory/profile from context] If no relevant information found after thorough search (5+ queries), state: "No relevant information found after thorough search using multiple query strategies." diff --git a/reme/agent/memory/default/personal_summarizer.py b/reme/agent/memory/default/personal_summarizer.py index eafbfa05..d6ac1c41 100644 --- a/reme/agent/memory/default/personal_summarizer.py +++ b/reme/agent/memory/default/personal_summarizer.py @@ -5,7 +5,7 @@ from loguru import logger from ..base_memory_agent import BaseMemoryAgent from ....core.enumeration import Role, MemoryType from ....core.op import BaseTool -from ....core.schema import Message, MemoryNode +from ....core.schema import Message class PersonalSummarizer(BaseMemoryAgent): @@ -13,10 +13,6 @@ class PersonalSummarizer(BaseMemoryAgent): memory_type: MemoryType = MemoryType.PERSONAL - def __init__(self, **kwargs): - super().__init__(**kwargs) - self.retrieved_nodes: list[MemoryNode] = [] - async def _build_phase1_messages(self) -> list[Message]: """Build messages for phase 1: retrieve and add memory.""" return [ diff --git a/reme/agent/memory/default/reme_retriever.py b/reme/agent/memory/default/reme_retriever.py index 88375ab7..659bf08d 100644 --- a/reme/agent/memory/default/reme_retriever.py +++ b/reme/agent/memory/default/reme_retriever.py @@ -78,7 +78,7 @@ class ReMeRetriever(BaseMemoryAgent): return { "answer": "\n".join(answer), "success": True, - "messages": self.messages, + "messages": messages, "tools": tools, "retrieved_nodes": retrieved_nodes, } diff --git a/reme/reme.py b/reme/reme.py index 76154919..f611792d 100644 --- a/reme/reme.py +++ b/reme/reme.py @@ -199,7 +199,6 @@ class ReMe: ] if version == "default": - reme_retriever = ReMeRetriever( meta_memories=meta_memories, tools=[ @@ -342,17 +341,18 @@ class ReMe: """Retrieve all memories from the vector store.""" return [node.to_memory_node() for node in await self.vector_store.list()] - @staticmethod - async def get_profiles(user_name: str | list[str]) -> str | list[str]: + async def get_profiles(self, user_name: str | list[str]) -> str | list[str]: """Retrieve user profile(s) from the system for the specified user(s).""" read_profile = ReadUserProfile(show_id="profile") if isinstance(user_name, str): - return await read_profile.call(memory_target=user_name) + return await read_profile.call(memory_target=user_name, service_context=self.service_context) else: - return [await read_profile.call(memory_target=name) for name in user_name] + return [ + await read_profile.call(memory_target=name, service_context=self.service_context) for name in user_name + ] - @staticmethod async def add_profile( + self, profile_key: str, profile_value: str, user_name: str, @@ -373,11 +373,12 @@ class ReMe: }, ], memory_target=user_name, + service_context=self.service_context, ) return update_user_profile.memory_nodes[0] - @staticmethod async def update_profile( + self, profile_id: str, profile_key: str, profile_value: str, @@ -399,11 +400,11 @@ class ReMe: }, ], memory_target=user_name, + service_context=self.service_context, ) return update_user_profile.memory_nodes[0] - @staticmethod - async def delete_all_profiles(user_name: str | list[str]): + async def delete_all_profiles(self, user_name: str | list[str]): """Delete all user profiles from ReMe system.""" if isinstance(user_name, str): user_name = [user_name] @@ -411,9 +412,13 @@ class ReMe: read_profile = ReadUserProfile(show_id="profile") update_profile = UpdateUserProfile() for memory_target in user_name: - await read_profile.call(memory_target=memory_target) + await read_profile.call(memory_target=memory_target, service_context=self.service_context) profile_ids = [profile.memory_id for profile in read_profile.memory_nodes] - await update_profile.call(profile_ids_to_delete=profile_ids, memory_target=memory_target) + await update_profile.call( + profile_ids_to_delete=profile_ids, + memory_target=memory_target, + service_context=self.service_context, + ) async def context_offload(self): """working memory summary""" diff --git a/reme/tool/memory/user_profile/read_user_profile.py b/reme/tool/memory/user_profile/read_user_profile.py index 5ad5db57..637b2070 100644 --- a/reme/tool/memory/user_profile/read_user_profile.py +++ b/reme/tool/memory/user_profile/read_user_profile.py @@ -38,7 +38,8 @@ class ReadUserProfile(BaseMemoryTool): return "" nodes = [MemoryNode(**data) for data in cached_data] - self.memory_nodes = nodes + self.memory_nodes.clear() + self.memory_nodes.extend(nodes) nodes.sort(key=lambda n: n.metadata.get("update_time", "")) formatted_profiles = [] diff --git a/reme/tool/memory/vector/add_memory.py b/reme/tool/memory/vector/add_memory.py index d59742d6..2f608f33 100644 --- a/reme/tool/memory/vector/add_memory.py +++ b/reme/tool/memory/vector/add_memory.py @@ -79,6 +79,7 @@ class AddMemory(BaseMemoryTool): memory_target=self.memory_target, content=memory_content, author=self.author, + ref_memory_id=self.history_node.memory_id, metadata=metadata, ) diff --git a/reme/tool/memory/vector/retrieve_memory.py b/reme/tool/memory/vector/retrieve_memory.py index 9d3e42db..7c95b538 100644 --- a/reme/tool/memory/vector/retrieve_memory.py +++ b/reme/tool/memory/vector/retrieve_memory.py @@ -128,7 +128,7 @@ class RetrieveMemory(BaseMemoryTool): line += f"conversation_time={node.metadata['conversation_time']} " line += node.content.strip() + " " if node.ref_memory_id: - line += f"history_id={node.ref_memory_id} " + line += f"history_id={node.ref_memory_id}" outputs.append(line.strip()) output = "\n".join(outputs)