|
| 1 | +--- |
| 2 | +layout: integration |
| 3 | +name: Mem0 |
| 4 | +description: Add persistent, user-specific memory to your Haystack agents and pipelines with Mem0 |
| 5 | +authors: |
| 6 | + - name: deepset |
| 7 | + socials: |
| 8 | + github: deepset-ai |
| 9 | + twitter: haystack_ai |
| 10 | + linkedin: https://www.linkedin.com/company/deepset-ai/ |
| 11 | + - name: Mem0 |
| 12 | + socials: |
| 13 | + github: mem0ai |
| 14 | + twitter: mem0ai |
| 15 | + linkedin: https://www.linkedin.com/company/mem0ai/ |
| 16 | +pypi: https://pypi.org/project/haystack-experimental/ |
| 17 | +repo: https://github.com/deepset-ai/haystack-experimental |
| 18 | +type: Memory Store |
| 19 | +report_issue: https://github.com/deepset-ai/haystack-experimental/issues |
| 20 | +logo: /logos/mem0.png |
| 21 | +version: Haystack 2.0 |
| 22 | +toc: true |
| 23 | +--- |
| 24 | + |
| 25 | +### Table of Contents |
| 26 | + |
| 27 | +- [Overview](#overview) |
| 28 | +- [Installation](#installation) |
| 29 | +- [Usage](#usage) |
| 30 | + - [Standalone Memory Operations](#standalone-memory-operations) |
| 31 | + - [Using Mem0 with a Haystack Agent](#using-mem0-with-a-haystack-agent) |
| 32 | +- [License](#license) |
| 33 | + |
| 34 | +## Overview |
| 35 | + |
| 36 | +[Mem0](https://mem0.ai) (pronounced "mem-zero") provides a universal memory layer for AI agents and assistants. It enables your Haystack applications to remember user preferences, adapt to individual needs, and continuously learn from past interactions — making AI conversations truly personalized. |
| 37 | + |
| 38 | +This integration is part of the [`haystack-experimental`](https://github.com/deepset-ai/haystack-experimental) package and provides the `Mem0MemoryStore`, which acts as a persistent memory backend for [Haystack Agents](https://docs.haystack.deepset.ai/docs/agent). Instead of relying solely on conversation history or static document stores, agents can use Mem0 to store and retrieve user-specific memories across sessions. |
| 39 | + |
| 40 | +### Key Features |
| 41 | + |
| 42 | +- **Persistent Memory**: Store and retrieve user-specific memories that persist across sessions |
| 43 | +- **Multi-Level Scoping**: Organize memories by User, Session, or Agent scope |
| 44 | +- **Intelligent Extraction**: Automatically extracts relevant facts from conversations — no need to store entire transcripts |
| 45 | +- **Seamless Agent Integration**: Works natively with Haystack's Agent component for context-aware responses |
| 46 | +- **Flexible Deployment**: Use the managed [Mem0 Platform](https://app.mem0.ai) or self-host with your own infrastructure |
| 47 | + |
| 48 | +More info about Mem0: |
| 49 | + |
| 50 | +- [Mem0 Website](https://mem0.ai) |
| 51 | +- [Mem0 Documentation](https://docs.mem0.ai) |
| 52 | +- [Mem0 Platform (Managed)](https://app.mem0.ai) |
| 53 | +- [Mem0 GitHub](https://github.com/mem0ai/mem0) |
| 54 | + |
| 55 | +## Installation |
| 56 | + |
| 57 | +```bash |
| 58 | +pip install haystack-ai haystack-experimental mem0ai |
| 59 | +``` |
| 60 | + |
| 61 | +### Environment Variables |
| 62 | + |
| 63 | +Set the following environment variable to use the Mem0 Platform: |
| 64 | + |
| 65 | +```bash |
| 66 | +export MEM0_API_KEY="your-mem0-api-key" |
| 67 | +``` |
| 68 | + |
| 69 | +You can obtain an API key by signing up at [app.mem0.ai](https://app.mem0.ai). |
| 70 | + |
| 71 | +## Usage |
| 72 | + |
| 73 | +### Components |
| 74 | + |
| 75 | +This integration introduces one component: |
| 76 | + |
| 77 | +- [`Mem0MemoryStore`](https://docs.haystack.deepset.ai/reference/experimental-mem0-memory-store-api#mem0memorystore): A memory store that uses Mem0 as the backend for storing and retrieving user-specific memories. It can be used standalone or plugged into a Haystack Agent. |
| 78 | + |
| 79 | +### Standalone Memory Operations |
| 80 | + |
| 81 | +You can use `Mem0MemoryStore` directly to add and search memories: |
| 82 | + |
| 83 | +```python |
| 84 | +import os |
| 85 | +from haystack.dataclasses import ChatMessage |
| 86 | +from haystack_experimental.memory_stores.mem0 import Mem0MemoryStore |
| 87 | + |
| 88 | +os.environ["MEM0_API_KEY"] = "your-mem0-api-key" |
| 89 | + |
| 90 | +# Initialize the memory store |
| 91 | +memory = Mem0MemoryStore() |
| 92 | + |
| 93 | +# Add memories from a conversation |
| 94 | +messages = [ |
| 95 | + ChatMessage.from_user("I'm a vegetarian and I love Italian food."), |
| 96 | +] |
| 97 | +memory.add_memories(messages=messages, user_id="alice") |
| 98 | + |
| 99 | +# Later, retrieve relevant memories |
| 100 | +query = "What kind of food do I like?" |
| 101 | +memories = memory.search_memories(query=query, user_id="alice") |
| 102 | + |
| 103 | +print(memories) |
| 104 | +# Returns memories related to Alice's food preferences |
| 105 | +``` |
| 106 | + |
| 107 | +### Using Mem0 with a Haystack Agent |
| 108 | + |
| 109 | +The primary use case for `Mem0MemoryStore` is to provide personalized context to a Haystack Agent. The agent automatically retrieves relevant memories before generating a response, enabling context-aware conversations: |
| 110 | + |
| 111 | +```python |
| 112 | +import os |
| 113 | +from haystack.components.generators.chat import OpenAIChatGenerator |
| 114 | +from haystack.dataclasses import ChatMessage |
| 115 | +from haystack_experimental.components.agents import Agent |
| 116 | +from haystack_experimental.memory_stores.mem0 import Mem0MemoryStore |
| 117 | + |
| 118 | +os.environ["MEM0_API_KEY"] = "your-mem0-api-key" |
| 119 | +os.environ["OPENAI_API_KEY"] = "your-openai-api-key" |
| 120 | + |
| 121 | +# Initialize components |
| 122 | +memory_store = Mem0MemoryStore() |
| 123 | +generator = OpenAIChatGenerator(model="gpt-4o") |
| 124 | + |
| 125 | +# Create an agent with memory |
| 126 | +agent = Agent( |
| 127 | + generator=generator, |
| 128 | + memory_store=memory_store, |
| 129 | + system_prompt="You are a helpful assistant that remembers user preferences.", |
| 130 | +) |
| 131 | + |
| 132 | +# First interaction — the agent learns about the user |
| 133 | +response = agent.run( |
| 134 | + query="I prefer dark mode and use vim keybindings.", |
| 135 | + memory_store_kwargs={"user_id": "user_123"}, |
| 136 | +) |
| 137 | +print(response["replies"][0].content) |
| 138 | + |
| 139 | +# Later interaction — the agent recalls the user's preferences |
| 140 | +response = agent.run( |
| 141 | + query="Can you recommend an IDE setup for me?", |
| 142 | + memory_store_kwargs={"user_id": "user_123"}, |
| 143 | +) |
| 144 | +print(response["replies"][0].content) |
| 145 | +# The agent will reference the user's preference for dark mode and vim keybindings |
| 146 | +``` |
| 147 | + |
| 148 | +> **Note:** The `Mem0MemoryStore` is currently part of the `haystack-experimental` package and is labeled as **experimental**. The API may change in future releases. Always refer to the [haystack-experimental repository](https://github.com/deepset-ai/haystack-experimental) for the latest updates. |
| 149 | +
|
| 150 | +### License |
| 151 | + |
| 152 | +`haystack-experimental` is distributed under the terms of the [Apache-2.0](https://spdx.org/licenses/Apache-2.0.html) license. |
0 commit comments