|
| 1 | +--- |
| 2 | +title: Graph Memory |
| 3 | +icon: "circle-nodes" |
| 4 | +iconType: "solid" |
| 5 | +description: "Enable graph-based memory retrieval for more contextually relevant results" |
| 6 | +--- |
| 7 | + |
| 8 | +## Overview |
| 9 | + |
| 10 | +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. |
| 11 | + |
| 12 | +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. |
| 13 | + |
| 14 | +## How Graph Memory Works |
| 15 | + |
| 16 | +The Graph Memory feature analyzes how each entity connects and relates to each other. When enabled: |
| 17 | + |
| 18 | +1. Mem0 automatically builds a graph representation of entities |
| 19 | +2. Retrieval considers graph relationships between entities |
| 20 | +3. Results include entities that may be contextually important even if they're not direct semantic matches |
| 21 | + |
| 22 | +## Using Graph Memory |
| 23 | + |
| 24 | +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. |
| 25 | + |
| 26 | +### Adding Memories with Graph Memory |
| 27 | + |
| 28 | +When adding new memories, enable Graph Memory to automatically build relationships with existing memories: |
| 29 | + |
| 30 | +<CodeGroup> |
| 31 | + |
| 32 | +```python Python |
| 33 | +from mem0 import MemoryClient |
| 34 | + |
| 35 | +client = MemoryClient( |
| 36 | + api_key="your-api-key", |
| 37 | + org_id="your-org-id", |
| 38 | + project_id="your-project-id" |
| 39 | +) |
| 40 | + |
| 41 | +messages = [ |
| 42 | + {"role": "user", "content": "My name is Joseph"}, |
| 43 | + {"role": "assistant", "content": "Hello Joseph, it's nice to meet you!"}, |
| 44 | + {"role": "user", "content": "I'm from Seattle and I work as a software engineer"} |
| 45 | +] |
| 46 | + |
| 47 | +# Enable graph memory when adding |
| 48 | +client.add( |
| 49 | + messages, |
| 50 | + user_id="joseph", |
| 51 | + version="v1", |
| 52 | + enable_graph=True, |
| 53 | + output_format="v1.1" |
| 54 | +) |
| 55 | +``` |
| 56 | + |
| 57 | +```javascript JavaScript |
| 58 | +import { MemoryClient } from "mem0"; |
| 59 | + |
| 60 | +const client = new MemoryClient({ |
| 61 | + apiKey: "your-api-key", |
| 62 | + orgId: "your-org-id", |
| 63 | + projectId: "your-project-id" |
| 64 | +}); |
| 65 | + |
| 66 | +const messages = [ |
| 67 | + { role: "user", content: "My name is Joseph" }, |
| 68 | + { role: "assistant", content: "Hello Joseph, it's nice to meet you!" }, |
| 69 | + { role: "user", content: "I'm from Seattle and I work as a software engineer" } |
| 70 | +]; |
| 71 | + |
| 72 | +// Enable graph memory when adding |
| 73 | +await client.add({ |
| 74 | + messages, |
| 75 | + userId: "joseph", |
| 76 | + version: "v1", |
| 77 | + enableGraph: true, |
| 78 | + outputFormat: "v1.1" |
| 79 | +}); |
| 80 | +``` |
| 81 | + |
| 82 | +```json Output |
| 83 | +{ |
| 84 | + "results": [ |
| 85 | + { |
| 86 | + "memory": "Name is Joseph", |
| 87 | + "event": "ADD", |
| 88 | + "id": "4a5a417a-fa10-43b5-8c53-a77c45e80438" |
| 89 | + }, |
| 90 | + { |
| 91 | + "memory": "Is from Seattle", |
| 92 | + "event": "ADD", |
| 93 | + "id": "8d268d0f-5452-4714-b27d-ae46f676a49d" |
| 94 | + }, |
| 95 | + { |
| 96 | + "memory": "Is a software engineer", |
| 97 | + "event": "ADD", |
| 98 | + "id": "5f0a184e-ddea-4fe6-9b92-692d6a901df8" |
| 99 | + } |
| 100 | + ] |
| 101 | +} |
| 102 | +``` |
| 103 | +</CodeGroup> |
| 104 | + |
| 105 | +The graph memory would look like this: |
| 106 | + |
| 107 | +<Frame> |
| 108 | + <img src="/images/graph-platform.png" alt="Graph Memory Visualization showing relationships between entities" /> |
| 109 | +</Frame> |
| 110 | + |
| 111 | +<Caption>Graph Memory creates a network of relationships between entities, enabling more contextual retrieval</Caption> |
| 112 | + |
| 113 | + |
| 114 | +<Note> |
| 115 | +Response for the graph memory's `add` operation will not be available directly in the response. |
| 116 | +As adding graph memories is an asynchronous operation due to heavy processing, |
| 117 | +you can use the `get_all()` endpoint to retrieve the memory with the graph metadata. |
| 118 | +</Note> |
| 119 | + |
| 120 | + |
| 121 | +### Searching with Graph Memory |
| 122 | + |
| 123 | +When searching memories, Graph Memory helps retrieve entities that are contextually important even if they're not direct semantic matches. |
| 124 | + |
| 125 | +<CodeGroup> |
| 126 | + |
| 127 | +```python Python |
| 128 | +# Search with graph memory enabled |
| 129 | +results = client.search( |
| 130 | + "what is my name?", |
| 131 | + user_id="joseph", |
| 132 | + enable_graph=True, |
| 133 | + output_format="v1.1" |
| 134 | +) |
| 135 | + |
| 136 | +print(results) |
| 137 | +``` |
| 138 | + |
| 139 | +```javascript JavaScript |
| 140 | +// Search with graph memory enabled |
| 141 | +const results = await client.search({ |
| 142 | + query: "what is my name?", |
| 143 | + userId: "joseph", |
| 144 | + enableGraph: true, |
| 145 | + outputFormat: "v1.1" |
| 146 | +}); |
| 147 | + |
| 148 | +console.log(results); |
| 149 | +``` |
| 150 | + |
| 151 | +```json Output |
| 152 | +{ |
| 153 | + "results": [ |
| 154 | + { |
| 155 | + "id": "4a5a417a-fa10-43b5-8c53-a77c45e80438", |
| 156 | + "memory": "Name is Joseph", |
| 157 | + "user_id": "joseph", |
| 158 | + "metadata": null, |
| 159 | + "categories": ["personal_details"], |
| 160 | + "immutable": false, |
| 161 | + "created_at": "2025-03-19T09:09:00.146390-07:00", |
| 162 | + "updated_at": "2025-03-19T09:09:00.146404-07:00", |
| 163 | + "score": 0.3621795393335552 |
| 164 | + }, |
| 165 | + { |
| 166 | + "id": "8d268d0f-5452-4714-b27d-ae46f676a49d", |
| 167 | + "memory": "Is from Seattle", |
| 168 | + "user_id": "joseph", |
| 169 | + "metadata": null, |
| 170 | + "categories": ["personal_details"], |
| 171 | + "immutable": false, |
| 172 | + "created_at": "2025-03-19T09:09:00.170680-07:00", |
| 173 | + "updated_at": "2025-03-19T09:09:00.170692-07:00", |
| 174 | + "score": 0.31212713194651254 |
| 175 | + } |
| 176 | + ], |
| 177 | + "relations": [ |
| 178 | + { |
| 179 | + "source": "joseph", |
| 180 | + "source_type": "person", |
| 181 | + "relationship": "name", |
| 182 | + "target": "joseph", |
| 183 | + "target_type": "person", |
| 184 | + "score": 0.39 |
| 185 | + } |
| 186 | + ] |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | +</CodeGroup> |
| 191 | + |
| 192 | +### Retrieving All Memories with Graph Memory |
| 193 | + |
| 194 | +When retrieving all memories, Graph Memory provides additional relationship context: |
| 195 | + |
| 196 | +<CodeGroup> |
| 197 | + |
| 198 | +```python Python |
| 199 | +# Get all memories with graph context |
| 200 | +memories = client.get_all( |
| 201 | + user_id="joseph", |
| 202 | + enable_graph=True, |
| 203 | + output_format="v1.1" |
| 204 | +) |
| 205 | + |
| 206 | +print(memories) |
| 207 | +``` |
| 208 | + |
| 209 | +```javascript JavaScript |
| 210 | +// Get all memories with graph context |
| 211 | +const memories = await client.getAll({ |
| 212 | + userId: "joseph", |
| 213 | + enableGraph: true, |
| 214 | + outputFormat: "v1.1" |
| 215 | +}); |
| 216 | + |
| 217 | +console.log(memories); |
| 218 | +``` |
| 219 | + |
| 220 | +```json Output |
| 221 | +{ |
| 222 | + "results": [ |
| 223 | + { |
| 224 | + "id": "5f0a184e-ddea-4fe6-9b92-692d6a901df8", |
| 225 | + "memory": "Is a software engineer", |
| 226 | + "user_id": "joseph", |
| 227 | + "metadata": null, |
| 228 | + "categories": ["professional_details"], |
| 229 | + "immutable": false, |
| 230 | + "created_at": "2025-03-19T09:09:00.194116-07:00", |
| 231 | + "updated_at": "2025-03-19T09:09:00.194128-07:00", |
| 232 | + }, |
| 233 | + { |
| 234 | + "id": "8d268d0f-5452-4714-b27d-ae46f676a49d", |
| 235 | + "memory": "Is from Seattle", |
| 236 | + "user_id": "joseph", |
| 237 | + "metadata": null, |
| 238 | + "categories": ["personal_details"], |
| 239 | + "immutable": false, |
| 240 | + "created_at": "2025-03-19T09:09:00.170680-07:00", |
| 241 | + "updated_at": "2025-03-19T09:09:00.170692-07:00", |
| 242 | + }, |
| 243 | + { |
| 244 | + "id": "4a5a417a-fa10-43b5-8c53-a77c45e80438", |
| 245 | + "memory": "Name is Joseph", |
| 246 | + "user_id": "joseph", |
| 247 | + "metadata": null, |
| 248 | + "categories": ["personal_details"], |
| 249 | + "immutable": false, |
| 250 | + "created_at": "2025-03-19T09:09:00.146390-07:00", |
| 251 | + "updated_at": "2025-03-19T09:09:00.146404-07:00", |
| 252 | + } |
| 253 | + ], |
| 254 | + "relations": [ |
| 255 | + { |
| 256 | + "source": "joseph", |
| 257 | + "source_type": "person", |
| 258 | + "relationship": "name", |
| 259 | + "target": "joseph", |
| 260 | + "target_type": "person" |
| 261 | + }, |
| 262 | + { |
| 263 | + "source": "joseph", |
| 264 | + "source_type": "person", |
| 265 | + "relationship": "city", |
| 266 | + "target": "seattle", |
| 267 | + "target_type": "city" |
| 268 | + }, |
| 269 | + { |
| 270 | + "source": "joseph", |
| 271 | + "source_type": "person", |
| 272 | + "relationship": "job", |
| 273 | + "target": "software engineer", |
| 274 | + "target_type": "job" |
| 275 | + } |
| 276 | + ] |
| 277 | +} |
| 278 | +``` |
| 279 | + |
| 280 | +</CodeGroup> |
| 281 | + |
| 282 | +## Best Practices |
| 283 | + |
| 284 | +- Enable Graph Memory for applications where understanding context and relationships between memories is important |
| 285 | +- Graph Memory works best with a rich history of related conversations |
| 286 | +- Consider Graph Memory for long-running assistants that need to track evolving information |
| 287 | + |
| 288 | +## Performance Considerations |
| 289 | + |
| 290 | +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. |
| 291 | + |
| 292 | +If you have any questions, please feel free to reach out to us using one of the following methods: |
| 293 | + |
| 294 | +<Snippet file="get-help.mdx" /> |
| 295 | + |
0 commit comments