Skip to content

Commit

Permalink
Integrate Mem0 for providing long-term memory for AI Agents (microsof…
Browse files Browse the repository at this point in the history
…t#3389)

* Add Mem0 docs

* add notebook link

* add notebook

* formatting

* formatting fix

* resolve comments

* writeup for mem0

---------

Co-authored-by: HRUSHIKESH DOKALA <96101829+Hk669@users.noreply.github.com>
Co-authored-by: Li Jiang <bnujli@gmail.com>
Co-authored-by: Aristo <6344553+randombet@users.noreply.github.com>
Co-authored-by: gagb <gagb@users.noreply.github.com>
  • Loading branch information
5 people authored Sep 6, 2024
1 parent b97bca5 commit 1ff477f
Show file tree
Hide file tree
Showing 4 changed files with 1,441 additions and 924 deletions.
353 changes: 353 additions & 0 deletions notebook/agentchat_memory_using_mem0.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,353 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "A3guIYLG6PnX"
},
"source": [
"# Agent with memory using Mem0"
]
},
{
"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",
"\n",
"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.\n",
"\n",
"Key features of Mem0 include:\n",
"1. Comprehensive Memory Management: Easily manage long-term, short-term, semantic, and episodic memories for individual users, agents, and sessions through robust APIs.\n",
"2. Self-Improving Memory: An adaptive system that continuously learns from user interactions, refining its understanding over time.\n",
"3. Cross-Platform Consistency: Ensures a unified user experience across various AI platforms and applications.\n",
"4. Centralized Memory Control: Simplifies storing, updating, and deleting memories.\n",
"\n",
"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.\n",
"\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",
"````{=mdx}\n",
":::info Requirements\n",
"Some extra dependencies are needed for this notebook, which can be installed via pip:\n",
"\n",
"```bash\n",
"pip install pyautogen mem0ai\n",
"```\n",
"\n",
"For more information, please refer to the [installation guide](/docs/installation/).\n",
":::\n",
"````"
]
},
{
"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\": \"dd@gmail.com\"},\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 dd@gmail.com\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": {
"front_matter": {
"description": "Use Mem0 to create agents with memory.",
"tags": [
"memory"
]
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Loading

0 comments on commit 1ff477f

Please sign in to comment.