Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate Mem0 for providing long-term memory for AI Agents #3389

Merged
merged 18 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
345 changes: 345 additions & 0 deletions notebook/agentchat_with_memory.ipynb
gagb marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,345 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "A3guIYLG6PnX"
},
"source": [
"<a href=\"https://colab.research.google.com/drive/1NZEwC9w6V2S6hYmK7l2SQ9jhQrG1uKk8?usp=sharing\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "di1opljX6sQT"
},
"source": [
"This notebook demonstrates an intelligent customer service chatbot system that combines:\n",
"\n",
"- PyAutoGen for conversational agents\n",
"- Mem0 for memory management\n",
"\n",
"[Mem0](https://www.mem0.ai/) 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](https://docs.mem0.ai/overview) for more information.\n",
"\n",
"The implementation showcases how to initialize agents, manage conversation memory, and facilitate multi-agent conversations for enhanced problem-solving in customer support scenarios."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "EAKSBJcLAf0h"
},
"source": [
"## Requirements\n",
"\n",
"- pyautogen\n",
"- mem0ai"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"collapsed": true,
"id": "zopsFIhY9mGu",
"outputId": "fad74532-0911-42f6-9076-c42bd670bc88"
},
"outputs": [],
"source": [
"!pip install pyautogen mem0ai"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "TA1-sGxD9-zZ"
},
"source": [
"## Get API Keys\n",
"\n",
"Please get `MEM0_API_KEY` from [Mem0 Platform](https://app.mem0.ai/)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "4nQWyJ-n9qOB",
"outputId": "de897f4f-32fd-4359-f021-c3510467b69c"
},
"outputs": [],
"source": [
"import os\n",
"\n",
"from mem0 import MemoryClient\n",
"\n",
"from autogen import ConversableAgent\n",
"\n",
"os.environ[\"OPENAI_API_KEY\"] = \"your_api_key\"\n",
"os.environ[\"MEM0_API_KEY\"] = \"your_api_key\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "6GAZAkGmAjsT"
},
"source": [
"## Initialize Agent and Memory\n",
"\n",
"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.\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"id": "xWSEHMKT9qLz"
},
"outputs": [],
"source": [
"agent = ConversableAgent(\n",
" \"chatbot\",\n",
" llm_config={\"config_list\": [{\"model\": \"gpt-4o\", \"api_key\": os.environ.get(\"OPENAI_API_KEY\")}]},\n",
" code_execution_config=False,\n",
" function_map=None,\n",
" human_input_mode=\"NEVER\",\n",
")\n",
"\n",
"memory = MemoryClient()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "c7vGQoMg_KhS"
},
"source": [
"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\"."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "b6ghSobs-mYo",
"outputId": "18a6e657-49ba-41cd-8e51-2e76cf668967"
},
"outputs": [
{
"data": {
"text/plain": [
"{'message': 'ok'}"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"conversation = [\n",
" {\n",
" \"role\": \"assistant\",\n",
" \"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?\",\n",
" },\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": 'Seeing horizontal lines on our tv. TV model: Sony - 77\" Class BRAVIA XR A80K OLED 4K UHD Smart Google TV',\n",
" },\n",
" {\n",
" \"role\": \"assistant\",\n",
" \"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.\",\n",
" },\n",
" {\n",
" \"role\": \"assistant\",\n",
" \"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?\",\n",
" },\n",
" {\"role\": \"user\", \"content\": \"[email protected]\"},\n",
" {\n",
" \"role\": \"assistant\",\n",
" \"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?\",\n",
" },\n",
" {\"role\": \"user\", \"content\": \"Order number: 112217629\"},\n",
" {\n",
" \"role\": \"assistant\",\n",
" \"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?\",\n",
" },\n",
" {\"role\": \"user\", \"content\": \"Yes please\"},\n",
" {\"role\": \"assistant\", \"content\": \"When should I schedule the appointment?\"},\n",
" {\"role\": \"user\", \"content\": \"Schedule it for tomorrow please\"},\n",
"]\n",
"\n",
"memory.add(messages=conversation, user_id=\"customer_service_bot\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "hlxxncxkAvck"
},
"source": [
"## Agent Inference\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "wYdDkuKc-Xc8",
"outputId": "b50f55d3-a280-4c0d-82e5-43fc0589109b"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sure, your order number is 112217629.\n"
]
}
],
"source": [
"data = \"I forgot the order numnber, can you quickly tell me?\"\n",
"\n",
"relevant_memories = memory.search(data, user_id=\"customer_service_bot\")\n",
"flatten_relevant_memories = \"\\n\".join([m[\"memory\"] for m in relevant_memories])\n",
"\n",
"prompt = f\"\"\"Answer the user question considering the memories. Keep answers clear and concise.\n",
"Memories:\n",
"{flatten_relevant_memories}\n",
"\\n\\n\n",
"Question: {data}\n",
"\"\"\"\n",
"\n",
"reply = agent.generate_reply(messages=[{\"content\": prompt, \"role\": \"user\"}])\n",
"print(reply)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "sryX0gfdBGGD"
},
"source": [
"## Multi Agent Conversation\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"id": "Vq5BFL2l-XZl"
},
"outputs": [],
"source": [
"manager = ConversableAgent(\n",
" \"manager\",\n",
" system_message=\"You are a manager who helps in resolving customer issues.\",\n",
" llm_config={\"config_list\": [{\"model\": \"gpt-4\", \"temperature\": 0, \"api_key\": os.environ.get(\"OPENAI_API_KEY\")}]},\n",
" human_input_mode=\"NEVER\",\n",
")\n",
"\n",
"customer_bot = ConversableAgent(\n",
" \"customer_bot\",\n",
" system_message=\"You are a customer service bot who gathers information on issues customers are facing. Keep answers clear and concise.\",\n",
" llm_config={\"config_list\": [{\"model\": \"gpt-4\", \"temperature\": 0, \"api_key\": os.environ.get(\"OPENAI_API_KEY\")}]},\n",
" human_input_mode=\"NEVER\",\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"id": "iX4ehmc6Fbib"
},
"outputs": [],
"source": [
"data = \"When is the appointment?\"\n",
"\n",
"relevant_memories = memory.search(data, user_id=\"customer_service_bot\")\n",
"flatten_relevant_memories = \"\\n\".join([m[\"memory\"] for m in relevant_memories])\n",
"\n",
"prompt = f\"\"\"\n",
"Context:\n",
"{flatten_relevant_memories}\n",
"\\n\\n\n",
"Question: {data}\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "D3paRZWvCIzt",
"outputId": "15eadb7c-5973-44f1-de43-5e6cdebe88de"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"manager (to customer_bot):\n",
"\n",
"\n",
"Context:\n",
"Scheduled an appointment for a home repair for tomorrow\n",
"Order number is 112217629\n",
"TV model is Sony - 77\" Class BRAVIA XR A80K OLED 4K UHD Smart Google TV\n",
"User's email address is [email protected]\n",
"\n",
"\n",
"\n",
"Question: When is the appointment?\n",
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"customer_bot (to manager):\n",
"\n",
"The appointment is scheduled for tomorrow.\n",
"\n",
"--------------------------------------------------------------------------------\n"
]
}
],
"source": [
"result = manager.send(prompt, customer_bot, request_reply=True)"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Loading
Loading