Memory-backed chat
This example demonstrates a practical memory stack:
- OpenAI GPT-5 is used as LLM.
- Mem0 stores and retrieves long-term memories extracted from the conversation.
- Qdrant is the vector database backend used by Mem0 for semantic retrieval.
The key takeaway is architectural: GPT-5 remains stateless between runs, while Mem0+Qdrant provides durable, searchable memory that can be injected back into the model’s context on each turn.
What you build
A CLI chat application that:
- Retrieves relevant long-term memories from Mem0/Qdrant for each user message
- Passes those memories to GPT-5 as part of the system instructions
- Stores the latest interaction back into Mem0 so it can influence future sessions
Prerequisites
- Python 3.10+
- An OpenAI API key (exported as
OPENAI_API_KEYenv variable) - A running Qdrant instance (e.g., using Docker)
Start Qdrant locally (Docker)
bash
docker run -p 6333:6333 -p 6334:6334 qdrant/qdrantQdrant will listen on localhost:6333 by default.
If you already run Qdrant elsewhere, set:
QDRANT_HOSTQDRANT_PORT
Setup
bash
python -m venv .venv
# macOS/Linux:
source .venv/bin/activate
# Windows Command Prompt:
.venv\Scripts\activate.bat
# Windows PowerShell:
.venv\Scripts\Activate.ps1
pip install -r requirements.txtCreate a .env file (optional):
bash
OPENAI_API_KEY=your_key_here
MODEL=gpt-5
QDRANT_HOST=localhost
QDRANT_PORT=6333
MEM0_COLLECTION=mem0_demoRun
bash
python mem0_chat.py --user aliceCommands
Inside the chat:
/help– show commands/memories– show a sample of stored memories for the user (best-effort)/forget– clear stored memories for the user (best-effort)/exit– quit
Suggested experiment
- Tell the assistant a stable preference (e.g., "When I ask for recommendations, I prefer a short list with pros/cons.").
- Exit the program and run it again.
- Ask a related question ("Recommend books for my next trip.").
- Observe that the assistant recalls and applies your preference, even though the in-session transcript is empty.
- Run
/memoriesto inspect what was stored.
Notes
- Mem0 performs extraction and conflict resolution when adding memories (default behavior).
- The script keeps a small in-session transcript (
--window) for local coherence; cross-session continuity comes from Mem0+Qdrant.