---
title: Autogen with Mem0
---

This guide demonstrates how to integrate AutoGen with Mem0 to create a conversational AI system with memory capabilities. The system includes a customer service bot and a manager agent, both leveraging Mem0 for context-aware interactions.

## Installation

First, install the required libraries:

```bash
pip install pyautogen mem0ai
```

## Setup

Import the necessary modules and set up your API keys:

```python
import os
from autogen import ConversableAgent
from mem0 import MemoryClient

os.environ["OPENAI_API_KEY"] = "your_openai_api_key_here"
os.environ["MEM0_API_KEY"] = "your_mem0_api_key_here"
```

## Initialize Agents and Memory

Create the conversational agent and Mem0 client:

```python
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(api_key=os.environ.get("MEM0_API_KEY"))
```

## Storing Conversations in Memory

You can store conversations in Mem0 for future reference:

<Accordion title="Conversation">
```python
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"
    }
]
```
</Accordion>
```python
memory.add(messages=conversation, user_id="customer_service_bot")
```

## Retrieving and Using Memory

When you need to answer a question, retrieve relevant memories and use them for context:

```python
data = "Which TV am I using?"

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.
Memories:
{flatten_relevant_memories}

Question: {data}
"""

reply = agent.generate_reply(messages=[{"content": prompt, "role": "user"}])
print(reply)
```

## Multi-Agent Conversation

You can create multiple agents for more complex interactions:

```python
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.",
    llm_config={"config_list": [{"model": "gpt-4", "temperature": 0, "api_key": os.environ.get("OPENAI_API_KEY")}]},
    human_input_mode="NEVER"
)

data = "What appointment is booked?"

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}

Question: {data}
"""

result = manager.send(prompt, customer_bot, request_reply=True)
```

This setup allows for a manager agent to interact with a customer service bot, both having access to the shared memory from Mem0.

## Conclusion

By integrating AutoGen with Mem0, you can create sophisticated conversational AI systems that maintain context across interactions. This approach is particularly useful for customer service applications, where understanding user history and preferences is crucial for providing personalized assistance.