mem0ai / mem0

The Memory layer for your AI apps
https://mem0.ai
Apache License 2.0
22.99k stars 2.12k forks source link

Graph Database Entities Not Utilized in Recall within #1991

Open pihang opened 3 weeks ago

pihang commented 3 weeks ago

🐛 Describe the bug

memo/proxy/main.py It seems that when answering questions in memo/proxy/main.py, the recall mechanism doesn't utilize the dictionary information from the graph database.

Current code: memories_text = "\n".join(memory["memory"] for memory in relevant_memories["results"])

In memo/memory/main.py, the memory search actually includes parallel searches in both vector storage and graph storage. The code returns: return {"results": original_memories, "relations": graph_entities}

However, here only original_memories from vector storage recall is handled, and the graph database entities (graph_entities) are not used in constructing the memory recall.

How should we handle the recall of entities from the graph database?

For instance, data structured as follows is not being utilized: search_results.append({"source": item[0], "relationship": item[1], "target": item[2]}) Example:

{
  "memories": [...],
  "entities": [
    {"source": "peter", "relation": "identity", "destination": "spiderman"}
  ]
}
Cirr0e commented 5 days ago

Here's a proposed solution to integrate graph database entities into the memory recall process:

def enhanced_memory_recall(relevant_memories):
    # Combine vector storage memories
    memories_text = "\n".join(memory["memory"] for memory in relevant_memories["results"])

    # Add graph entity information to provide additional context
    if "relations" in relevant_memories and relevant_memories["relations"]:
        # Format graph entities into a readable string
        graph_context = "\n".join([
            f"Relation: {entity.get('relationship', 'Unknown')} "
            f"From: {entity.get('source', 'Unknown')} "
            f"To: {entity.get('target', 'Unknown')}"
            for entity in relevant_memories.get("relations", [])
        ])

        # Combine memories with graph entity context
        full_context = f"{memories_text}\n\n--- Related Entities ---\n{graph_context}"
    else:
        full_context = memories_text

    return full_context

Key improvements:

  1. Preserves existing vector memory retrieval
  2. Adds graph entity information if available
  3. Creates a comprehensive context string
  4. Provides optional fallback if no graph entities exist

Recommended modifications in memo/proxy/main.py:

# Replace existing memory text generation
memories_text = enhanced_memory_recall(relevant_memories)

Rationale and References:

Additional Recommendations:

  1. Ensure your Neo4j configuration is up to date (version v1.1+)
  2. Validate graph entity extraction in your memory creation process
  3. Consider adding logging to track graph entity utilization

Potential Enhancement: For more advanced use cases, you might want to develop a more sophisticated entity integration strategy that can: