repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | --- |
| #2 | title: Graph Memory |
| #3 | description: "Enable graph-based memory retrieval for more contextually relevant results" |
| #4 | --- |
| #5 | |
| #6 | ## Overview |
| #7 | |
| #8 | Graph Memory enhances the memory pipeline by creating relationships between entities in your data. It builds a network of interconnected information for more contextually relevant search results. |
| #9 | |
| #10 | 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. |
| #11 | |
| #12 | ## How Graph Memory Works |
| #13 | |
| #14 | The Graph Memory feature analyzes how each entity connects and relates to each other. When enabled: |
| #15 | |
| #16 | 1. Mem0 automatically builds a graph representation of entities |
| #17 | 2. Vector search returns the top semantic matches (with any reranker you configure) |
| #18 | 3. Graph relations are returned alongside those results to provide additional context—they do not reorder the vector hits |
| #19 | |
| #20 | ## Using Graph Memory |
| #21 | |
| #22 | To use Graph Memory, you need to enable it in your API calls by setting the `enable_graph=True` parameter. |
| #23 | |
| #24 | ### Adding Memories with Graph Memory |
| #25 | |
| #26 | When adding new memories, enable Graph Memory to automatically build relationships with existing memories: |
| #27 | |
| #28 | <CodeGroup> |
| #29 | |
| #30 | ```python Python |
| #31 | from mem0 import MemoryClient |
| #32 | |
| #33 | client = MemoryClient( |
| #34 | api_key="your-api-key", |
| #35 | org_id="your-org-id", |
| #36 | project_id="your-project-id" |
| #37 | ) |
| #38 | |
| #39 | messages = [ |
| #40 | {"role": "user", "content": "My name is Joseph"}, |
| #41 | {"role": "assistant", "content": "Hello Joseph, it's nice to meet you!"}, |
| #42 | {"role": "user", "content": "I'm from Seattle and I work as a software engineer"} |
| #43 | ] |
| #44 | |
| #45 | # Enable graph memory when adding |
| #46 | client.add( |
| #47 | messages, |
| #48 | user_id="joseph", |
| #49 | enable_graph=True |
| #50 | ) |
| #51 | ``` |
| #52 | |
| #53 | ```javascript JavaScript |
| #54 | import { MemoryClient } from "mem0"; |
| #55 | |
| #56 | const client = new MemoryClient({ |
| #57 | apiKey: "your-api-key", |
| #58 | org_id: "your-org-id", |
| #59 | project_id: "your-project-id" |
| #60 | }); |
| #61 | |
| #62 | const messages = [ |
| #63 | { role: "user", content: "My name is Joseph" }, |
| #64 | { role: "assistant", content: "Hello Joseph, it's nice to meet you!" }, |
| #65 | { role: "user", content: "I'm from Seattle and I work as a software engineer" } |
| #66 | ]; |
| #67 | |
| #68 | // Enable graph memory when adding |
| #69 | await client.add({ |
| #70 | messages, |
| #71 | user_id: "joseph", |
| #72 | enable_graph: true |
| #73 | }); |
| #74 | ``` |
| #75 | |
| #76 | ```json Output |
| #77 | { |
| #78 | "results": [ |
| #79 | { |
| #80 | "memory": "Name is Joseph", |
| #81 | "event": "ADD", |
| #82 | "id": "4a5a417a-fa10-43b5-8c53-a77c45e80438" |
| #83 | }, |
| #84 | { |
| #85 | "memory": "Is from Seattle", |
| #86 | "event": "ADD", |
| #87 | "id": "8d268d0f-5452-4714-b27d-ae46f676a49d" |
| #88 | }, |
| #89 | { |
| #90 | "memory": "Is a software engineer", |
| #91 | "event": "ADD", |
| #92 | "id": "5f0a184e-ddea-4fe6-9b92-692d6a901df8" |
| #93 | } |
| #94 | ] |
| #95 | } |
| #96 | ``` |
| #97 | </CodeGroup> |
| #98 | |
| #99 | The graph memory would look like this: |
| #100 | |
| #101 | <Frame> |
| #102 | <img src="/images/graph-platform.png" alt="Graph Memory Visualization showing relationships between entities" /> |
| #103 | </Frame> |
| #104 | |
| #105 | <Caption>Graph Memory creates a network of relationships between entities, enabling more contextual retrieval</Caption> |
| #106 | |
| #107 | |
| #108 | <Note> |
| #109 | 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. |
| #110 | </Note> |
| #111 | |
| #112 | |
| #113 | ### Searching with Graph Memory |
| #114 | |
| #115 | When searching memories, Graph Memory helps retrieve entities that are contextually important even if they're not direct semantic matches. |
| #116 | |
| #117 | <CodeGroup> |
| #118 | |
| #119 | ```python Python |
| #120 | # Search with graph memory enabled |
| #121 | results = client.search( |
| #122 | "what is my name?", |
| #123 | user_id="joseph", |
| #124 | enable_graph=True |
| #125 | ) |
| #126 | |
| #127 | print(results) |
| #128 | ``` |
| #129 | |
| #130 | ```javascript JavaScript |
| #131 | // Search with graph memory enabled |
| #132 | const results = await client.search({ |
| #133 | query: "what is my name?", |
| #134 | user_id: "joseph", |
| #135 | enable_graph: true |
| #136 | }); |
| #137 | |
| #138 | console.log(results); |
| #139 | ``` |
| #140 | |
| #141 | ```json Output |
| #142 | { |
| #143 | "results": [ |
| #144 | { |
| #145 | "id": "4a5a417a-fa10-43b5-8c53-a77c45e80438", |
| #146 | "memory": "Name is Joseph", |
| #147 | "user_id": "joseph", |
| #148 | "metadata": null, |
| #149 | "categories": ["personal_details"], |
| #150 | "immutable": false, |
| #151 | "created_at": "2025-03-19T09:09:00.146390-07:00", |
| #152 | "updated_at": "2025-03-19T09:09:00.146404-07:00", |
| #153 | "score": 0.3621795393335552 |
| #154 | }, |
| #155 | { |
| #156 | "id": "8d268d0f-5452-4714-b27d-ae46f676a49d", |
| #157 | "memory": "Is from Seattle", |
| #158 | "user_id": "joseph", |
| #159 | "metadata": null, |
| #160 | "categories": ["personal_details"], |
| #161 | "immutable": false, |
| #162 | "created_at": "2025-03-19T09:09:00.170680-07:00", |
| #163 | "updated_at": "2025-03-19T09:09:00.170692-07:00", |
| #164 | "score": 0.31212713194651254 |
| #165 | } |
| #166 | ], |
| #167 | "relations": [ |
| #168 | { |
| #169 | "source": "joseph", |
| #170 | "source_type": "person", |
| #171 | "relationship": "name", |
| #172 | "target": "joseph", |
| #173 | "target_type": "person", |
| #174 | "score": 0.39 |
| #175 | } |
| #176 | ] |
| #177 | } |
| #178 | ``` |
| #179 | |
| #180 | </CodeGroup> |
| #181 | |
| #182 | <Note> |
| #183 | `results` always reflects the vector search order (optionally reranked). Graph Memory augments that response by adding related entities in the `relations` array; it does not re-rank the vector results automatically. |
| #184 | </Note> |
| #185 | |
| #186 | ### Retrieving All Memories with Graph Memory |
| #187 | |
| #188 | When retrieving all memories, Graph Memory provides additional relationship context: |
| #189 | |
| #190 | <Callout type="warning" title="Filters Required"> |
| #191 | `get_all()` now requires filters to be specified. |
| #192 | </Callout> |
| #193 | |
| #194 | <CodeGroup> |
| #195 | |
| #196 | ```python Python |
| #197 | # Get all memories with graph context |
| #198 | memories = client.get_all( |
| #199 | filters={"AND": [{"user_id": "joseph"}]}, |
| #200 | enable_graph=True |
| #201 | ) |
| #202 | |
| #203 | print(memories) |
| #204 | ``` |
| #205 | |
| #206 | ```javascript JavaScript |
| #207 | // Get all memories with graph context |
| #208 | const memories = await client.getAll({ |
| #209 | filters: {"AND": [{"user_id": "joseph"}]}, |
| #210 | enable_graph: true |
| #211 | }); |
| #212 | |
| #213 | console.log(memories); |
| #214 | ``` |
| #215 | |
| #216 | ```json Output |
| #217 | { |
| #218 | "results": [ |
| #219 | { |
| #220 | "id": "5f0a184e-ddea-4fe6-9b92-692d6a901df8", |
| #221 | "memory": "Is a software engineer", |
| #222 | "user_id": "joseph", |
| #223 | "metadata": null, |
| #224 | "categories": ["professional_details"], |
| #225 | "immutable": false, |
| #226 | "created_at": "2025-03-19T09:09:00.194116-07:00", |
| #227 | "updated_at": "2025-03-19T09:09:00.194128-07:00", |
| #228 | }, |
| #229 | { |
| #230 | "id": "8d268d0f-5452-4714-b27d-ae46f676a49d", |
| #231 | "memory": "Is from Seattle", |
| #232 | "user_id": "joseph", |
| #233 | "metadata": null, |
| #234 | "categories": ["personal_details"], |
| #235 | "immutable": false, |
| #236 | "created_at": "2025-03-19T09:09:00.170680-07:00", |
| #237 | "updated_at": "2025-03-19T09:09:00.170692-07:00", |
| #238 | }, |
| #239 | { |
| #240 | "id": "4a5a417a-fa10-43b5-8c53-a77c45e80438", |
| #241 | "memory": "Name is Joseph", |
| #242 | "user_id": "joseph", |
| #243 | "metadata": null, |
| #244 | "categories": ["personal_details"], |
| #245 | "immutable": false, |
| #246 | "created_at": "2025-03-19T09:09:00.146390-07:00", |
| #247 | "updated_at": "2025-03-19T09:09:00.146404-07:00", |
| #248 | } |
| #249 | ], |
| #250 | "relations": [ |
| #251 | { |
| #252 | "source": "joseph", |
| #253 | "source_type": "person", |
| #254 | "relationship": "name", |
| #255 | "target": "joseph", |
| #256 | "target_type": "person" |
| #257 | }, |
| #258 | { |
| #259 | "source": "joseph", |
| #260 | "source_type": "person", |
| #261 | "relationship": "city", |
| #262 | "target": "seattle", |
| #263 | "target_type": "city" |
| #264 | }, |
| #265 | { |
| #266 | "source": "joseph", |
| #267 | "source_type": "person", |
| #268 | "relationship": "job", |
| #269 | "target": "software engineer", |
| #270 | "target_type": "job" |
| #271 | } |
| #272 | ] |
| #273 | } |
| #274 | ``` |
| #275 | |
| #276 | </CodeGroup> |
| #277 | |
| #278 | ### Setting Graph Memory at Project Level |
| #279 | |
| #280 | Instead of passing `enable_graph=True` to every add call, you can enable it once at the project level: |
| #281 | |
| #282 | <CodeGroup> |
| #283 | |
| #284 | ```python Python |
| #285 | from mem0 import MemoryClient |
| #286 | |
| #287 | client = MemoryClient( |
| #288 | api_key="your-api-key", |
| #289 | org_id="your-org-id", |
| #290 | project_id="your-project-id" |
| #291 | ) |
| #292 | |
| #293 | # Enable graph memory for all operations in this project |
| #294 | client.project.update(enable_graph=True) |
| #295 | |
| #296 | # Now all add operations will use graph memory by default |
| #297 | messages = [ |
| #298 | {"role": "user", "content": "My name is Joseph"}, |
| #299 | {"role": "assistant", "content": "Hello Joseph, it's nice to meet you!"}, |
| #300 | {"role": "user", "content": "I'm from Seattle and I work as a software engineer"} |
| #301 | ] |
| #302 | |
| #303 | client.add( |
| #304 | messages, |
| #305 | user_id="joseph" |
| #306 | ) |
| #307 | ``` |
| #308 | |
| #309 | ```javascript JavaScript |
| #310 | import { MemoryClient } from "mem0"; |
| #311 | |
| #312 | const client = new MemoryClient({ |
| #313 | apiKey: "your-api-key", |
| #314 | org_id: "your-org-id", |
| #315 | project_id: "your-project-id" |
| #316 | }); |
| #317 | |
| #318 | // Enable graph memory for all operations in this project |
| #319 | await client.project.update({ enable_graph: true }); |
| #320 | |
| #321 | // Now all add operations will use graph memory by default |
| #322 | const messages = [ |
| #323 | { role: "user", content: "My name is Joseph" }, |
| #324 | { role: "assistant", content: "Hello Joseph, it's nice to meet you!" }, |
| #325 | { role: "user", content: "I'm from Seattle and I work as a software engineer" } |
| #326 | ]; |
| #327 | |
| #328 | await client.add({ |
| #329 | messages, |
| #330 | user_id: "joseph" |
| #331 | }); |
| #332 | ``` |
| #333 | |
| #334 | </CodeGroup> |
| #335 | |
| #336 | |
| #337 | ## Best Practices |
| #338 | |
| #339 | - Enable Graph Memory for applications where understanding context and relationships between memories is important. |
| #340 | - Graph Memory works best with a rich history of related conversations. |
| #341 | - Consider Graph Memory for long-running assistants that need to track evolving information. |
| #342 | |
| #343 | ## Performance Considerations |
| #344 | |
| #345 | 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. |
| #346 | |
| #347 | If you have any questions, please feel free to reach out to us using one of the following methods: |
| #348 | |
| #349 | <Snippet file="get-help.mdx" /> |
| #350 |