487 lines
14 KiB
Plaintext
487 lines
14 KiB
Plaintext
---
|
|
title: Guide
|
|
description: 'Get started with Mem0 quickly!'
|
|
---
|
|
|
|
> Welcome to the Mem0 quickstart guide. This guide will help you get up and running with Mem0 in no time.
|
|
|
|
## Installation
|
|
|
|
To install Mem0, you can use pip. Run the following command in your terminal:
|
|
|
|
```bash
|
|
pip install mem0ai
|
|
```
|
|
|
|
## Basic Usage
|
|
|
|
### Initialize Mem0
|
|
|
|
<Tabs>
|
|
<Tab title="Basic">
|
|
```python
|
|
from mem0 import Memory
|
|
m = Memory()
|
|
```
|
|
</Tab>
|
|
<Tab title="Advanced">
|
|
If you want to run Mem0 in production, initialize using the following method:
|
|
|
|
Run Qdrant first:
|
|
|
|
```bash
|
|
docker pull qdrant/qdrant
|
|
|
|
docker run -p 6333:6333 -p 6334:6334 \
|
|
-v $(pwd)/qdrant_storage:/qdrant/storage:z \
|
|
qdrant/qdrant
|
|
```
|
|
|
|
Then, instantiate memory with qdrant server:
|
|
|
|
```python
|
|
from mem0 import Memory
|
|
|
|
config = {
|
|
"vector_store": {
|
|
"provider": "qdrant",
|
|
"config": {
|
|
"host": "localhost",
|
|
"port": 6333,
|
|
}
|
|
},
|
|
}
|
|
|
|
m = Memory.from_config(config)
|
|
```
|
|
</Tab>
|
|
|
|
<Tab title="Advanced (Graph Memory)">
|
|
|
|
```python
|
|
from mem0 import Memory
|
|
|
|
config = {
|
|
"graph_store": {
|
|
"provider": "neo4j",
|
|
"config": {
|
|
"url": "neo4j+s://---",
|
|
"username": "neo4j",
|
|
"password": "---"
|
|
}
|
|
},
|
|
"version": "v1.1"
|
|
}
|
|
|
|
m = Memory.from_config(config_dict=config)
|
|
```
|
|
|
|
</Tab>
|
|
</Tabs>
|
|
|
|
|
|
### Store a Memory
|
|
|
|
<CodeGroup>
|
|
```python Code
|
|
# For a user
|
|
result = m.add("Likes to play cricket on weekends", user_id="alice", metadata={"category": "hobbies"})
|
|
|
|
# messages = [
|
|
# {"role": "user", "content": "Hi, I'm Alex. I like to play cricket on weekends."},
|
|
# {"role": "assistant", "content": "Hello Alex! It's great to know that you enjoy playing cricket on weekends. I'll remember that for future reference."}
|
|
# ]
|
|
# client.add(messages, user_id="alice")
|
|
```
|
|
|
|
```json Output
|
|
{
|
|
"results": [
|
|
{"id": "bf4d4092-cf91-4181-bfeb-b6fa2ed3061b", "memory": "Likes to play cricket on weekends", "event": "ADD"}
|
|
],
|
|
"relations": [
|
|
{"source": "alice", "relationship": "likes_to_play", "target": "cricket"},
|
|
{"source": "alice", "relationship": "plays_on", "target": "weekends"}
|
|
]
|
|
}
|
|
```
|
|
</CodeGroup>
|
|
|
|
### Retrieve Memories
|
|
|
|
<CodeGroup>
|
|
```python Code
|
|
# Get all memories
|
|
all_memories = m.get_all(user_id="alice")
|
|
```
|
|
|
|
```json Output
|
|
{
|
|
"results": [
|
|
{
|
|
"id": "bf4d4092-cf91-4181-bfeb-b6fa2ed3061b",
|
|
"memory": "Likes to play cricket on weekends",
|
|
"hash": "285d07801ae42054732314853e9eadd7",
|
|
"metadata": {"category": "hobbies"},
|
|
"created_at": "2024-10-28T12:32:07.744891-07:00",
|
|
"updated_at": None,
|
|
"user_id": "alice"
|
|
}
|
|
],
|
|
"relations": [
|
|
{"source": "alice", "relationship": "likes_to_play", "target": "cricket"},
|
|
{"source": "alice", "relationship": "plays_on", "target": "weekends"}
|
|
]
|
|
}
|
|
```
|
|
</CodeGroup>
|
|
|
|
|
|
<br />
|
|
|
|
<CodeGroup>
|
|
```python Code
|
|
# Get a single memory by ID
|
|
specific_memory = m.get("bf4d4092-cf91-4181-bfeb-b6fa2ed3061b")
|
|
```
|
|
|
|
```json Output
|
|
{
|
|
"id": "bf4d4092-cf91-4181-bfeb-b6fa2ed3061b",
|
|
"memory": "Likes to play cricket on weekends",
|
|
"hash": "285d07801ae42054732314853e9eadd7",
|
|
"metadata": {"category": "hobbies"},
|
|
"created_at": "2024-10-28T12:32:07.744891-07:00",
|
|
"updated_at": None,
|
|
"user_id": "alice"
|
|
}
|
|
```
|
|
</CodeGroup>
|
|
|
|
### Search Memories
|
|
|
|
<CodeGroup>
|
|
```python Code
|
|
related_memories = m.search(query="What are Alice's hobbies?", user_id="alice")
|
|
```
|
|
|
|
```json Output
|
|
{
|
|
"results": [
|
|
{
|
|
"id": "bf4d4092-cf91-4181-bfeb-b6fa2ed3061b",
|
|
"memory": "Likes to play cricket on weekends",
|
|
"hash": "285d07801ae42054732314853e9eadd7",
|
|
"metadata": {"category": "hobbies"},
|
|
"score": 0.30808347,
|
|
"created_at": "2024-10-28T12:32:07.744891-07:00",
|
|
"updated_at": None,
|
|
"user_id": "alice"
|
|
}
|
|
],
|
|
"relations": [
|
|
{"source": "alice", "relationship": "plays_on", "target": "weekends"},
|
|
{"source": "alice", "relationship": "likes_to_play", "target": "cricket"}
|
|
]
|
|
}
|
|
```
|
|
</CodeGroup>
|
|
|
|
### Update a Memory
|
|
|
|
<CodeGroup>
|
|
```python Code
|
|
result = m.update(memory_id="bf4d4092-cf91-4181-bfeb-b6fa2ed3061b", data="Likes to play tennis on weekends")
|
|
```
|
|
|
|
```json Output
|
|
{'message': 'Memory updated successfully!'}
|
|
```
|
|
</CodeGroup>
|
|
|
|
### Memory History
|
|
|
|
<CodeGroup>
|
|
```python Code
|
|
history = m.history(memory_id="bf4d4092-cf91-4181-bfeb-b6fa2ed3061b")
|
|
```
|
|
|
|
```json Output
|
|
[
|
|
{
|
|
"id": "96d2821d-e551-4089-aa57-9398c421d450",
|
|
"memory_id": "bf4d4092-cf91-4181-bfeb-b6fa2ed3061b",
|
|
"old_memory": None,
|
|
"new_memory": "Likes to play cricket on weekends",
|
|
"event": "ADD",
|
|
"created_at": "2024-10-28T12:32:07.744891-07:00",
|
|
"updated_at": None
|
|
},
|
|
{
|
|
"id": "3db4cb58-c0f1-4dd0-b62a-8123068ebfe7",
|
|
"memory_id": "bf4d4092-cf91-4181-bfeb-b6fa2ed3061b",
|
|
"old_memory": "Likes to play cricket on weekends",
|
|
"new_memory": "Likes to play tennis on weekends",
|
|
"event": "UPDATE",
|
|
"created_at": "2024-10-28T12:32:07.744891-07:00",
|
|
"updated_at": "2024-10-28T13:05:46.987978-07:00"
|
|
}
|
|
]
|
|
```
|
|
</CodeGroup>
|
|
|
|
### Delete Memory
|
|
|
|
```python
|
|
# Delete a memory by id
|
|
m.delete(memory_id="bf4d4092-cf91-4181-bfeb-b6fa2ed3061b")
|
|
# Delete all memories for a user
|
|
m.delete_all(user_id="alice")
|
|
```
|
|
|
|
### Reset Memory
|
|
|
|
```python
|
|
m.reset() # Reset all memories
|
|
```
|
|
|
|
## Configuration Parameters
|
|
|
|
Mem0 offers extensive configuration options to customize its behavior according to your needs. These configurations span across different components like vector stores, language models, embedders, and graph stores.
|
|
|
|
<AccordionGroup>
|
|
<Accordion title="Vector Store Configuration">
|
|
| Parameter | Description | Default |
|
|
|-------------|---------------------------------|-------------|
|
|
| `provider` | Vector store provider (e.g., "qdrant") | "qdrant" |
|
|
| `host` | Host address | "localhost" |
|
|
| `port` | Port number | 6333 |
|
|
</Accordion>
|
|
|
|
<Accordion title="LLM Configuration">
|
|
| Parameter | Description | Provider |
|
|
|-----------------------|-----------------------------------------------|-------------------|
|
|
| `provider` | LLM provider (e.g., "openai", "anthropic") | All |
|
|
| `model` | Model to use | All |
|
|
| `temperature` | Temperature of the model | All |
|
|
| `api_key` | API key to use | All |
|
|
| `max_tokens` | Tokens to generate | All |
|
|
| `top_p` | Probability threshold for nucleus sampling | All |
|
|
| `top_k` | Number of highest probability tokens to keep | All |
|
|
| `http_client_proxies` | Allow proxy server settings | AzureOpenAI |
|
|
| `models` | List of models | Openrouter |
|
|
| `route` | Routing strategy | Openrouter |
|
|
| `openrouter_base_url` | Base URL for Openrouter API | Openrouter |
|
|
| `site_url` | Site URL | Openrouter |
|
|
| `app_name` | Application name | Openrouter |
|
|
| `ollama_base_url` | Base URL for Ollama API | Ollama |
|
|
| `openai_base_url` | Base URL for OpenAI API | OpenAI |
|
|
| `azure_kwargs` | Azure LLM args for initialization | AzureOpenAI |
|
|
| `deepseek_base_url` | Base URL for DeepSeek API | DeepSeek |
|
|
</Accordion>
|
|
|
|
<Accordion title="Embedder Configuration">
|
|
| Parameter | Description | Default |
|
|
|-------------|---------------------------------|------------------------------|
|
|
| `provider` | Embedding provider | "openai" |
|
|
| `model` | Embedding model to use | "text-embedding-3-small" |
|
|
| `api_key` | API key for embedding service | None |
|
|
</Accordion>
|
|
|
|
<Accordion title="Graph Store Configuration">
|
|
| Parameter | Description | Default |
|
|
|-------------|---------------------------------|-------------|
|
|
| `provider` | Graph store provider (e.g., "neo4j") | "neo4j" |
|
|
| `url` | Connection URL | None |
|
|
| `username` | Authentication username | None |
|
|
| `password` | Authentication password | None |
|
|
</Accordion>
|
|
|
|
<Accordion title="General Configuration">
|
|
| Parameter | Description | Default |
|
|
|------------------|--------------------------------------|----------------------------|
|
|
| `history_db_path` | Path to the history database | "{mem0_dir}/history.db" |
|
|
| `version` | API version | "v1.0" |
|
|
| `custom_prompt` | Custom prompt for memory processing | None |
|
|
</Accordion>
|
|
|
|
<Accordion title="Complete Configuration Example">
|
|
```python
|
|
config = {
|
|
"vector_store": {
|
|
"provider": "qdrant",
|
|
"config": {
|
|
"host": "localhost",
|
|
"port": 6333
|
|
}
|
|
},
|
|
"llm": {
|
|
"provider": "openai",
|
|
"config": {
|
|
"api_key": "your-api-key",
|
|
"model": "gpt-4"
|
|
}
|
|
},
|
|
"embedder": {
|
|
"provider": "openai",
|
|
"config": {
|
|
"api_key": "your-api-key",
|
|
"model": "text-embedding-3-small"
|
|
}
|
|
},
|
|
"graph_store": {
|
|
"provider": "neo4j",
|
|
"config": {
|
|
"url": "neo4j+s://your-instance",
|
|
"username": "neo4j",
|
|
"password": "password"
|
|
}
|
|
},
|
|
"history_db_path": "/path/to/history.db",
|
|
"version": "v1.1",
|
|
"custom_prompt": "Optional custom prompt for memory processing"
|
|
}
|
|
```
|
|
</Accordion>
|
|
</AccordionGroup>
|
|
|
|
## Run Mem0 Locally
|
|
|
|
Please refer to the example [Mem0 with Ollama](../examples/mem0-with-ollama) to run Mem0 locally.
|
|
|
|
|
|
## Chat Completion
|
|
|
|
Mem0 can be easily integrated into chat applications to enhance conversational agents with structured memory. Mem0's APIs are designed to be compatible with OpenAI's, with the goal of making it easy to leverage Mem0 in applications you may have already built.
|
|
|
|
If you have a `Mem0 API key`, you can use it to initialize the client. Alternatively, you can initialize Mem0 without an API key if you're using it locally.
|
|
|
|
Mem0 supports several language models (LLMs) through integration with various [providers](https://litellm.vercel.app/docs/providers).
|
|
|
|
## Use Mem0 Platform
|
|
|
|
```python
|
|
from mem0.proxy.main import Mem0
|
|
|
|
client = Mem0(api_key="m0-xxx")
|
|
|
|
# First interaction: Storing user preferences
|
|
messages = [
|
|
{
|
|
"role": "user",
|
|
"content": "I love indian food but I cannot eat pizza since allergic to cheese."
|
|
},
|
|
]
|
|
user_id = "alice"
|
|
chat_completion = client.chat.completions.create(messages=messages, model="gpt-4o-mini", user_id=user_id)
|
|
# Memory saved after this will look like: "Loves Indian food. Allergic to cheese and cannot eat pizza."
|
|
|
|
# Second interaction: Leveraging stored memory
|
|
messages = [
|
|
{
|
|
"role": "user",
|
|
"content": "Suggest restaurants in San Francisco to eat.",
|
|
}
|
|
]
|
|
|
|
chat_completion = client.chat.completions.create(messages=messages, model="gpt-4o-mini", user_id=user_id)
|
|
print(chat_completion.choices[0].message.content)
|
|
# Answer: You might enjoy Indian restaurants in San Francisco, such as Amber India, Dosa, or Curry Up Now, which offer delicious options without cheese.
|
|
```
|
|
|
|
In this example, you can see how the second response is tailored based on the information provided in the first interaction. Mem0 remembers the user's preference for Indian food and their cheese allergy, using this information to provide more relevant and personalized restaurant suggestions in San Francisco.
|
|
|
|
### Use Mem0 OSS
|
|
|
|
```python
|
|
config = {
|
|
"vector_store": {
|
|
"provider": "qdrant",
|
|
"config": {
|
|
"host": "localhost",
|
|
"port": 6333,
|
|
}
|
|
},
|
|
}
|
|
|
|
client = Mem0(config=config)
|
|
|
|
chat_completion = client.chat.completions.create(
|
|
messages=[
|
|
{
|
|
"role": "user",
|
|
"content": "What's the capital of France?",
|
|
}
|
|
],
|
|
model="gpt-4o",
|
|
)
|
|
```
|
|
|
|
## APIs
|
|
|
|
Get started with using Mem0 APIs in your applications. For more details, refer to the [Platform](/platform/quickstart.mdx).
|
|
|
|
Here is an example of how to use Mem0 APIs:
|
|
|
|
```python
|
|
from mem0 import MemoryClient
|
|
client = MemoryClient(api_key="your-api-key") # get api_key from https://app.mem0.ai/
|
|
|
|
# Store messages
|
|
messages = [
|
|
{"role": "user", "content": "Hi, I'm Alex. I'm a vegetarian and I'm allergic to nuts."},
|
|
{"role": "assistant", "content": "Hello Alex! I've noted that you're a vegetarian and have a nut allergy. I'll keep this in mind for any food-related recommendations or discussions."}
|
|
]
|
|
result = client.add(messages, user_id="alex")
|
|
print(result)
|
|
|
|
# Retrieve memories
|
|
all_memories = client.get_all(user_id="alex")
|
|
print(all_memories)
|
|
|
|
# Search memories
|
|
query = "What do you know about me?"
|
|
related_memories = client.search(query, user_id="alex")
|
|
|
|
# Get memory history
|
|
history = client.history(memory_id="m1")
|
|
print(history)
|
|
```
|
|
|
|
|
|
## Contributing
|
|
|
|
We welcome contributions to Mem0! Here's how you can contribute:
|
|
|
|
1. Fork the repository and create your branch from `main`.
|
|
2. Clone the forked repository to your local machine.
|
|
3. Install the project dependencies:
|
|
|
|
```bash
|
|
poetry install
|
|
```
|
|
|
|
4. Install pre-commit hooks:
|
|
|
|
```bash
|
|
pip install pre-commit # If pre-commit is not already installed
|
|
pre-commit install
|
|
```
|
|
|
|
5. Make your changes and ensure they adhere to the project's coding standards.
|
|
|
|
6. Run the tests locally:
|
|
|
|
```bash
|
|
poetry run pytest
|
|
```
|
|
|
|
7. If all tests pass, commit your changes and push to your fork.
|
|
8. Open a pull request with a clear title and description.
|
|
|
|
Please make sure your code follows our coding conventions and is well-documented. We appreciate your contributions to make Mem0 better!
|
|
|
|
|
|
If you have any questions, please feel free to reach out to us using one of the following methods:
|
|
|
|
<Snippet file="get-help.mdx" /> |