These memory instances persist across multiple sessions. Ideal for maintaining memory over long time spans.
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."}]# The default output_format is v1.0client.add(messages, user_id="alex", output_format="v1.0", version="v2")# To use the latest output_format, set the output_format parameter to "v1.1"client.add(messages, user_id="alex", output_format="v1.1", metadata={"food":"vegan"}, version="v2")
The add method offers support for two output formats: v1.0 (default) and v1.1. To enable the latest format, which provides enhanced detail for each memory operation, set the output_format parameter to v1.1.
Messages passed along with user_id, run_id, or app_id are stored as user memories, while messages from the assistant are excluded from memory. To store messages for the assistant, use agent_id exclusively and avoid including other IDs, such as user_id, alongside it. This ensures the memory is properly attributed to the assistant.
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.
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."}]# The default output_format is v1.0client.add(messages, user_id="alex", run_id="trip-planning-2024", output_format="v1.0", version="v2")# To use the latest output_format, set the output_format parameter to "v1.1"client.add(messages, user_id="alex", run_id="trip-planning-2024", output_format="v1.1", version="v2")
Add a memory layer for the assistants and agents so that their responses remain consistent across sessions.
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."}]# The default output_format is v1.0client.add(messages, agent_id="ai-tutor", output_format="v1.0", version="v2")# To use the latest output_format, set the output_format parameter to "v1.1"client.add(messages, agent_id="ai-tutor", output_format="v1.1", version="v2")
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 with both identifiers attached:
Each memory will be tagged with both the specified user_id and agent_id
During retrieval, you’ll need to provide both IDs to access the memories
This enables tracking the full context of conversations between specific users and agents
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", version="v2")
You can set an expiration date for memories, after which they will no longer be retrieved in searches. This is useful for creating temporary memories or memories that are only relevant for a specific time period.
import datetimemessages =[{"role":"user","content":"I'll be in San Francisco until August 31st."}]# Set an expiration date for this memoryclient.add(messages=messages, user_id="alex", expiration_date=str(datetime.datetime.now().date()+ datetime.timedelta(days=30)))# You can also use an explicit date stringclient.add(messages=messages, user_id="alex", expiration_date="2023-08-31")
Once a memory reaches its expiration date, it won’t be included in search or get results.
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:
query ="What should I cook for dinner today?"# The default output_format is v1.0client.search(query, user_id="alex", output_format="v1.0")# To use the latest output_format, set the output_format parameter to "v1.1"client.search(query, user_id="alex", output_format="v1.1")
Use category and metadata filters:
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). 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
query ="What do you know about me?"filters ={"AND":[{"user_id":"alex"},{"agent_id":{"in":["travel-assistant","customer-support"]}}]}client.search(query, version="v2", filters=filters)
Example 2: Search using date filters
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
query ="What do you know about me?"filters ={"AND":[{"metadata":{"food":"vegan"}},{"categories":{"contains":"food_preferences"}}]}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:
# 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). 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
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
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)
Get history of how a memory has changed over time.
# 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)
Manage memories using TypeScript with Mem0. Mem0 has completet TypeScript support Below is an example demonstrating how to add and search memories.
import MemoryClient,{ Message, SearchOptions, MemoryOptions }from'mem0ai';const apiKey ='your-api-key-here';const client =newMemoryClient(apiKey);// Messagesconst messages: Message[]=[{ 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."}];// ADDconst memoryOptions: MemoryOptions ={ user_id:"alex", agent_id:"travel-assistant"}client.add(messages, memoryOptions).then(result =>console.log(result)).catch(error =>console.error(error));// SEARCHconst query:string="What do you know about me?";const searchOptions: SearchOptions ={ user_id:"alex", filters:{OR:[{ agent_id:"travel-assistant"},{ user_id:"alex"}]}, threshold:0.1, api_version:'v2'}client.search(query, searchOptions).then(results =>console.log(results)).catch(error =>console.error(error));
If you have any questions, please feel free to reach out to us using one of the following methods: