update memory_deduplication.py

This commit is contained in:
caozouying.czy 2026-02-11 16:28:23 +08:00
parent 7c397603fa
commit 8765d0273d
4 changed files with 71 additions and 10 deletions

View file

@ -1,10 +1,35 @@
"""Procedural memory retriever agent implementation."""
from ..base_memory_agent import BaseMemoryAgent
from ....core.enumeration import MemoryType
from ....core.enumeration import Role, MemoryType
from ....core.schema import Message
from ....core.utils import format_messages
class ProceduralRetriever(BaseMemoryAgent):
"""Agent responsible for retrieving procedural memories."""
memory_type: MemoryType = MemoryType.PROCEDURAL
async def build_messages(self) -> list[Message]:
"""Build messages with system prompt and user message."""
if self.context.get("query"):
context = self.context.query
elif self.context.get("messages"):
context = format_messages(self.context.messages)
else:
raise ValueError("input must have either `query` or `messages`")
return [
Message(
role=Role.SYSTEM,
content=self.prompt_format(
prompt_name="system_prompt",
meta_memory_info=await self._read_meta_memories(),
context=context,
)
),
Message(
role=Role.USER,
content=self.get_prompt("user_message")
),
]

View file

@ -1,10 +1,46 @@
"""Procedural memory summarizer agent implementation."""
from ..base_memory_agent import BaseMemoryAgent
from ....core.enumeration import MemoryType
from ....core.enumeration import Role, MemoryType
from ....core.schema import Message
from ....core.utils import format_messages
class ProceduralSummarizer(BaseMemoryAgent):
"""Agent responsible for summarizing procedural memories."""
memory_type: MemoryType = MemoryType.PROCEDURAL
async def build_messages(self) -> list[Message]:
return [
Message(
role=Role.SYSTEM,
content=self.prompt_format(
prompt_name="system_prompt",
context=self.description + "\n" + format_messages(self.get_messages()),
outcome="successful task completion" if self.success else "task failure",
memory_type=self.memory_type.value,
memory_target=self.memory_target,
)
),
Message(
role=Role.USER,
content=self.get_prompt("user_message")
),
]
async def _reasoning_step(self, messages: list[Message], step: int, **kwargs) -> tuple[Message, bool]:
return await super()._reasoning_step(messages, step, **kwargs)
async def _acting_step(self, assistant_message: Message, step: int, **kwargs) -> list[Message]:
"""Execute tool calls with memory_target, memory_type, and author context."""
messages: list[Message] = await super()._acting_step(
assistant_message,
step,
memory_type=self.memory_type.value,
memory_target=self.memory_target,
ref_memory_id=self.ref_memory_id,
author=self.author,
**kwargs,
)
return messages

View file

@ -134,7 +134,7 @@ class RewriteMemory(BaseOp):
for i, memory in enumerate(memories, 1):
condition = memory.when_to_use
memory_content = memory.content
memory_text = f"Experience {i} :\n When to use: {condition}\n Content: {memory_content}\n"
memory_text = f"Memory {i} :\n When to use: {condition}\n Content: {memory_content}\n"
formatted_memories.append(memory_text)

View file

@ -68,7 +68,7 @@ class MemoryDeduplication(BaseOp):
continue
# Check similarity with current batch task memories
if self._is_similar_to_current_task_memories(current_embedding, unique_task_memories, similarity_threshold):
if await self._is_similar_to_current_task_memories(current_embedding, unique_task_memories, similarity_threshold):
logger.debug(f"Skipping duplicate in current batch: {str(task_memory.when_to_use)[:50]}...")
continue
@ -105,13 +105,13 @@ class MemoryDeduplication(BaseOp):
logger.warning(f"Failed to retrieve existing task memory embeddings: {e}")
return []
def _get_task_memory_embedding(self, task_memory: MemoryNode) -> List[float] | None:
async def _get_task_memory_embedding(self, task_memory: MemoryNode) -> List[float] | None:
"""Generate embedding for task memory"""
try:
# Combine task memory description and content for embedding
text_for_embedding = f"{task_memory.when_to_use} {task_memory.content}"
embeddings = self.vector_store.embedding_model.get_embeddings([text_for_embedding])
embeddings = await self.vector_store.get_embeddings([text_for_embedding])
if embeddings and len(embeddings) > 0:
return embeddings[0]
@ -137,7 +137,7 @@ class MemoryDeduplication(BaseOp):
return True
return False
def _is_similar_to_current_task_memories(
async def _is_similar_to_current_task_memories(
self,
current_embedding: List[float],
current_task_memories: List[MemoryNode],
@ -145,7 +145,7 @@ class MemoryDeduplication(BaseOp):
) -> bool:
"""Check if current embedding is similar to other memories in current batch."""
for existing_task_memory in current_task_memories:
existing_embedding = self._get_task_memory_embedding(existing_task_memory)
existing_embedding = await self._get_task_memory_embedding(existing_task_memory)
if existing_embedding is None:
continue