fix(memory): handle varied memory formats in prompt formatting
Some checks failed
Build and Push Docker / build-and-push (push) Has been cancelled
Some checks failed
Build and Push Docker / build-and-push (push) Has been cancelled
This commit is contained in:
@ -28,7 +28,7 @@ Please respond to the user's query: {query}
|
|||||||
|
|
||||||
In your response, consider the memories above to provide a personalized answer."""
|
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."""
|
"""Search for relevant memories about the user."""
|
||||||
try:
|
try:
|
||||||
results = self.memory.search(
|
results = self.memory.search(
|
||||||
@ -36,6 +36,11 @@ In your response, consider the memories above to provide a personalized answer."
|
|||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
limit=limit
|
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
|
return results
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[ERROR] Failed to search memories: {e}")
|
print(f"[ERROR] Failed to search memories: {e}")
|
||||||
@ -54,13 +59,15 @@ In your response, consider the memories above to provide a personalized answer."
|
|||||||
print(f"[ERROR] Failed to add memory: {e}")
|
print(f"[ERROR] Failed to add memory: {e}")
|
||||||
return {}
|
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."""
|
"""Format memories into a string for the prompt."""
|
||||||
if not memories:
|
if not memories:
|
||||||
return "No previous memories about this user."
|
return "No previous memories about this user."
|
||||||
|
|
||||||
formatted = []
|
formatted = []
|
||||||
for i, memory in enumerate(memories, 1):
|
for i, memory in enumerate(memories, 1):
|
||||||
|
# Handle both string and dict formats
|
||||||
|
if isinstance(memory, dict):
|
||||||
memory_text = memory.get("memory", "")
|
memory_text = memory.get("memory", "")
|
||||||
created_at = memory.get("created_at", "")
|
created_at = memory.get("created_at", "")
|
||||||
if created_at:
|
if created_at:
|
||||||
@ -73,6 +80,8 @@ In your response, consider the memories above to provide a personalized answer."
|
|||||||
formatted.append(f"{i}. {memory_text} (remembered on: {created_str})")
|
formatted.append(f"{i}. {memory_text} (remembered on: {created_str})")
|
||||||
else:
|
else:
|
||||||
formatted.append(f"{i}. {memory_text}")
|
formatted.append(f"{i}. {memory_text}")
|
||||||
|
elif isinstance(memory, str):
|
||||||
|
formatted.append(f"{i}. {memory}")
|
||||||
|
|
||||||
return "\n".join(formatted)
|
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."""
|
"""Get all memories for a user."""
|
||||||
try:
|
try:
|
||||||
memories = self.memory.get_all(user_id=user_id)
|
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
|
return memories
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[ERROR] Failed to get all memories: {e}")
|
print(f"[ERROR] Failed to get all memories: {e}")
|
||||||
|
Reference in New Issue
Block a user