fix(memory): handle varied memory formats in prompt formatting
Some checks failed
Build and Push Docker / build-and-push (push) Has been cancelled

This commit is contained in:
gameloader
2025-10-12 16:53:01 +08:00
parent 618c2ec209
commit a7f8e6f775

View File

@ -28,7 +28,7 @@ Please respond to the user's query: {query}
In your response, consider the memories above to provide a personalized answer."""
def search_memories(self, query: str, user_id: str, limit: int = 5) -> List[Dict[str, Any]]:
def search_memories(self, query: str, user_id: str, limit: int = 5) -> List[Any]:
"""Search for relevant memories about the user."""
try:
results = self.memory.search(
@ -36,6 +36,11 @@ In your response, consider the memories above to provide a personalized answer."
user_id=user_id,
limit=limit
)
# Debug: Print the actual format of results
print(f"[DEBUG] Search results type: {type(results)}")
if results:
print(f"[DEBUG] First result type: {type(results[0])}")
print(f"[DEBUG] First result: {results[0]}")
return results
except Exception as e:
print(f"[ERROR] Failed to search memories: {e}")
@ -54,25 +59,29 @@ In your response, consider the memories above to provide a personalized answer."
print(f"[ERROR] Failed to add memory: {e}")
return {}
def format_memories_for_prompt(self, memories: List[Dict[str, Any]]) -> str:
def format_memories_for_prompt(self, memories: List[Any]) -> str:
"""Format memories into a string for the prompt."""
if not memories:
return "No previous memories about this user."
formatted = []
for i, memory in enumerate(memories, 1):
memory_text = memory.get("memory", "")
created_at = memory.get("created_at", "")
if created_at:
try:
# Format the date if it's available
created_date = datetime.fromisoformat(created_at.replace('Z', '+00:00'))
created_str = created_date.strftime("%Y-%m-%d %H:%M")
except:
created_str = created_at
formatted.append(f"{i}. {memory_text} (remembered on: {created_str})")
else:
formatted.append(f"{i}. {memory_text}")
# Handle both string and dict formats
if isinstance(memory, dict):
memory_text = memory.get("memory", "")
created_at = memory.get("created_at", "")
if created_at:
try:
# Format the date if it's available
created_date = datetime.fromisoformat(created_at.replace('Z', '+00:00'))
created_str = created_date.strftime("%Y-%m-%d %H:%M")
except:
created_str = created_at
formatted.append(f"{i}. {memory_text} (remembered on: {created_str})")
else:
formatted.append(f"{i}. {memory_text}")
elif isinstance(memory, str):
formatted.append(f"{i}. {memory}")
return "\n".join(formatted)
@ -134,6 +143,11 @@ In your response, consider the memories above to provide a personalized answer."
"""Get all memories for a user."""
try:
memories = self.memory.get_all(user_id=user_id)
# Debug: Print the actual format of memories
print(f"[DEBUG] All memories type: {type(memories)}")
if memories:
print(f"[DEBUG] First memory type: {type(memories[0])}")
print(f"[DEBUG] First memory: {memories[0]}")
return memories
except Exception as e:
print(f"[ERROR] Failed to get all memories: {e}")