Introduction
Standard LLMs have total amnesia—they reset every time you start a new session. But what if your AI could remember your name, your coding style, or your favorite movies? In this tutorial, we'll build exactly that using mem0 (the memory layer), OpenAI (the brain), and Qdrant (the storage).
Key Technologies
mem0: A library that acts as a "memory management" layer for AI.
Qdrant: A high-performance vector database to store memories.
OpenAI GPT-4o-mini: The LLM that processes our requests.
Step 1: The Setup (Docker)
First, we need a place to store our vector embeddings. We'll use Qdrant running in Docker.
docker-compose.yml
yaml
services:
vector-db:
image: qdrant/qdrant
ports:
- "6333:6333"
Run this with docker-compose up -d.
Step 2: The Python Code
Here’s the complete script (mem.py). It initializes the memory client, connects to Qdrant, and runs a chat loop.
Configuration: We configure mem0 to use OpenAI for embedding and generation, and Qdrant as the vector store.
config = {
"version": "v1.1",
"embedder": {
"provider": "openai",
"config": {
"api_key": OPENAI_API_KEY,
"model": "text-embedding-3-small"
}
},
"llm": {
"provider": "openai",
"config": {
"api_key": OPENAI_API_KEY,
"model": "gpt-4o-mini"
}
},
"vector_store": {
"provider": "qdrant",
"config": {
"host": "localhost",
"port": 6333,
}
}
}
The Loop: The core logic is simple:
Search: Fetch relevant memories for the user (mem_client.search).
Context: Inject these memories into the system prompt.
Chat: Send the prompt + user query to OpenAI.
Save: Store the new interaction back into memory (mem_client.add).
Conclusion
With just a few lines of configuration, you've transformed a stateless chatbot into a personalized assistant. The mem0 library handles the complex vector retrieval logic, letting you focus on building the product.
