# Mem0

  }
    title="Mem0 integration"
    href="https://github.com/mem0ai/mem0/blob/v1.0.5/examples/graph-db-demo/memgraph-example.ipynb"
    is_external={true}
  />

Mem0 is a framework for managing memory in AI applications. Its graph memory
can use Memgraph as the backend, so an agent's memories are stored and
searched as a graph.

> **Info**
>
> Mem0 graph memory with Memgraph works only in Mem0 1.x. Mem0 2.0 removed graph
> memory from the open-source package, so install a 1.x version, as shown
> below.

## Installation

Install Mem0 1.x with graph memory support:

```bash
pip install "mem0ai[graph]<2"
```

Start Memgraph. Mem0 stores embeddings in a Memgraph vector index:

```bash
docker run -p 7687:7687 memgraph/memgraph-mage:latest
```

## Configuration

Set your OpenAI API key:

```python
from mem0 import Memory
import os

os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY"
```

Configure OpenAI for embeddings and Memgraph as the graph store:

```python
config = {
    "embedder": {
        "provider": "openai",
        "config": {
            "model": "text-embedding-3-large",
            "embedding_dims": 1536,
        },
    },
    "graph_store": {
        "provider": "memgraph",
        "config": {
            "url": "bolt://localhost:7687",
            "username": "memgraph",
            "password": "mem0graph",
        },
    },
}
```

Create the memory store. Mem0 connects to Memgraph and creates the vector
index it needs:

```python
m = Memory.from_config(config_dict=config)
```

## Store memories

Add a conversation as memories for a user, with metadata:

```python
messages = [
    {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
    {"role": "assistant", "content": "How about a thriller movie? They can be quite engaging."},
    {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
    {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."},
]

result = m.add(messages, user_id="alice", metadata={"category": "movie_recommendations"})
```

## Search memories

Search a user's memories with a question:

```python
for result in m.search("what does alice love?", user_id="alice")["results"]:
    print(result["memory"], result["score"])
```

The output looks like this:

```plaintext copy=false
Loves sci-fi movies 0.315
Planning to watch a movie tonight 0.096
Not a big fan of thriller movies 0.094
```

For the full example, see Mem0's [Memgraph
notebook](https://github.com/mem0ai/mem0/blob/v1.0.5/examples/graph-db-demo/memgraph-example.ipynb)
from the 1.0.5 release.
