📢 Announcing our research paper: Mem0 achieves 26% higher accuracy than OpenAI Memory, 91% lower latency, and 90% token savings! Read the paper to learn how we're revolutionizing AI agent memory.
🎉 Looking for TypeScript support? Mem0 has you covered! Check out an example here.
These memory instances persist across multiple sessions. Ideal for maintaining memory over long time spans.
Copy
Ask AI
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."}]client.add(messages, user_id="alex", metadata={"food": "vegan"})
When passing user_id, memories are primarily created based on user messages, but may be influenced by assistant messages for contextual understanding. For example, in a conversation about food preferences, both the user’s stated preferences and their responses to the assistant’s questions would form user memories. Similarly, when using agent_id, assistant messages are prioritized, but user messages might influence the agent’s memories based on context. This approach ensures comprehensive memory creation while maintaining appropriate attribution to either users or agents.
Example:
Copy
Ask AI
User: My favorite cuisine is ItalianAssistant: Nice! What about Indian cuisine?User: Don't like it much since I cannot eat spicy foodResulting user memories:memory1 - Likes Italian foodmemory2 - Doesn't like Indian food since cannot eat spicy (memory2 comes from user's response about Indian cuisine)
Metadata allows you to store structured information (location, timestamp, user state) with memories. Add it during creation to enable precise filtering and retrieval during searches.
These memory instances persist only for the duration of a user session. Ideal for non-repetitive interactions and managing context windows efficiently.
Copy
Ask AI
messages = [ {"role": "user", "content": "I'm planning a trip to Japan next month."}, {"role": "assistant", "content": "That's exciting, Alex! A trip to Japan next month sounds wonderful. Would you like some recommendations for vegetarian-friendly restaurants in Japan?"}, {"role": "user", "content": "Yes, please! Especially in Tokyo."}, {"role": "assistant", "content": "Great! I'll remember that you're interested in vegetarian restaurants in Tokyo for your upcoming trip. I'll prepare a list for you in our next interaction."}]client.add(messages, user_id="alex", run_id="trip-planning-2024")
Add a memory layer for the assistants and agents so that their responses remain consistent across sessions.
Copy
Ask AI
messages = [ {"role": "system", "content": "You are an AI tutor with a personality. Give yourself a name for the user."}, {"role": "assistant", "content": "Understood. I'm an AI tutor with a personality. My name is Alice."}]client.add(messages, agent_id="ai-tutor")
The agent_id retains memories exclusively based on messages generated by the assistant or those explicitly provided as input to the assistant. Messages outside these criteria are not stored as memory.
When you provide both user_id and agent_id, Mem0 will store memories for both identifiers separately:
Memories from messages with "role": "user" are automatically tagged with the provided user_id
Memories from messages with "role": "assistant" are automatically tagged with the provided agent_id
During retrieval, you can provide either user_id or agent_id to access the respective memories
You can continuously enrich existing memory collections by adding new memories to the same user_id or agent_id in subsequent API calls, either together or separately, allowing for progressive memory building over time
This dual-tagging approach enables personalized experiences for both users and AI agents in your application
Copy
Ask AI
messages = [ {"role": "user", "content": "I'm travelling to San Francisco"}, {"role": "assistant", "content": "That's great! I'm going to Dubai next month."},]client.add(messages=messages, user_id="user1", agent_id="agent1")
Pass user messages, interactions, and queries into our search method to retrieve relevant memories.
The search method supports two output formats: v1.0 (default) and v1.1. To use the latest format, which provides more detailed information about each memory operation, set the output_format parameter to v1.1:
Copy
Ask AI
query = "What should I cook for dinner today?"client.search(query, user_id="alex")
Use category and metadata filters:
Copy
Ask AI
query = "What do you know about me?"client.search(query, categories=["food_preferences"], metadata={"food": "vegan"})
Our advanced search allows you to set custom search filters. You can filter by user_id, agent_id, app_id, run_id, created_at, updated_at, categories, and text. The filters support logical operators (AND, OR) and comparison operators (in, gte, lte, gt, lt, ne, contains, icontains, ). The wildcard character () matches everything for a specific field. For more details, see V2 Search Memories.
Here you need to define version as v2 in the search method.
Example 1: Search using user_id and agent_id filters
Copy
Ask AI
query = "What do you know about me?"filters = { "OR":[ { "user_id":"alex" }, { "agent_id":{ "in":[ "travel-assistant", "customer-support" ] } } ]}client.search(query, version="v2", filters=filters)
Example 2: Search using date filters
Copy
Ask AI
query = "What do you know about me?"filters = { "AND": [ {"created_at": {"gte": "2024-07-20", "lte": "2024-07-10"}}, {"user_id": "alex"} ]}client.search(query, version="v2", filters=filters)
Example 3: Search using metadata and categories Filters
Copy
Ask AI
query = "What do you know about me?"filters = { "AND": [ {"metadata": {"food": "vegan"}}, { "categories":{ "contains": "food_preferences" } } ]}client.search(query, version="v2", filters=filters)
Example 4: Search using NOT filters
Copy
Ask AI
query = "What do you know about me?"filters = { "NOT": [ { "categories": { "contains": "food_preferences" } } ]}client.search(query, version="v2", filters=filters)
Example 5: Search using wildcard filters
Copy
Ask AI
query = "What do you know about me?"filters = { "AND": [ { "user_id": "alex" }, { "run_id": "*" # Matches all run_ids } ]}client.search(query, version="v2", filters=filters)
Fetch all memories for a user, agent, or run using the getAll() method.
The get_all method supports two output formats: v1.0 (default) and v1.1. To use the latest format, which provides more detailed information about each memory operation, set the output_format parameter to v1.1:
We’re soon deprecating the default output format for get_all() method, which returned a list. Once the changes are live, paginated response will be the only supported format, with 100 memories per page by default. You can customize this using the page and page_size parameters.
The following examples showcase the paginated output format.
You can filter memories by their categories when using get_all:
Copy
Ask AI
# Get memories with specific categoriesmemories = client.get_all(user_id="alex", categories=["likes"])# Get memories with multiple categoriesmemories = client.get_all(user_id="alex", categories=["likes", "food_preferences"])# Custom pagination with categoriesmemories = client.get_all(user_id="alex", categories=["likes"], page=1, page_size=50)# Get memories with specific keywordsmemories = client.get_all(user_id="alex", keywords="to play", page=1, page_size=50)
Our advanced retrieval allows you to set custom filters when fetching memories. You can filter by user_id, agent_id, app_id, run_id, created_at, updated_at, categories, and keywords. The filters support logical operators (AND, OR) and comparison operators (in, gte, lte, gt, lt, ne, contains, icontains, ). The wildcard character () matches everything for a specific field. For more details, see v2 Get Memories.
Here you need to define version as v2 in the get_all method.
Example 1. Get all memories using user_id and date filters
Copy
Ask AI
filters = { "AND":[ { "user_id":"alex" }, { "created_at":{ "gte":"2024-07-01", "lte":"2024-07-31" } }, { "categories":{ "contains": "food_preferences" } } ]}# Default (No Pagination)client.get_all(version="v2", filters=filters)# Pagination (You can also use the page and page_size parameters)client.get_all(version="v2", filters=filters, page=1, page_size=50)
Example 2: Search using metadata and categories Filters
Copy
Ask AI
filters = { "AND": [ {"metadata": {"food": "vegan"}}, { "categories":{ "contains": "food_preferences" } } ]}# Default (No Pagination)client.get_all(version="v2", filters=filters)# Pagination (You can also use the page and page_size parameters)client.get_all(version="v2", filters=filters, page=1, page_size=50)
Example 3: Get all memories using NOT filters
Copy
Ask AI
filters = { "NOT": [ { "categories": { "contains": "food_preferences" } } ]}# Default (No Pagination)client.get_all(version="v2", filters=filters)# Pagination (You can also use the page and page_size parameters)client.get_all(version="v2", filters=filters, page=1, page_size=50)
Example 4: Get all memories using wildcard filters
Copy
Ask AI
filters = { "AND": [ { "user_id": "alex" }, { "run_id": "*" # Matches all run_ids } ]}# Default (No Pagination)client.get_all(version="v2", filters=filters)# Pagination (You can also use the page and page_size parameters)client.get_all(version="v2", filters=filters, page=1, page_size=50)
Get history of how a memory has changed over time.
Copy
Ask AI
# Add some message to create historymessages = [{"role": "user", "content": "I recently tried chicken and I loved it. I'm thinking of trying more non-vegetarian dishes.."}]client.add(messages, user_id="alex")# Add second message to update historymessages.append({'role': 'user', 'content': 'I turned vegetarian now.'})client.add(messages, user_id="alex")# Get history of how memory changed over timememory_id = "<memory-id-here>"history = client.history(memory_id)