Imagine customer support where you never repeat yourself, each interaction feels human, and your history is truly understood. Traditional support often leaves customers feeling like just another ticket number, but AI can change that. By combining advanced memory management with Mem0 and Qdrant’s vector storage, we’ll build an intelligent AI that remembers past conversations, understands context, and offers personalized solutions. This tutorial uses free tools so everyone can follow along without cost barriers.
Table of Content
- Introducing Mem0: AI Memory Management
- Understanding Qdrant: Vector Database
- Understanding Mem0 Workflow
- Implementing Customer Support AI with Mem0
- Analyzing AI Responses
Let’s start with understanding what Mem0 is in depth.
Introducing Mem0: AI Memory Management
In an era where AI interactions often feel disconnected and impersonal, Mem0 emerges as a groundbreaking memory management framework that fundamentally transforms how AI systems engage with users. Unlike traditional approaches that reset with each interaction, Mem0’s innovative hybrid database architecture enables AI applications to retain and build upon past conversations, creating truly personalized experiences that evolve over time.
This sophisticated framework doesn’t just store information, it understands context, learns from interactions, and adapts to user preferences, making it invaluable for applications ranging from customer support to virtual companions. By bridging the gap between short-term interactions and long-term memory, Mem0 empowers developers to create AI systems that deliver increasingly meaningful and natural user experiences.
Understanding Qdrant: Vector Database
In the evolving landscape of AI applications, vector databases are transforming how we handle complex data representations, and Qdrant stands at the forefront of this revolution. Unlike traditional databases that rely on rigid rows and columns, Qdrant excels at managing high-dimensional vector mathematical representations that capture the essence of images, text, and audio data.
This powerful vector similarity search engine provides developers with a sophisticated toolkit for storing and querying these abstract data representations, enabling advanced features like semantic search and intelligent recommendations. By efficiently managing vector embeddings and their associated payloads through a robust API, Qdrant empowers developers to create more intuitive, context-aware AI solutions that understand and process information in ways that mirror human cognition.
Understanding Mem0 Workflow
At the heart of Mem0’s sophisticated architecture lies a hybrid database strategy that changes how AI agents manage and retrieve long-term memories. Through unique identifier associations, Mem0 creates personalized memory spaces for each user or agent. When new information arrives via the add() method, the system intelligently distributes data across vector, key-value, and graph databases, optimizing storage and retrieval efficiency.
The search() method leverages this multi-faceted architecture to recall relevant memories, employing a scoring layer that evaluates both relevance and recency. These carefully selected memories are then seamlessly integrated into the AI’s prompt, enabling contextually rich responses. This dynamic system creates a continuous feedback loop between user interactions and memory updates, resulting in increasingly personalized and effective AI experiences.
Implementing Customer Support AI with Mem0
Step 1: Overview and Setting Up the Environment
Before diving into the code, ensure you have the necessary dependencies. You’ll need:
- mem0 for memory management.
- qdrant-client for vector database interactions.
- google.generativeai for using Gemini LLM.
Also, remember to store your API keys securely in environment variables. You can set them in your operating system or a .env file. Here’s how to access them in Python:
import os
# Load the API key from a secure environment variable
os.environ["GEMINI_API_KEY"] = os.getenv('GEMINI_API_KEY') # Set your API key securely
api_key1 = os.environ.get("GEMINI_API_KEY") # Retrieve the API key
Step 2: Define the Customer Support AI Agent Class
Next, create the CustomerSupportAIAgent class, which will manage memory and generate responses.
from mem0 import Memory
from qdrant_client import QdrantClient
import google.generativeai as genai
class CustomerSupportAIAgent:
def __init__(self):
"""
Initialize the CustomerSupportAIAgent with memory configuration and Gemini client.
"""
config = {
"vector_store": {
"provider": "qdrant",
"config": {
"embedding_model_dims": 768,
"url": "https://your-qdrant-url",
"api_key": "your-qdrant-api-key",
}
},
"llm": {
"provider": "litellm",
"config": {
"model": "gemini/gemini-1.5-flash",
},
},
"embedder": {
"provider": "gemini",
"config": {
"model": "models/text-embedding-004",
"embedding_dims": 768,
}
}
}
self.memory = Memory.from_config(config)
self.app_id = "customer-support"
- The vector_store config sets up the Qdrant database.
- The llm and embedder configs specify the use of the Gemini model for generating responses and embeddings.
Step 3: Handling Customer Queries
The handle_query method processes customer queries, generates responses, and stores interactions in memory.
def handle_query(self, query, customer_id):
"""
Handle a customer query, generate a response, and store relevant information in memory.
"""
# Generate a response for the query
response = self.generate_response(query, customer_id)
# Store the query and response in memory
self.memory.add(query, customer_id, metadata={"app_id": self.app_id, "response": response})
# Print the response
print("AI Agent:", response)
Step 4: Generating a Response with Gemini LLM
The generate_response method uses the Gemini model to create responses based on customer queries.
def generate_response(self, query, customer_id):
"""
Generate a response using Gemini LLM.
"""
try:
# Retrieve related memories
related_memories = self.memory.search(query, customer_id)
print("Related Memories:", related_memories)
# Get the queries for context, if available
recent_memories = [memory['memory'] for memory in related_memories if 'memory' in memory]
context = " ".join(recent_memories)
combined_input = f"Context: {context} Query: {query}"
# Generate response using the Gemini model
model = genai.GenerativeModel("models/gemini-1.5-flash")
response = model.generate_content(combined_input
return response.text
except Exception as e:
# Handle exceptions and return a fallback response
print(f"Error while generating response: {e}")
return "I'm sorry, but I encountered an error while processing your request."
The code retrieves related memories to provide context, enhancing the quality of the response generated by the LLM.
Step 5: Retrieving Customer Memory History
You can retrieve a list of stored interactions for a specific customer ID using the get_memories method.
def get_memories(self, customer_id):
"""
Retrieve all memories associated with the given customer ID.
"""
return self.memory.get_all(customer_id)
Step 6: Setting Up the Interactive Session
Finally, create an interactive session for users to engage with the AI agent.
# Instantiate the CustomerSupportAIAgent
support_agent = CustomerSupportAIAgent()
# Define a customer ID for the interactive session
customer_id = "jane_doe"
# Interactive session
print("Welcome to the AI-powered Customer Support! Type 'exit' to end the session or 'show memories' to see stored conversations.\n")
while True:
user_query = input("You: ")
# Check if user wants to exit or view memories
if user_query.lower() == 'exit':
print("Ending the session. Goodbye!")
break
elif user_query.lower() == 'show memories':
memories = support_agent.get_memories(customer_id)
for memory in memories:
print(memory['memory'])
else:
# Process the user query through the AI agent
support_agent.handle_query(user_query, customer_id)
The loop allows the user to input queries, view their past conversations, or exit the session.
Analyzing AI Responses
Welcome to the AI-powered Customer Support! Type 'exit' to end the session or 'show memories' to see stored conversations.
You: My parcel is over a week late, and I am furious! I needed it for an important event, and it ruined everything!
AI Agent: I understand your frustration. It's incredibly annoying when a package is late, especially when it's for an important event.
To help me understand the situation better and offer some helpful advice, please tell me:
* **Who is the shipping company?** (e.g., FedEx, UPS, USPS, etc.)
* **What was the expected delivery date?**
* **What kind of package is it?** (e.g., gift, important document, clothing, etc.)
* **Have you already contacted the shipping company?** If so, what did they say?
* **Have you tried tracking the package online?** What is the current status?
Knowing these details will help me give you the most relevant and helpful advice to resolve this issue.
You: FedEx, Delivery by 12th august, it was shoes, I havent contacted the shipping company, Status is showing shipping
AI Agent: Subject: Urgent - Package Delivery Delay - Tracking Number [Tracking Number]
Dear FedEx,
I am writing to express my extreme frustration and disappointment regarding the delay in delivery of my package with tracking number [Tracking Number]. The package, containing a pair of shoes, was due to arrive by August 12th, but it is now over a week late.
This delay has caused significant inconvenience and disruption, as I urgently needed the shoes for an important event on [Date of Event]. Unfortunately, the delay completely ruined my plans.
Currently, the tracking information shows the package is still "shipping," but I haven't received any updates or communication about the cause of the delay or a revised delivery date.
I kindly request your immediate attention to this matter. Please investigate the delay and provide me with a clear explanation and a definitive delivery date.
Sincerely,
[Your Name]
[Your Contact Information]
You: It better arrive soon! I can’t believe this happened.
AI Agent: It sounds like you're incredibly frustrated, and understandably so! It's really disappointing when a package is late, especially when it's crucial for an important event.
Here's what I suggest:
1. **Contact FedEx immediately.** Since it's their delivery, they can help you track the package and see what's causing the delay. You can also check if there are any updates on their website.
2. **Explain the urgency.** Let them know that you absolutely need the package by the 12th of August and that it was supposed to have been delivered by now. The more specific you are, the better.
3. **Consider alternative shipping options.** If FedEx can't guarantee delivery by the 12th, ask if there are any other options available, like express shipping.
4. **Keep records of all communication.** This will be helpful if you need to escalate the issue or file a claim later.
It's important to stay calm and be polite but firm when speaking with FedEx. I hope they can help you get your package on time!
You: can you summarize everything
AI Agent: The user ordered shoes that were supposed to arrive by August 12th via FedEx. The package is over a week late, and the shipping status still shows it as "shipping." The user is very upset because the shoes were needed for an important event, and they haven't contacted FedEx about the delay.
Thus, we have built a customer support AI capable of remembering all previous interactions and providing solutions. This capability gives the impression that we are interacting with a human agent.
All memories associated with the user can be accessed by using “show memories” as input.
Final Words
Building smarter AI systems isn’t just about processing power, it’s about creating experiences that feel genuinely human. By combining Mem0’s intelligent memory management with Qdrant’s vector search capabilities, we’ve developed a customer support AI that truly remembers, learns, and evolves. The results speak for themselves: more personalized responses, faster issue resolution, and significantly improved customer satisfaction. As we look ahead, this integration of memory and context in AI systems isn’t just an enhancement; it’s becoming essential for businesses that want to deliver exceptional customer experiences in an increasingly digital world.