|
| 1 | +--- |
| 2 | +title: "Mem0MemoryRetriever" |
| 3 | +id: mem0memoryretriever |
| 4 | +slug: "/mem0memoryretriever" |
| 5 | +description: "Retrieves long-term memories from Mem0 as ChatMessage objects." |
| 6 | +--- |
| 7 | + |
| 8 | +# Mem0MemoryRetriever |
| 9 | + |
| 10 | +Retrieves long-term memories from Mem0 as `ChatMessage` objects. |
| 11 | + |
| 12 | +<div className="key-value-table"> |
| 13 | + |
| 14 | +| | | |
| 15 | +| --- | --- | |
| 16 | +| **Most common position in a pipeline** | Before an [`Agent`](../agents-1/agent.mdx) or Chat Generator in memory-augmented pipelines | |
| 17 | +| **Mandatory init variables** | `memory_store`: A `Mem0MemoryStore` instance | |
| 18 | +| **Mandatory run variables** | `query`: A text query or `None`; at least one Mem0 scope through `user_id`, `run_id`, `agent_id`, `app_id`, or `filters` | |
| 19 | +| **Output variables** | `memories`: A list of `ChatMessage` objects | |
| 20 | +| **Mem0 API docs** | [Search Memories](https://docs.mem0.ai/api-reference/memory/search-memories), [Memory Filters](https://docs.mem0.ai/platform/features/v2-memory-filters) | |
| 21 | +| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/mem0 | |
| 22 | +| **Package name** | `mem0-haystack` | |
| 23 | + |
| 24 | +</div> |
| 25 | + |
| 26 | +## Overview |
| 27 | + |
| 28 | +`Mem0MemoryRetriever` retrieves memories from a `Mem0MemoryStore` and returns them as system `ChatMessage` objects. |
| 29 | +Use it to inject long-term memory into an Agent or a chat generation pipeline before the model produces a response. |
| 30 | + |
| 31 | +The `query` input can be a string or `None`. |
| 32 | +When `query` is a string, the component searches for relevant memories and applies `top_k`. |
| 33 | +When `query` is `None`, it returns all memories matching the provided scope. |
| 34 | + |
| 35 | +Scope the retrieval with at least one Mem0 entity ID: `user_id`, `run_id`, `agent_id`, or `app_id`. |
| 36 | +You can also pass Haystack-style `filters`; when filters and ID parameters are both provided, they are combined with an `AND` condition. |
| 37 | +For general filter syntax, see [Metadata Filtering](../../concepts/metadata-filtering.mdx). |
| 38 | + |
| 39 | +User-provided Mem0 metadata is included in each returned message's `meta`. |
| 40 | +Mem0 retrieval fields such as `memory_id`, `user_id`, `score`, and timestamps are included under `meta["mem0"]`. |
| 41 | + |
| 42 | +### Installation |
| 43 | + |
| 44 | +Install the Mem0 integration: |
| 45 | + |
| 46 | +```shell |
| 47 | +pip install mem0-haystack |
| 48 | +``` |
| 49 | + |
| 50 | +Set your Mem0 API key: |
| 51 | + |
| 52 | +```shell |
| 53 | +export MEM0_API_KEY="your-mem0-api-key" |
| 54 | +``` |
| 55 | + |
| 56 | +## Usage |
| 57 | + |
| 58 | +### On its own |
| 59 | + |
| 60 | +```python |
| 61 | +from haystack.dataclasses import ChatMessage |
| 62 | + |
| 63 | +from haystack_integrations.components.retrievers.mem0 import Mem0MemoryRetriever |
| 64 | +from haystack_integrations.memory_stores.mem0 import Mem0MemoryStore |
| 65 | + |
| 66 | +store = Mem0MemoryStore() |
| 67 | +store.add_memories( |
| 68 | + messages=[ChatMessage.from_user("Alice prefers concise Python examples.")], |
| 69 | + user_id="alice", |
| 70 | + infer=False, |
| 71 | +) |
| 72 | + |
| 73 | +retriever = Mem0MemoryRetriever(memory_store=store, top_k=3) |
| 74 | + |
| 75 | +result = retriever.run(query="answer style", user_id="alice") |
| 76 | +memories = result["memories"] |
| 77 | + |
| 78 | +for memory in memories: |
| 79 | + print(memory.text) |
| 80 | +``` |
| 81 | + |
| 82 | +To retrieve all memories in scope, pass `query=None`: |
| 83 | + |
| 84 | +```python |
| 85 | +all_memories = retriever.run(query=None, user_id="alice")["memories"] |
| 86 | +print([memory.text for memory in all_memories]) |
| 87 | +``` |
| 88 | + |
| 89 | +### In a Pipeline |
| 90 | + |
| 91 | +This example retrieves memories, prepends them to the current user message, and passes the combined message list to an Agent. |
| 92 | + |
| 93 | +```python |
| 94 | +from haystack import Pipeline |
| 95 | +from haystack.components.agents import Agent |
| 96 | +from haystack.components.converters import OutputAdapter |
| 97 | +from haystack.components.generators.chat import OpenAIChatGenerator |
| 98 | +from haystack.components.generators.utils import print_streaming_chunk |
| 99 | +from haystack.dataclasses import ChatMessage |
| 100 | + |
| 101 | +from haystack_integrations.components.retrievers.mem0 import Mem0MemoryRetriever |
| 102 | +from haystack_integrations.memory_stores.mem0 import Mem0MemoryStore |
| 103 | + |
| 104 | +store = Mem0MemoryStore() |
| 105 | + |
| 106 | +pipeline = Pipeline() |
| 107 | +pipeline.add_component("retriever", Mem0MemoryRetriever(memory_store=store, top_k=5)) |
| 108 | +pipeline.add_component( |
| 109 | + "memory_context", |
| 110 | + OutputAdapter( |
| 111 | + template="{{ memories + user_messages }}", |
| 112 | + output_type=list[ChatMessage], |
| 113 | + unsafe=True, |
| 114 | + ), |
| 115 | +) |
| 116 | +pipeline.add_component( |
| 117 | + "agent", |
| 118 | + Agent( |
| 119 | + chat_generator=OpenAIChatGenerator(model="gpt-4o-mini"), |
| 120 | + system_prompt=( |
| 121 | + "Use any system messages at the start of the conversation as long-term memory. " |
| 122 | + "Answer concisely." |
| 123 | + ), |
| 124 | + streaming_callback=print_streaming_chunk, |
| 125 | + ), |
| 126 | +) |
| 127 | + |
| 128 | +pipeline.connect("retriever.memories", "memory_context.memories") |
| 129 | +pipeline.connect("memory_context.output", "agent.messages") |
| 130 | + |
| 131 | +query = "Give me a short implementation tip." |
| 132 | + |
| 133 | +pipeline.run( |
| 134 | + { |
| 135 | + "retriever": { |
| 136 | + "query": query, |
| 137 | + "user_id": "alice", |
| 138 | + }, |
| 139 | + "memory_context": { |
| 140 | + "user_messages": [ |
| 141 | + ChatMessage.from_user(query), |
| 142 | + ], |
| 143 | + }, |
| 144 | + }, |
| 145 | +) |
| 146 | +``` |
0 commit comments