Getting Started
Core Features
How-To Guides
Configuration
Troubleshooting
Integrations
Mem0 enhances your AI assistants and agents by providing an intelligent memory layer. This feature allows your AI to remember past interactions, user preferences, and adapt to individual needs over time. By adding memories, you enable your AI to have more personalized, consistent, and context-rich conversations, making it more effective for tasks like customer support, personalized recommendations, or adaptive workflows.
Essentially, adding memories teaches your AI about ongoing conversations and user-specific information, allowing it to learn and improve its responses based on historical data.
Before you can add memories, you need to initialize the Mem0 library and an OpenAI client (or another supported Large Language Model). This sets up the connection your application will use to interact with the memory system.
from openai import OpenAI
from mem0 import Memory
# Initialize your OpenAI client
openai_client = OpenAI()
# Initialize the Mem0 memory instance
memory = Memory()messages you provide.[{ "role": "user", "content": "..." }, { "role": "assistant", "content": "..." }]. Mem0 processes these messages to extract and store relevant memories.Here's a complete example demonstrating how to add memories within a continuous chat interaction:
from openai import OpenAI
from mem0 import Memory
openai_client = OpenAI()
memory
In this example, after the AI generates a response, the entire messages list (including the new assistant response) is passed to memory.add(). This ensures that Mem0 learns from the complete conversation turn, continuously updating its understanding and memory for that specific user.
Memories are typically added from a conversation's history. You'll need a list of messages, where each message is a dictionary containing a role (e.g., "user", "assistant", "system") and the content of that message.
For example, after a user asks a question and the AI responds, you would have a list like this:
# Example conversation history
messages = [
{"role": "system", "content": "You are a helpful AI."},
{"role": "user", "content": "What is the capital of France?"},
{"role": "assistant", "content": "The capital of France is Paris."}
]Once you have your messages and a user_id to identify the user, you can add them to Mem0's memory. This action processes the conversation and stores relevant information as memories associated with that user.
user_id = "your_unique_user_id_here" # Use a unique identifier for each user
# Add the conversation messages to memory
memory.add(messages, user_id=user_id)The user_id is crucial for personalization. Ensure you use a unique identifier for each user so Mem0 can correctly associate memories with the right individual. If you don't specify a user_id, a default one will be used, which might lead to mixed memories if multiple users interact without distinct IDs.
After adding memories, you can optionally search for them to confirm they have been stored. This helps you see what information Mem0 has retained.
# Search for relevant memories based on a query
relevant_memories = memory.search(query="What did I ask about France?", user_id=user_id, limit=3)
print("Relevant Memories:")
for entry in relevant_memories["results"]:
print(f"- {entry['memory']}")