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:
pip install mem0ai

Basic Usage

Initialize Mem0

import os
from mem0 import Memory

os.environ["OPENAI_API_KEY"] = "your-api-key"

m = Memory()

Store a Memory

messages = [
    {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
    {"role": "assistant", "content": "How about a thriller movies? 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."}
]

# Store inferred memories (default behavior)
result = m.add(messages, user_id="alice", metadata={"category": "movie_recommendations"})

# Store memories with agent and run context
result = m.add(messages, user_id="alice", agent_id="movie-assistant", run_id="session-001", metadata={"category": "movie_recommendations"})

# Store raw messages without inference
# result = m.add(messages, user_id="alice", metadata={"category": "movie_recommendations"}, infer=False)

Retrieve Memories

# Get all memories
all_memories = m.get_all(user_id="alice")

# Get a single memory by ID
specific_memory = m.get("892db2ae-06d9-49e5-8b3e-585ef9b85b8e")

Search Memories

related_memories = m.search(query="What do you know about me?", user_id="alice")

Update a Memory

result = m.update(memory_id="892db2ae-06d9-49e5-8b3e-585ef9b85b8e", data="I love India, it is my favorite country.")

Memory History

history = m.history(memory_id="892db2ae-06d9-49e5-8b3e-585ef9b85b8e")

Delete Memory

# Delete a memory by id
m.delete(memory_id="892db2ae-06d9-49e5-8b3e-585ef9b85b8e")
# Delete all memories for a user
m.delete_all(user_id="alice")

Reset Memory

m.reset() # Reset all memories

Advanced Memory Organization

Mem0 supports three key parameters for organizing memories:
  • user_id: Organize memories by user identity
  • agent_id: Organize memories by AI agent or assistant
  • run_id: Organize memories by session, workflow, or execution context

Using All Three Parameters

# Store memories with full context
m.add("User prefers vegetarian food", 
      user_id="alice", 
      agent_id="diet-assistant", 
      run_id="consultation-001")

# Retrieve memories with different scopes
all_user_memories = m.get_all(user_id="alice")
agent_memories = m.get_all(user_id="alice", agent_id="diet-assistant")
session_memories = m.get_all(user_id="alice", run_id="consultation-001")
specific_memories = m.get_all(user_id="alice", agent_id="diet-assistant", run_id="consultation-001")

# Search with context
general_search = m.search("What do you know about me?", user_id="alice")
agent_search = m.search("What do you know about me?", user_id="alice", agent_id="diet-assistant")
session_search = m.search("What do you know about me?", user_id="alice", run_id="consultation-001")

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.

Run Mem0 Locally

Please refer to the example 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.

Use Mem0 OSS

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",
)

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:
    poetry install
    
  4. Install pre-commit hooks:
    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:
    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: