Graph Memory
FreshGraph Memory establishes connections between data entities, creating a network of interconnected information for more contextually relevant search outcomes.
How It Works
- Mem0 constructs a graph representation of entities automatically
- Vector search identifies the top semantic matches (with optional reranking)
- Graph relations accompany results for additional context
mermaid
graph LR
A[Joseph] -->|name| B[Joseph]
A -->|city| C[Seattle]
A -->|job| D[Software Engineer]Adding Memories with Graph Memory
Enable Graph Memory using the enable_graph=True parameter.
python
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, nice to meet you!"},
{"role": "user", "content": "I'm from Seattle and I work as a software engineer"}
]
client.add(messages, user_id="joseph", enable_graph=True)javascript
import { MemoryClient } from "mem0";
const client = new MemoryClient({
apiKey: "your-api-key",
org_id: "your-org-id",
project_id: "your-project-id"
});
const messages = [
{ role: "user", content: "My name is Joseph" },
{ role: "assistant", content: "Hello Joseph, nice to meet you!" },
{ role: "user", content: "I'm from Seattle and I work as a software engineer" }
];
await client.add({
messages,
user_id: "joseph",
enable_graph: true
});INFO
Graph memory add responses are not immediately available since the operation processes asynchronously. Use get_all() to retrieve memories with graph metadata.
Searching with Graph Memory
python
results = client.search(
"what is my name?",
user_id="joseph",
enable_graph=True
)javascript
const results = await client.search({
query: "what is my name?",
user_id: "joseph",
enable_graph: true
});Search Response
json
{
"results": [
{
"id": "4a5a417a-...",
"memory": "Name is Joseph",
"user_id": "joseph",
"categories": ["personal_details"],
"score": 0.3621
}
],
"relations": [
{
"source": "joseph",
"source_type": "person",
"relationship": "name",
"target": "joseph",
"target_type": "person",
"score": 0.39
}
]
}TIP
Results reflect vector search order (optionally reranked). Graph Memory augments responses by adding related entities in the relations array without reordering vector results.
Retrieving All Memories with Graph
python
memories = client.get_all(
filters={"AND": [{"user_id": "joseph"}]},
enable_graph=True
)javascript
const memories = await client.getAll({
filters: { AND: [{ user_id: "joseph" }] },
enable_graph: true
});WARNING
get_all() requires filters to be specified.
Project-Level Graph Memory
Set graph memory at the project level so all operations use it automatically:
python
client.project.update(enable_graph=True)
# Now all add() calls use graph memory by default
client.add(messages, user_id="joseph")javascript
await client.project.update({ enable_graph: true });
// Now all add() calls use graph memory by default
await client.add({ messages, user_id: "joseph" });Best Practices
- Use Graph Memory when understanding entity relationships matters
- Performance improves with substantial interaction history
- Graph Memory suits long-term assistants managing evolving information
- Expect slightly longer response times for large memory repositories