Mem0: Memory Layer for Personalized AI Applications
One of the biggest challenges in building AI applications is the ability to remember user context and preferences across sessions. By default, LLMs like GPT or Claude have no persistent memory. Every conversation starts from scratch. This is where Mem0 comes in as a solution.
Mem0 is an open-source memory layer that allows your AI applications to store, retrieve, and manage memories intelligently. With Mem0, your chatbot can remember user preferences, interaction history, and other important context.
Why Mem0?
Imagine you're building an AI assistant for customer service. Without a memory layer, every time a customer reaches out, the AI has to ask for basic information again. With Mem0:
- AI remembers customer names, preferences, and history
- Responses become more personal and contextual
- User experience improves dramatically
- Operational efficiency increases
Mem0 uses semantic search to find relevant memories, not just keyword matching. This means the AI can understand context and retrieve information that is truly relevant.
Installation
First, install Mem0 using pip:
pip install mem0ai
To use full features with a vector database, also install optional dependencies:
pip install mem0ai[qdrant]
You'll also need an API key from OpenAI (default) or another LLM provider:
export OPENAIAPIKEY="sk-your-api-key-here"
Initializing Mem0
There are two ways to use Mem0: an in-memory version for development and a persistent version for production.
Basic Setup (In-Memory)
from mem0 import Memory
Initialize with default configuration
m = Memory()
Setup with Qdrant (Persistent)
from mem0 import Memory
config = {
"vectorstore": {
"provider": "qdrant",
"config": {
"host": "localhost",
"port": 6333,
"collectionname": "mem0memories"
}
},
"llm": {
"provider": "openai",
"config": {
"model": "gpt-4o-mini",
"temperature": 0.1
}
},
"embedder": {
"provider": "openai",
"config": {
"model": "text-embedding-3-small"
}
}
}
m = Memory.fromconfig(config)
Basic Memory Operations
Mem0 provides four main operations: add, get, search, and delete.
Adding Memories (Add)
# Add a memory for a specific user
result = m.add(
"I like black coffee without sugar and usually drink it in the morning",
userid="user123"
)
print(result)
Output: {'results': [{'id': '...', 'memory': 'Likes black coffee without sugar', 'event': 'ADD'}]}
Mem0 automatically extracts important information from the given text. Note that Mem0 doesn't store raw text but rather extracts facts from it.
Retrieving All Memories (Get)
# Get all memories for a specific user
memories = m.getall(userid="user123")
for memory in memories:
print(f"ID: {memory['id']}")
print(f"Memory: {memory['memory']}")
print(f"Created: {memory['createdat']}")
print("---")
Searching Memories (Search)
# Semantic search - find relevant memories
results = m.search(
"What is this user's favorite drink?",
userid="user123"
)
for result in results:
print(f"Memory: {result['memory']}")
print(f"Score: {result['score']}")
Search uses semantic similarity, so the query "favorite drink" will find memories about "black coffee" even though the words are different.
Deleting Memories (Delete)
# Delete a specific memory by ID
m.delete(memoryid="memoryidhere")
Delete all memories for a user
m.deleteall(userid="user123")