Agent with memory using Mem0
This notebook demonstrates an intelligent customer service chatbot system that combines:
- AutoGen for conversational agents
- Mem0 for memory management
Mem0 provides a smart, self-improving memory layer for Large Language Models (LLMs), enabling developers to create personalized AI experiences that evolve with each user interaction. Refer docs for more information.
Mem0 uses a hybrid database approach, combining vector, key-value, and graph databases to efficiently store and retrieve different types of information. It associates memories with unique identifiers, extracts relevant facts and preferences when storing, and uses a sophisticated retrieval process that considers relevance, importance, and recency.
Key features of Mem0 include: 1. Comprehensive Memory Management: Easily manage long-term, short-term, semantic, and episodic memories for individual users, agents, and sessions through robust APIs. 2. Self-Improving Memory: An adaptive system that continuously learns from user interactions, refining its understanding over time. 3. Cross-Platform Consistency: Ensures a unified user experience across various AI platforms and applications. 4. Centralized Memory Control: Simplifies storing, updating, and deleting memories.
This approach allows for maintaining context across sessions, adaptive personalization, and dynamic updates, making it more powerful than traditional Retrieval-Augmented Generation (RAG) approaches for creating context-aware AI applications.
The implementation showcases how to initialize agents, manage conversation memory, and facilitate multi-agent conversations for enhanced problem-solving in customer support scenarios.
Requirements
Some extra dependencies are needed for this notebook, which can be installed via pip:
pip install autogen mem0ai
For more information, please refer to the installation guide.
Get API Keys
Please get MEM0_API_KEY
from Mem0 Platform.
import os
from mem0 import MemoryClient
from autogen import ConversableAgent
os.environ["OPENAI_API_KEY"] = "your_api_key"
os.environ["MEM0_API_KEY"] = "your_api_key"
Initialize Agent and Memory
The conversational agent is set up using the ‘gpt-4o’ model and a mem0 client. We’ll utilize the client’s methods for storing and accessing memories.
agent = ConversableAgent(
"chatbot",
llm_config={"config_list": [{"model": "gpt-4o", "api_key": os.environ.get("OPENAI_API_KEY")}]},
code_execution_config=False,
function_map=None,
human_input_mode="NEVER",
)
memory = MemoryClient()
Initialize a conversation history for a Best Buy customer service
chatbot. It contains a list of message exchanges between the user and
the assistant, structured as dictionaries with ‘role’ and ‘content’
keys. The entire conversation is then stored in memory using the
memory.add()
method, associated with the identifier
“customer_service_bot”.
conversation = [
{
"role": "assistant",
"content": "Hi, I'm Best Buy's chatbot!\n\nThanks for being a My Best Buy TotalTM member.\n\nWhat can I help you with?",
},
{
"role": "user",
"content": 'Seeing horizontal lines on our tv. TV model: Sony - 77" Class BRAVIA XR A80K OLED 4K UHD Smart Google TV',
},
{
"role": "assistant",
"content": "Thanks for being a My Best Buy Total™ member. I can connect you to an expert immediately - just one perk of your membership!\n\nSelect the button below when you're ready to chat.",
},
{
"role": "assistant",
"content": "Good evening, thank you for choosing Best Buy, Fnu. My name is Lovely. I hope you are doing well. I'm sorry to hear that you're seeing horizontal lines on your TV.\n\nI'm absolutely committed to exploring all possible ways to assist you to fix this issue.\n\nTo ensure that we are on the right account, may I please have your email address registered with your Best Buy account?",
},
{"role": "user", "content": "dd@gmail.com"},
{
"role": "assistant",
"content": "Perfect! Thank you for providing all the details, surely you have made my job easier by doing this. I really appreciate it.\n\nI also want to take a moment to express our heartfelt appreciation for your trust and loyalty. Thank you for being an amazing customer of BestBuy Total.\n\nCould you please help me with the order number or product's details to check it quickly?\n\nSamsung - 49\" Odyssey OLED G9 (G95SC) DQHD 240Hz 0.03ms G-Sync Compatible Curved Smart Gaming Monitor - Silver - just to confirm this is the item, right?",
},
{"role": "user", "content": "Order number: 112217629"},
{
"role": "assistant",
"content": "Superb! Thank you for confirmation.\n\nThank you for your patience. After exploring all possible solutions, I can help you to arrange a home repair appointment for your device. Our Geek Squad experts will visit your home to inspect and fix your device.\n\nIt's great that you have a protection plan - rest assured, we've got your back! As a valued Total member, you can avail this service at a minimal service fee. This fee, applicable to all repairs, covers the cost of diagnosing the issue and any small parts needed for the repair. It's part of our 24-month free protection plan.\n\nPlease click here to review the service fee and plan coverage details -\n\nhttps://www.bestbuy.com/site/best-buy-membership/best-buy-protection/pcmcat1608643232014.c?id=pcmcat1608643232014#jl-servicefees\n\nFnu - just to confirm shall I proceed to schedule the appointment?",
},
{"role": "user", "content": "Yes please"},
{"role": "assistant", "content": "When should I schedule the appointment?"},
{"role": "user", "content": "Schedule it for tomorrow please"},
]
memory.add(messages=conversation, user_id="customer_service_bot")
{'message': 'ok'}
Agent Inference
We ask a question to the agent, utilizing mem0 to retrieve relevant memories. The agent then formulates a response based on both the question and the retrieved contextual information.
data = "I forgot the order numnber, can you quickly tell me?"
relevant_memories = memory.search(data, user_id="customer_service_bot")
flatten_relevant_memories = "\n".join([m["memory"] for m in relevant_memories])
prompt = f"""Answer the user question considering the memories. Keep answers clear and concise.
Memories:
{flatten_relevant_memories}
\n\n
Question: {data}
"""
reply = agent.generate_reply(messages=[{"content": prompt, "role": "user"}])
print(reply)
Sure, your order number is 112217629.
Multi Agent Conversation
Initialize two AI agents: a “manager” for resolving customer issues and a “customer_bot” for gathering information on customer problems, both using GPT-4. It then retrieves relevant memories for a given question, combining them with the question into a prompt. This prompt can be used by either the manager or customer_bot to generate a contextually informed response.
manager = ConversableAgent(
"manager",
system_message="You are a manager who helps in resolving customer issues.",
llm_config={"config_list": [{"model": "gpt-4", "temperature": 0, "api_key": os.environ.get("OPENAI_API_KEY")}]},
human_input_mode="NEVER",
)
customer_bot = ConversableAgent(
"customer_bot",
system_message="You are a customer service bot who gathers information on issues customers are facing. Keep answers clear and concise.",
llm_config={"config_list": [{"model": "gpt-4", "temperature": 0, "api_key": os.environ.get("OPENAI_API_KEY")}]},
human_input_mode="NEVER",
)
data = "When is the appointment?"
relevant_memories = memory.search(data, user_id="customer_service_bot")
flatten_relevant_memories = "\n".join([m["memory"] for m in relevant_memories])
prompt = f"""
Context:
{flatten_relevant_memories}
\n\n
Question: {data}
"""
result = manager.send(prompt, customer_bot, request_reply=True)
Context:
Scheduled an appointment for a home repair for tomorrow
Order number is 112217629
TV model is Sony - 77" Class BRAVIA XR A80K OLED 4K UHD Smart Google TV
User's email address is dd@gmail.com
Question: When is the appointment?
--------------------------------------------------------------------------------
The appointment is scheduled for tomorrow.
--------------------------------------------------------------------------------