refactor(memory): update memory management and retrieval implementation

This commit is contained in:
jinli.yl 2026-01-27 01:26:17 +08:00
parent e63a3fa632
commit c21b69da11
7 changed files with 23 additions and 22 deletions

View file

@ -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."

View file

@ -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 [

View file

@ -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,
}

View file

@ -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"""

View file

@ -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 = []

View file

@ -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,
)

View file

@ -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)