Overview

Graph Memory enhances memory pipeline by creating relationships between entities in your data. It builds a network of interconnected information for more contextually relevant search results.

This feature allows your AI applications to understand connections between entities, providing richer context for responses. It’s ideal for applications needing relationship tracking and nuanced information retrieval across related memories.

How Graph Memory Works

The Graph Memory feature analyzes how each entity connects and relates to each other. When enabled:

  1. Mem0 automatically builds a graph representation of entities
  2. Retrieval considers graph relationships between entities
  3. Results include entities that may be contextually important even if they’re not direct semantic matches

Using Graph Memory

To use Graph Memory, you need to enable it in your API calls by setting the enable_graph=True parameter. You’ll also need to specify output_format="v1.1" to receive the enriched response format.

Adding Memories with Graph Memory

When adding new memories, enable Graph Memory to automatically build relationships with existing memories:

from mem0 import MemoryClient

client = MemoryClient(
    api_key="your-api-key",
    org_id="your-org-id",
    project_id="your-project-id"
)

messages = [
    {"role": "user", "content": "My name is Joseph"},
    {"role": "assistant", "content": "Hello Joseph, it's nice to meet you!"},
    {"role": "user", "content": "I'm from Seattle and I work as a software engineer"}
]

# Enable graph memory when adding
client.add(
    messages, 
    user_id="joseph", 
    version="v1", 
    enable_graph=True, 
    output_format="v1.1"
)

The graph memory would look like this:

Response for the graph memory’s add operation will not be available directly in the response. As adding graph memories is an asynchronous operation due to heavy processing, you can use the get_all() endpoint to retrieve the memory with the graph metadata.

Searching with Graph Memory

When searching memories, Graph Memory helps retrieve entities that are contextually important even if they’re not direct semantic matches.

# Search with graph memory enabled
results = client.search(
    "what is my name?", 
    user_id="joseph", 
    enable_graph=True, 
    output_format="v1.1"
)

print(results)

Retrieving All Memories with Graph Memory

When retrieving all memories, Graph Memory provides additional relationship context:

# Get all memories with graph context
memories = client.get_all(
    user_id="joseph", 
    enable_graph=True, 
    output_format="v1.1"
)

print(memories)

Best Practices

  • Enable Graph Memory for applications where understanding context and relationships between memories is important
  • Graph Memory works best with a rich history of related conversations
  • Consider Graph Memory for long-running assistants that need to track evolving information

Performance Considerations

Graph Memory requires additional processing and may increase response times slightly for very large memory stores. However, for most use cases, the improved retrieval quality outweighs the minimal performance impact.

If you have any questions, please feel free to reach out to us using one of the following methods: