📢 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.
Welcome to the Mem0 quickstart guide. This guide will help you get up and running with Mem0 in no time.
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 raw messages without inference# result = m.add(messages, user_id="alice", metadata={"category": "movie_recommendations"}, infer=False)
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.
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.
from mem0.proxy.main import Mem0client = Mem0(api_key="m0-xxx")# First interaction: Storing user preferencesmessages = [ { "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 memorymessages = [ { "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.
Get started with using Mem0 APIs in your applications. For more details, refer to the Platform.
Here is an example of how to use Mem0 APIs:
Copy
import osfrom mem0 import MemoryClientos.environ["MEM0_API_KEY"] = "your-api-key"client = MemoryClient() # get api_key from https://app.mem0.ai/# Store messagesmessages = [ {"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 memoriesall_memories = client.get_all(user_id="alex")print(all_memories)# Search memoriesquery = "What do you know about me?"related_memories = client.search(query, user_id="alex")# Get memory historyhistory = client.history(memory_id="m1")print(history)