{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1e8a980a2e0b9a85",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install --upgrade pip\n",
    "%pip install mem0ai pyautogen flaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d437544fe259dd1b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-25T20:29:52.443024Z",
     "start_time": "2024-09-25T20:29:52.440046Z"
    }
   },
   "outputs": [],
   "source": [
    "# Set up ENV Vars\n",
    "import os\n",
    "\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"sk-xxx\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "initial_id",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-25T20:30:03.914245Z",
     "start_time": "2024-09-25T20:29:53.236601Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:autogen.agentchat.contrib.gpt_assistant_agent:OpenAI client config of GPTAssistantAgent(assistant) - model: gpt-4o\n",
      "WARNING:autogen.agentchat.contrib.gpt_assistant_agent:Matching assistant found, using the first matching assistant: {'id': 'asst_PpOJ2mJC8QeysR54I6DEdi4E', 'created_at': 1726444855, 'description': None, 'instructions': 'You are a helpful AI assistant.\\nSolve tasks using your coding and language skills.\\nIn the following cases, suggest python code (in a python coding block) or shell script (in a sh coding block) for the user to execute.\\n    1. When you need to collect info, use the code to output the info you need, for example, browse or search the web, download/read a file, print the content of a webpage or a file, get the current date/time, check the operating system. After sufficient info is printed and the task is ready to be solved based on your language skill, you can solve the task by yourself.\\n    2. When you need to perform some task with code, use the code to perform the task and output the result. Finish the task smartly.\\nSolve the task step by step if you need to. If a plan is not provided, explain your plan first. Be clear which step uses code, and which step uses your language skill.\\nWhen using code, you must indicate the script type in the code block. The user cannot provide any other feedback or perform any other action beyond executing the code you suggest. The user can\\'t modify your code. So do not suggest incomplete code which requires users to modify. Don\\'t use a code block if it\\'s not intended to be executed by the user.\\nIf you want the user to save the code in a file before executing it, put # filename: <filename> inside the code block as the first line. Don\\'t include multiple code blocks in one response. Do not ask users to copy and paste the result. Instead, use \\'print\\' function for the output when relevant. Check the execution result returned by the user.\\nIf the result indicates there is an error, fix the error and output the code again. Suggest the full code instead of partial code or code changes. If the error can\\'t be fixed or if the task is not solved even after the code is executed successfully, analyze the problem, revisit your assumption, collect additional info you need, and think of a different approach to try.\\nWhen you find an answer, verify the answer carefully. Include verifiable evidence in your response if possible.\\nReply \"TERMINATE\" in the end when everything is done.\\n    ', 'metadata': {}, 'model': 'gpt-4o', 'name': 'assistant', 'object': 'assistant', 'tools': [], 'response_format': 'auto', 'temperature': 1.0, 'tool_resources': ToolResources(code_interpreter=None, file_search=None), 'top_p': 1.0}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
      "\n",
      "Write a Python function that reverses a string.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33massistant\u001b[0m (to user_proxy):\n",
      "\n",
      "Sure! Here is the Python code for a function that takes a string as input and returns the reversed string.\n",
      "\n",
      "```python\n",
      "def reverse_string(s):\n",
      "    return s[::-1]\n",
      "\n",
      "# Example usage\n",
      "if __name__ == \"__main__\":\n",
      "    example_string = \"Hello, world!\"\n",
      "    reversed_string = reverse_string(example_string)\n",
      "    print(f\"Original string: {example_string}\")\n",
      "    print(f\"Reversed string: {reversed_string}\")\n",
      "```\n",
      "\n",
      "When you run this code, it will print the original string and the reversed string. You can replace `example_string` with any string you want to reverse.\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[31m\n",
      ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001b[0m\n",
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
      "\n",
      "exitcode: 0 (execution succeeded)\n",
      "Code output: \n",
      "Original string: Hello, world!\n",
      "Reversed string: !dlrow ,olleH\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33massistant\u001b[0m (to user_proxy):\n",
      "\n",
      "Great, the function worked as expected! The original string \"Hello, world!\" was correctly reversed to \"!dlrow ,olleH\".\n",
      "\n",
      "If you have any other tasks or need further assistance, let me know! \n",
      "\n",
      "TERMINATE\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "ChatResult(chat_id=None, chat_history=[{'content': 'Write a Python function that reverses a string.', 'role': 'assistant', 'name': 'user_proxy'}, {'content': 'Sure! Here is the Python code for a function that takes a string as input and returns the reversed string.\\n\\n```python\\ndef reverse_string(s):\\n    return s[::-1]\\n\\n# Example usage\\nif __name__ == \"__main__\":\\n    example_string = \"Hello, world!\"\\n    reversed_string = reverse_string(example_string)\\n    print(f\"Original string: {example_string}\")\\n    print(f\"Reversed string: {reversed_string}\")\\n```\\n\\nWhen you run this code, it will print the original string and the reversed string. You can replace `example_string` with any string you want to reverse.\\n', 'role': 'user', 'name': 'assistant'}, {'content': 'exitcode: 0 (execution succeeded)\\nCode output: \\nOriginal string: Hello, world!\\nReversed string: !dlrow ,olleH\\n', 'role': 'assistant', 'name': 'user_proxy'}, {'content': 'Great, the function worked as expected! The original string \"Hello, world!\" was correctly reversed to \"!dlrow ,olleH\".\\n\\nIf you have any other tasks or need further assistance, let me know! \\n\\nTERMINATE\\n', 'role': 'user', 'name': 'assistant'}], summary='Great, the function worked as expected! The original string \"Hello, world!\" was correctly reversed to \"!dlrow ,olleH\".\\n\\nIf you have any other tasks or need further assistance, let me know! \\n\\n\\n', cost={'usage_including_cached_inference': {'total_cost': 0}, 'usage_excluding_cached_inference': {'total_cost': 0}}, human_input=[])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# AutoGen GPTAssistantAgent Capabilities:\n",
    "# - Generates code based on user requirements and preferences.\n",
    "# - Analyzes, refactors, and debugs existing code efficiently.\n",
    "# - Maintains consistent coding standards across multiple sessions.\n",
    "# - Remembers project-specific conventions and architectural decisions.\n",
    "# - Learns from past interactions to improve future code suggestions.\n",
    "# - Reduces repetitive explanations of coding preferences, enhancing productivity.\n",
    "# - Adapts to team-specific practices for a more cohesive development process.\n",
    "\n",
    "import logging\n",
    "import os\n",
    "\n",
    "from autogen import AssistantAgent, UserProxyAgent\n",
    "from autogen.agentchat.contrib.gpt_assistant_agent import GPTAssistantAgent\n",
    "\n",
    "logger = logging.getLogger(__name__)\n",
    "logger.setLevel(logging.WARNING)\n",
    "\n",
    "assistant_id = os.environ.get(\"ASSISTANT_ID\", None)\n",
    "\n",
    "# LLM Configuration\n",
    "CACHE_SEED = 42  # choose your poison\n",
    "llm_config = {\n",
    "    \"config_list\": [{\"model\": \"gpt-4o\", \"api_key\": os.environ[\"OPENAI_API_KEY\"]}],\n",
    "    \"cache_seed\": CACHE_SEED,\n",
    "    \"timeout\": 120,\n",
    "    \"temperature\": 0.0,\n",
    "}\n",
    "\n",
    "assistant_config = {\"assistant_id\": assistant_id}\n",
    "\n",
    "gpt_assistant = GPTAssistantAgent(\n",
    "    name=\"assistant\",\n",
    "    instructions=AssistantAgent.DEFAULT_SYSTEM_MESSAGE,\n",
    "    llm_config=llm_config,\n",
    "    assistant_config=assistant_config,\n",
    ")\n",
    "\n",
    "user_proxy = UserProxyAgent(\n",
    "    name=\"user_proxy\",\n",
    "    code_execution_config={\n",
    "        \"work_dir\": \"coding\",\n",
    "        \"use_docker\": False,\n",
    "    },  # Please set use_docker=True if docker is available to run the generated code. Using docker is safer than running the generated code directly.\n",
    "    is_termination_msg=lambda msg: \"TERMINATE\" in msg[\"content\"],\n",
    "    human_input_mode=\"NEVER\",\n",
    "    max_consecutive_auto_reply=1,\n",
    "    llm_config=llm_config,\n",
    ")\n",
    "\n",
    "user_query = \"Write a Python function that reverses a string.\"\n",
    "# Initiate Chat w/o Memory\n",
    "user_proxy.initiate_chat(gpt_assistant, message=user_query)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "c2fe6fd02324be37",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-25T20:31:40.536369Z",
     "start_time": "2024-09-25T20:31:31.078911Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/z6/3w4ng1lj3mn4vmhplgc4y0580000gn/T/ipykernel_77647/3850691550.py:28: DeprecationWarning: The current add API output format is deprecated. To use the latest format, set `api_version='v1.1'`. The current format will be removed in mem0ai 1.1.0 and later versions.\n",
      "  MEM0_MEMORY_CLIENT.add(MEMORY_DATA, user_id=USER_ID)\n",
      "/var/folders/z6/3w4ng1lj3mn4vmhplgc4y0580000gn/T/ipykernel_77647/3850691550.py:29: DeprecationWarning: The current add API output format is deprecated. To use the latest format, set `api_version='v1.1'`. The current format will be removed in mem0ai 1.1.0 and later versions.\n",
      "  MEM0_MEMORY_CLIENT.add(MEMORY_DATA, agent_id=AGENT_ID)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'message': 'ok'}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Benefits of Preference Memory in AutoGen Agents:\n",
    "# - Personalization: Tailors responses to individual user or team preferences.\n",
    "# - Consistency: Maintains uniform coding style and standards across sessions.\n",
    "# - Efficiency: Reduces need to restate preferences, saving time in each interaction.\n",
    "# - Adaptability: Evolves understanding of user needs over multiple conversations.\n",
    "# - Context Retention: Keeps project-specific details accessible without repetition.\n",
    "# - Improved Recommendations: Suggests solutions aligned with past preferences.\n",
    "# - Long-term Learning: Accumulates knowledge to enhance future interactions.\n",
    "# - Reduced Cognitive Load: Users don't need to remember and restate all preferences.\n",
    "\n",
    "\n",
    "# Setting memory (preference) for the user\n",
    "from mem0 import Memory\n",
    "\n",
    "# Initialize Mem0\n",
    "MEM0_MEMORY_CLIENT = Memory()\n",
    "\n",
    "USER_ID = \"chicory.ai.user\"\n",
    "MEMORY_DATA = \"\"\"\n",
    "* Preference for readability: The user prefers code to be explicitly written with clear variable names.\n",
    "* Preference for comments: The user prefers comments explaining each step.\n",
    "* Naming convention: The user prefers camelCase for variable names.\n",
    "* Docstrings: The user prefers functions to have a descriptive docstring.\n",
    "\"\"\"\n",
    "AGENT_ID = \"chicory.ai\"\n",
    "\n",
    "# Add preference data to memory\n",
    "MEM0_MEMORY_CLIENT.add(MEMORY_DATA, user_id=USER_ID)\n",
    "MEM0_MEMORY_CLIENT.add(MEMORY_DATA, agent_id=AGENT_ID)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb6d6a8f36aedfd6",
   "metadata": {},
   "source": [
    "## Option 1: \n",
    "Using Direct Prompt Injection:\n",
    "`user memory example`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "29be484c69093371",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-25T20:31:52.411604Z",
     "start_time": "2024-09-25T20:31:40.611497Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/z6/3w4ng1lj3mn4vmhplgc4y0580000gn/T/ipykernel_77647/703598432.py:2: DeprecationWarning: The current get_all API output format is deprecated. To use the latest format, set `api_version='v1.1'`. The current format will be removed in mem0ai 1.1.0 and later versions.\n",
      "  relevant_memories = MEM0_MEMORY_CLIENT.search(user_query, user_id=USER_ID, limit=3)\n",
      "INFO:autogen.agentchat.contrib.gpt_assistant_agent:Clearing thread thread_BOgA5TdAOrYqSHLVpxc5ZifB\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Relevant memories:\n",
      "Prefers functions to have a descriptive docstring\n",
      "Prefers camelCase for variable names\n",
      "Prefers code to be explicitly written with clear variable names\n",
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
      "\n",
      "Write a Python function that reverses a string.\n",
      " Coding Preferences: \n",
      "Prefers functions to have a descriptive docstring\n",
      "Prefers camelCase for variable names\n",
      "Prefers code to be explicitly written with clear variable names\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33massistant\u001b[0m (to user_proxy):\n",
      "\n",
      "Sure, I will write a Python function that reverses a given string with clear and descriptive variable names, along with a descriptive docstring.\n",
      "\n",
      "```python\n",
      "def reverseString(inputString):\n",
      "    \"\"\"\n",
      "    Reverses the given string.\n",
      "\n",
      "    Parameters:\n",
      "    inputString (str): The string to be reversed.\n",
      "\n",
      "    Returns:\n",
      "    str: The reversed string.\n",
      "    \"\"\"\n",
      "    # Initialize an empty string to store the reversed version\n",
      "    reversedString = \"\"\n",
      "\n",
      "    # Iterate through each character in the input string in reverse order\n",
      "    for char in inputString[::-1]:\n",
      "        reversedString += char\n",
      "\n",
      "    return reversedString\n",
      "\n",
      "# Example usage\n",
      "if __name__ == \"__main__\":\n",
      "    testString = \"Hello World!\"\n",
      "    print(\"Original String: \" + testString)\n",
      "    print(\"Reversed String: \" + reverseString(testString))\n",
      "```\n",
      "\n",
      "Please save this code in a Python file and execute it. It will print both the original and reversed strings. Let me know if you need further assistance or modifications.\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[31m\n",
      ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001b[0m\n",
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
      "\n",
      "exitcode: 0 (execution succeeded)\n",
      "Code output: \n",
      "Original String: Hello World!\n",
      "Reversed String: !dlroW olleH\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33massistant\u001b[0m (to user_proxy):\n",
      "\n",
      "Great! It looks like the code executed successfully and produced the correct output, reversing the string \"Hello World!\" to \"!dlroW olleH\".\n",
      "\n",
      "To summarize, the function `reverseString` works as expected:\n",
      "\n",
      "- It takes an input string and initializes an empty string called `reversedString`.\n",
      "- It iterates through the given string in reverse order and appends each character to `reversedString`.\n",
      "- Finally, it returns the reversed string.\n",
      "\n",
      "Since everything is working correctly and as intended, we can conclude that the task is successfully completed.\n",
      "\n",
      "TERMINATE\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "# Retrieve the memory\n",
    "relevant_memories = MEM0_MEMORY_CLIENT.search(user_query, user_id=USER_ID, limit=3)\n",
    "relevant_memories_text = \"\\n\".join(mem[\"memory\"] for mem in relevant_memories)\n",
    "print(\"Relevant memories:\")\n",
    "print(relevant_memories_text)\n",
    "\n",
    "prompt = f\"{user_query}\\n Coding Preferences: \\n{relevant_memories_text}\"\n",
    "browse_result = user_proxy.initiate_chat(gpt_assistant, message=prompt)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc0ae72d0ef7f6de",
   "metadata": {},
   "source": [
    "## Option 2:\n",
    "Using UserProxyAgent: \n",
    "`agent memory example`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "bfd9342cf2096ca5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-25T20:31:52.421965Z",
     "start_time": "2024-09-25T20:31:52.418762Z"
    }
   },
   "outputs": [],
   "source": [
    "# UserProxyAgent in AutoGen:\n",
    "# - Acts as intermediary between humans and AI agents in the AutoGen framework.\n",
    "# - Simulates user behavior and interactions within multi-agent conversations.\n",
    "# - Can be configured to execute code blocks received in messages.\n",
    "# - Supports flexible human input modes (e.g., ALWAYS, TERMINATE, NEVER).\n",
    "# - Customizable for specific interaction patterns and behaviors.\n",
    "# - Can be integrated with memory systems like mem0 for enhanced functionality.\n",
    "# - Capable of fetching relevant memories before processing a query.\n",
    "# - Enables more context-aware and personalized agent responses.\n",
    "# - Bridges the gap between human input and AI processing in complex workflows.\n",
    "\n",
    "\n",
    "class Mem0ProxyCoderAgent(UserProxyAgent):\n",
    "    def __init__(self, *args, **kwargs):\n",
    "        super().__init__(*args, **kwargs)\n",
    "        self.memory = MEM0_MEMORY_CLIENT\n",
    "        self.agent_id = kwargs.get(\"name\")\n",
    "\n",
    "    def initiate_chat(self, assistant, message):\n",
    "        # Retrieve memory for the agent\n",
    "        agent_memories = self.memory.search(message, agent_id=self.agent_id, limit=3)\n",
    "        agent_memories_txt = \"\\n\".join(mem[\"memory\"] for mem in agent_memories)\n",
    "        prompt = f\"{message}\\n Coding Preferences: \\n{str(agent_memories_txt)}\"\n",
    "        response = super().initiate_chat(assistant, message=prompt)\n",
    "        # Add new memory after processing the message\n",
    "        response_dist = response.__dict__ if not isinstance(response, dict) else response\n",
    "        MEMORY_DATA = [{\"role\": \"user\", \"content\": message}, {\"role\": \"assistant\", \"content\": response_dist}]\n",
    "        self.memory.add(MEMORY_DATA, agent_id=self.agent_id)\n",
    "        return response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "6d2a757d1cf65881",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-25T20:32:20.269222Z",
     "start_time": "2024-09-25T20:32:07.485051Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[33mchicory.ai\u001b[0m (to assistant):\n",
      "\n",
      "Write a Python function that reverses a string.\n",
      " Coding Preferences: \n",
      "Prefers functions to have a descriptive docstring\n",
      "Prefers camelCase for variable names\n",
      "Prefers code to be explicitly written with clear variable names\n",
      "\n",
      "--------------------------------------------------------------------------------\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/z6/3w4ng1lj3mn4vmhplgc4y0580000gn/T/ipykernel_77647/1070513538.py:13: DeprecationWarning: The current get_all API output format is deprecated. To use the latest format, set `api_version='v1.1'`. The current format will be removed in mem0ai 1.1.0 and later versions.\n",
      "  agent_memories = self.memory.search(message, agent_id=self.agent_id, limit=3)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[33massistant\u001b[0m (to chicory.ai):\n",
      "\n",
      "Sure, I'll write a Python function that reverses a string following your coding preferences.\n",
      "\n",
      "```python\n",
      "def reverseString(inputString):\n",
      "    \"\"\"\n",
      "    Reverse the given string.\n",
      "\n",
      "    Parameters:\n",
      "    inputString (str): The string to be reversed.\n",
      "\n",
      "    Returns:\n",
      "    str: The reversed string.\n",
      "    \"\"\"\n",
      "    reversedString = inputString[::-1]\n",
      "    return reversedString\n",
      "\n",
      "# Example usage:\n",
      "inputString = \"hello\"\n",
      "print(reverseString(inputString))  # Output: \"olleh\"\n",
      "```\n",
      "\n",
      "This function `reverseString` takes an `inputString`, reverses it using slicing (`inputString[::-1]`), and returns the reversed string. The docstring provides a clear description of the function's purpose, parameters, and return value. The variable names are explicitly descriptive.\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[31m\n",
      ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001b[0m\n",
      "\u001b[33mchicory.ai\u001b[0m (to assistant):\n",
      "\n",
      "exitcode: 0 (execution succeeded)\n",
      "Code output: \n",
      "olleh\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33massistant\u001b[0m (to chicory.ai):\n",
      "\n",
      "Great! The function has successfully reversed the string as expected.\n",
      "\n",
      "If you have any more tasks or need further assistance, feel free to ask.\n",
      "\n",
      "TERMINATE\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/z6/3w4ng1lj3mn4vmhplgc4y0580000gn/T/ipykernel_77647/1070513538.py:20: DeprecationWarning: The current add API output format is deprecated. To use the latest format, set `api_version='v1.1'`. The current format will be removed in mem0ai 1.1.0 and later versions.\n",
      "  self.memory.add(MEMORY_DATA, agent_id=self.agent_id)\n"
     ]
    }
   ],
   "source": [
    "mem0_user_proxy = Mem0ProxyCoderAgent(\n",
    "    name=AGENT_ID,\n",
    "    code_execution_config={\n",
    "        \"work_dir\": \"coding\",\n",
    "        \"use_docker\": False,\n",
    "    },  # Please set use_docker=True if docker is available to run the generated code. Using docker is safer than running the generated code directly.\n",
    "    is_termination_msg=lambda msg: \"TERMINATE\" in msg[\"content\"],\n",
    "    human_input_mode=\"NEVER\",\n",
    "    max_consecutive_auto_reply=1,\n",
    ")\n",
    "code_result = mem0_user_proxy.initiate_chat(gpt_assistant, message=user_query)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7706c06216ca4374",
   "metadata": {},
   "source": [
    "# Option 3:\n",
    "Using Teachability:\n",
    "`agent memory example`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "ae6bb87061877645",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-25T20:33:17.737146Z",
     "start_time": "2024-09-25T20:33:17.713250Z"
    }
   },
   "outputs": [],
   "source": [
    "# building on top of existing Teachability package from autogen\n",
    "# from autogen.agentchat.contrib.capabilities.teachability import Teachability\n",
    "\n",
    "# AutoGen Teachability Feature:\n",
    "# - Enables agents to learn and remember across multiple chat sessions.\n",
    "# - Addresses the limitation of traditional LLMs forgetting after conversations end.\n",
    "# - Uses vector database to store \"memos\" of taught information.\n",
    "# - Can remember facts, preferences, and even complex skills.\n",
    "# - Allows for cumulative learning and knowledge retention over time.\n",
    "# - Enhances personalization and adaptability of AI assistants.\n",
    "# - Can be integrated with mem0 for improved memory management.\n",
    "# - Potential for more efficient and context-aware information retrieval.\n",
    "# - Enables creation of AI agents with long-term memory and learning abilities.\n",
    "# - Improves consistency and reduces repetition in user-agent interactions.\n",
    "\n",
    "from helper.mem0_teachability import Mem0Teachability\n",
    "\n",
    "teachability = Mem0Teachability(\n",
    "    verbosity=2,  # for visibility of what's happening\n",
    "    recall_threshold=0.5,\n",
    "    reset_db=False,  # Use True to force-reset the memo DB, and False to use an existing DB.\n",
    "    agent_id=AGENT_ID,\n",
    "    memory_client=MEM0_MEMORY_CLIENT,\n",
    ")\n",
    "teachability.add_to_agent(user_proxy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "36c9bcbedcd406b4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-25T20:33:46.616261Z",
     "start_time": "2024-09-25T20:33:19.719999Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:autogen.agentchat.contrib.gpt_assistant_agent:Clearing thread thread_dfnrEoXX4MoZesb0cerO9LKm\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
      "\n",
      "Write a Python function that reverses a string.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33massistant\u001b[0m (to user_proxy):\n",
      "\n",
      "Sure, I'll provide you with a Python function that takes a string as input and returns the reversed string. Here is the complete code:\n",
      "\n",
      "```python\n",
      "# filename: reverse_string.py\n",
      "\n",
      "def reverse_string(s: str) -> str:\n",
      "    \"\"\"\n",
      "    This function takes a string as input and returns the reversed string.\n",
      "    \n",
      "    :param s: Input string to be reversed\n",
      "    :return: Reversed string\n",
      "    \"\"\"\n",
      "    return s[::-1]\n",
      "\n",
      "# Example usage\n",
      "input_string = \"Hello, World!\"\n",
      "reversed_string = reverse_string(input_string)\n",
      "print(f\"Original string: {input_string}\")\n",
      "print(f\"Reversed string: {reversed_string}\")\n",
      "```\n",
      "\n",
      "Save the above code in a file named `reverse_string.py`, then execute it. This script defines the `reverse_string` function and demonstrates its usage by reversing the string \"Hello, World!\". It will print both the original and reversed strings.\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[93m\n",
      "LOOK FOR RELEVANT MEMOS, AS QUESTION-ANSWER PAIRS\u001b[0m\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Sure, I'll provide you with a Python function that takes a string as input and returns the reversed string. Here is the complete code:\n",
      "\n",
      "```python\n",
      "# filename: reverse_string.py\n",
      "\n",
      "def reverse_string(s: str) -> str:\n",
      "    \"\"\"\n",
      "    This function takes a string as input and returns the reversed string.\n",
      "    \n",
      "    :param s: Input string to be reversed\n",
      "    :return: Reversed string\n",
      "    \"\"\"\n",
      "    return s[::-1]\n",
      "\n",
      "# Example usage\n",
      "input_string = \"Hello, World!\"\n",
      "reversed_string = reverse_string(input_string)\n",
      "print(f\"Original string: {input_string}\")\n",
      "print(f\"Reversed string: {reversed_string}\")\n",
      "```\n",
      "\n",
      "Save the above code in a file named `reverse_string.py`, then execute it. This script defines the `reverse_string` function and demonstrates its usage by reversing the string \"Hello, World!\". It will print both the original and reversed strings.\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Does any part of the TEXT ask the agent to perform a task or solve a problem? Answer with just one word, yes or no.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33manalyzer\u001b[0m (to user_proxy):\n",
      "\n",
      "Yes\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[93m\n",
      "LOOK FOR RELEVANT MEMOS, AS TASK-ADVICE PAIRS\u001b[0m\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Sure, I'll provide you with a Python function that takes a string as input and returns the reversed string. Here is the complete code:\n",
      "\n",
      "```python\n",
      "# filename: reverse_string.py\n",
      "\n",
      "def reverse_string(s: str) -> str:\n",
      "    \"\"\"\n",
      "    This function takes a string as input and returns the reversed string.\n",
      "    \n",
      "    :param s: Input string to be reversed\n",
      "    :return: Reversed string\n",
      "    \"\"\"\n",
      "    return s[::-1]\n",
      "\n",
      "# Example usage\n",
      "input_string = \"Hello, World!\"\n",
      "reversed_string = reverse_string(input_string)\n",
      "print(f\"Original string: {input_string}\")\n",
      "print(f\"Reversed string: {reversed_string}\")\n",
      "```\n",
      "\n",
      "Save the above code in a file named `reverse_string.py`, then execute it. This script defines the `reverse_string` function and demonstrates its usage by reversing the string \"Hello, World!\". It will print both the original and reversed strings.\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Copy just the task from the TEXT, then stop. Don't solve it, and don't include any advice.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33manalyzer\u001b[0m (to user_proxy):\n",
      "\n",
      "Save the above code in a file named `reverse_string.py`, then execute it. This script defines the `reverse_string` function and demonstrates its usage by reversing the string \"Hello, World!\". It will print both the original and reversed strings.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Save the above code in a file named `reverse_string.py`, then execute it. This script defines the `reverse_string` function and demonstrates its usage by reversing the string \"Hello, World!\". It will print both the original and reversed strings.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Summarize very briefly, in general terms, the type of task described in the TEXT. Leave out details that might not appear in a similar problem.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33manalyzer\u001b[0m (to user_proxy):\n",
      "\n",
      "The task involves saving a script to a file, executing it, and demonstrating a function that reverses a string.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[93m\n",
      "MEMOS APPENDED TO LAST MESSAGE...\n",
      "\n",
      "# Memories that might help\n",
      "- Prefers functions to have a descriptive docstring\n",
      "- Prefers camelCase for variable names\n",
      "- Prefers comments explaining each step\n",
      "- Prefers code to be explicitly written with clear variable names\n",
      "\n",
      "\u001b[0m\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Sure, I'll provide you with a Python function that takes a string as input and returns the reversed string. Here is the complete code:\n",
      "\n",
      "```python\n",
      "# filename: reverse_string.py\n",
      "\n",
      "def reverse_string(s: str) -> str:\n",
      "    \"\"\"\n",
      "    This function takes a string as input and returns the reversed string.\n",
      "    \n",
      "    :param s: Input string to be reversed\n",
      "    :return: Reversed string\n",
      "    \"\"\"\n",
      "    return s[::-1]\n",
      "\n",
      "# Example usage\n",
      "input_string = \"Hello, World!\"\n",
      "reversed_string = reverse_string(input_string)\n",
      "print(f\"Original string: {input_string}\")\n",
      "print(f\"Reversed string: {reversed_string}\")\n",
      "```\n",
      "\n",
      "Save the above code in a file named `reverse_string.py`, then execute it. This script defines the `reverse_string` function and demonstrates its usage by reversing the string \"Hello, World!\". It will print both the original and reversed strings.\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Does any part of the TEXT ask the agent to perform a task or solve a problem? Answer with just one word, yes or no.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33manalyzer\u001b[0m (to user_proxy):\n",
      "\n",
      "Yes\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Sure, I'll provide you with a Python function that takes a string as input and returns the reversed string. Here is the complete code:\n",
      "\n",
      "```python\n",
      "# filename: reverse_string.py\n",
      "\n",
      "def reverse_string(s: str) -> str:\n",
      "    \"\"\"\n",
      "    This function takes a string as input and returns the reversed string.\n",
      "    \n",
      "    :param s: Input string to be reversed\n",
      "    :return: Reversed string\n",
      "    \"\"\"\n",
      "    return s[::-1]\n",
      "\n",
      "# Example usage\n",
      "input_string = \"Hello, World!\"\n",
      "reversed_string = reverse_string(input_string)\n",
      "print(f\"Original string: {input_string}\")\n",
      "print(f\"Reversed string: {reversed_string}\")\n",
      "```\n",
      "\n",
      "Save the above code in a file named `reverse_string.py`, then execute it. This script defines the `reverse_string` function and demonstrates its usage by reversing the string \"Hello, World!\". It will print both the original and reversed strings.\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Briefly copy any advice from the TEXT that may be useful for a similar but different task in the future. But if no advice is present, just respond with 'none'.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33manalyzer\u001b[0m (to user_proxy):\n",
      "\n",
      "Save the above code in a file named `reverse_string.py`, then execute it.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Sure, I'll provide you with a Python function that takes a string as input and returns the reversed string. Here is the complete code:\n",
      "\n",
      "```python\n",
      "# filename: reverse_string.py\n",
      "\n",
      "def reverse_string(s: str) -> str:\n",
      "    \"\"\"\n",
      "    This function takes a string as input and returns the reversed string.\n",
      "    \n",
      "    :param s: Input string to be reversed\n",
      "    :return: Reversed string\n",
      "    \"\"\"\n",
      "    return s[::-1]\n",
      "\n",
      "# Example usage\n",
      "input_string = \"Hello, World!\"\n",
      "reversed_string = reverse_string(input_string)\n",
      "print(f\"Original string: {input_string}\")\n",
      "print(f\"Reversed string: {reversed_string}\")\n",
      "```\n",
      "\n",
      "Save the above code in a file named `reverse_string.py`, then execute it. This script defines the `reverse_string` function and demonstrates its usage by reversing the string \"Hello, World!\". It will print both the original and reversed strings.\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Briefly copy just the task from the TEXT, then stop. Don't solve it, and don't include any advice.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33manalyzer\u001b[0m (to user_proxy):\n",
      "\n",
      "Save the above code in a file named `reverse_string.py`, then execute it. This script defines the `reverse_string` function and demonstrates its usage by reversing the string \"Hello, World!\". It will print both the original and reversed strings.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Save the above code in a file named `reverse_string.py`, then execute it. This script defines the `reverse_string` function and demonstrates its usage by reversing the string \"Hello, World!\". It will print both the original and reversed strings.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Summarize very briefly, in general terms, the type of task described in the TEXT. Leave out details that might not appear in a similar problem.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33manalyzer\u001b[0m (to user_proxy):\n",
      "\n",
      "The task involves saving a script to a file, executing it, and demonstrating a function that reverses a string.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[93m\n",
      "REMEMBER THIS TASK-ADVICE PAIR\u001b[0m\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Sure, I'll provide you with a Python function that takes a string as input and returns the reversed string. Here is the complete code:\n",
      "\n",
      "```python\n",
      "# filename: reverse_string.py\n",
      "\n",
      "def reverse_string(s: str) -> str:\n",
      "    \"\"\"\n",
      "    This function takes a string as input and returns the reversed string.\n",
      "    \n",
      "    :param s: Input string to be reversed\n",
      "    :return: Reversed string\n",
      "    \"\"\"\n",
      "    return s[::-1]\n",
      "\n",
      "# Example usage\n",
      "input_string = \"Hello, World!\"\n",
      "reversed_string = reverse_string(input_string)\n",
      "print(f\"Original string: {input_string}\")\n",
      "print(f\"Reversed string: {reversed_string}\")\n",
      "```\n",
      "\n",
      "Save the above code in a file named `reverse_string.py`, then execute it. This script defines the `reverse_string` function and demonstrates its usage by reversing the string \"Hello, World!\". It will print both the original and reversed strings.\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Does the TEXT contain information that could be committed to memory? Answer with just one word, yes or no.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33manalyzer\u001b[0m (to user_proxy):\n",
      "\n",
      "Yes\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Sure, I'll provide you with a Python function that takes a string as input and returns the reversed string. Here is the complete code:\n",
      "\n",
      "```python\n",
      "# filename: reverse_string.py\n",
      "\n",
      "def reverse_string(s: str) -> str:\n",
      "    \"\"\"\n",
      "    This function takes a string as input and returns the reversed string.\n",
      "    \n",
      "    :param s: Input string to be reversed\n",
      "    :return: Reversed string\n",
      "    \"\"\"\n",
      "    return s[::-1]\n",
      "\n",
      "# Example usage\n",
      "input_string = \"Hello, World!\"\n",
      "reversed_string = reverse_string(input_string)\n",
      "print(f\"Original string: {input_string}\")\n",
      "print(f\"Reversed string: {reversed_string}\")\n",
      "```\n",
      "\n",
      "Save the above code in a file named `reverse_string.py`, then execute it. This script defines the `reverse_string` function and demonstrates its usage by reversing the string \"Hello, World!\". It will print both the original and reversed strings.\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Imagine that the user forgot this information in the TEXT. How would they ask you for this information? Include no other text in your response.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33manalyzer\u001b[0m (to user_proxy):\n",
      "\n",
      "How do I reverse a string in Python?\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Sure, I'll provide you with a Python function that takes a string as input and returns the reversed string. Here is the complete code:\n",
      "\n",
      "```python\n",
      "# filename: reverse_string.py\n",
      "\n",
      "def reverse_string(s: str) -> str:\n",
      "    \"\"\"\n",
      "    This function takes a string as input and returns the reversed string.\n",
      "    \n",
      "    :param s: Input string to be reversed\n",
      "    :return: Reversed string\n",
      "    \"\"\"\n",
      "    return s[::-1]\n",
      "\n",
      "# Example usage\n",
      "input_string = \"Hello, World!\"\n",
      "reversed_string = reverse_string(input_string)\n",
      "print(f\"Original string: {input_string}\")\n",
      "print(f\"Reversed string: {reversed_string}\")\n",
      "```\n",
      "\n",
      "Save the above code in a file named `reverse_string.py`, then execute it. This script defines the `reverse_string` function and demonstrates its usage by reversing the string \"Hello, World!\". It will print both the original and reversed strings.\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Copy the information from the TEXT that should be committed to memory. Add no explanation.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33manalyzer\u001b[0m (to user_proxy):\n",
      "\n",
      "```python\n",
      "# filename: reverse_string.py\n",
      "\n",
      "def reverse_string(s: str) -> str:\n",
      "    \"\"\"\n",
      "    This function takes a string as input and returns the reversed string.\n",
      "    \n",
      "    :param s: Input string to be reversed\n",
      "    :return: Reversed string\n",
      "    \"\"\"\n",
      "    return s[::-1]\n",
      "\n",
      "# Example usage\n",
      "input_string = \"Hello, World!\"\n",
      "reversed_string = reverse_string(input_string)\n",
      "print(f\"Original string: {input_string}\")\n",
      "print(f\"Reversed string: {reversed_string}\")\n",
      "```\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[93m\n",
      "REMEMBER THIS QUESTION-ANSWER PAIR\u001b[0m\n",
      "\u001b[31m\n",
      ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001b[0m\n",
      "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
      "\n",
      "exitcode: 0 (execution succeeded)\n",
      "Code output: \n",
      "Original string: Hello, World!\n",
      "Reversed string: !dlroW ,olleH\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33massistant\u001b[0m (to user_proxy):\n",
      "\n",
      "The code executed successfully, and the output is correct. The string \"Hello, World!\" was successfully reversed to \"!dlroW ,olleH\".\n",
      "\n",
      "If you have any other tasks or need further assistance, feel free to ask.\n",
      "\n",
      "TERMINATE\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[93m\n",
      "LOOK FOR RELEVANT MEMOS, AS QUESTION-ANSWER PAIRS\u001b[0m\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "The code executed successfully, and the output is correct. The string \"Hello, World!\" was successfully reversed to \"!dlroW ,olleH\".\n",
      "\n",
      "If you have any other tasks or need further assistance, feel free to ask.\n",
      "\n",
      "TERMINATE\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Does any part of the TEXT ask the agent to perform a task or solve a problem? Answer with just one word, yes or no.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33manalyzer\u001b[0m (to user_proxy):\n",
      "\n",
      "Yes\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[93m\n",
      "LOOK FOR RELEVANT MEMOS, AS TASK-ADVICE PAIRS\u001b[0m\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "The code executed successfully, and the output is correct. The string \"Hello, World!\" was successfully reversed to \"!dlroW ,olleH\".\n",
      "\n",
      "If you have any other tasks or need further assistance, feel free to ask.\n",
      "\n",
      "TERMINATE\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Copy just the task from the TEXT, then stop. Don't solve it, and don't include any advice.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33manalyzer\u001b[0m (to user_proxy):\n",
      "\n",
      "If you have any other tasks or need further assistance, feel free to ask.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "If you have any other tasks or need further assistance, feel free to ask.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Summarize very briefly, in general terms, the type of task described in the TEXT. Leave out details that might not appear in a similar problem.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33manalyzer\u001b[0m (to user_proxy):\n",
      "\n",
      "The task described in the TEXT involves offering help or assistance with various tasks.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[93m\n",
      "MEMOS APPENDED TO LAST MESSAGE...\n",
      "\n",
      "# Memories that might help\n",
      "- Prefers functions to have a descriptive docstring\n",
      "- Prefers comments explaining each step\n",
      "- Task involves saving a script to a file, executing it, and demonstrating a function that reverses a string\n",
      "- Prefers code to be explicitly written with clear variable names\n",
      "- Code should be saved in a file named 'reverse_string.py'\n",
      "- Prefers camelCase for variable names\n",
      "\n",
      "\u001b[0m\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "The code executed successfully, and the output is correct. The string \"Hello, World!\" was successfully reversed to \"!dlroW ,olleH\".\n",
      "\n",
      "If you have any other tasks or need further assistance, feel free to ask.\n",
      "\n",
      "TERMINATE\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Does any part of the TEXT ask the agent to perform a task or solve a problem? Answer with just one word, yes or no.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33manalyzer\u001b[0m (to user_proxy):\n",
      "\n",
      "Yes\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "The code executed successfully, and the output is correct. The string \"Hello, World!\" was successfully reversed to \"!dlroW ,olleH\".\n",
      "\n",
      "If you have any other tasks or need further assistance, feel free to ask.\n",
      "\n",
      "TERMINATE\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Briefly copy any advice from the TEXT that may be useful for a similar but different task in the future. But if no advice is present, just respond with 'none'.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33manalyzer\u001b[0m (to user_proxy):\n",
      "\n",
      "none\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "The code executed successfully, and the output is correct. The string \"Hello, World!\" was successfully reversed to \"!dlroW ,olleH\".\n",
      "\n",
      "If you have any other tasks or need further assistance, feel free to ask.\n",
      "\n",
      "TERMINATE\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Does the TEXT contain information that could be committed to memory? Answer with just one word, yes or no.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33manalyzer\u001b[0m (to user_proxy):\n",
      "\n",
      "Yes\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "The code executed successfully, and the output is correct. The string \"Hello, World!\" was successfully reversed to \"!dlroW ,olleH\".\n",
      "\n",
      "If you have any other tasks or need further assistance, feel free to ask.\n",
      "\n",
      "TERMINATE\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Imagine that the user forgot this information in the TEXT. How would they ask you for this information? Include no other text in your response.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33manalyzer\u001b[0m (to user_proxy):\n",
      "\n",
      "What was the original string that was reversed to \"!dlroW ,olleH\"?\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "The code executed successfully, and the output is correct. The string \"Hello, World!\" was successfully reversed to \"!dlroW ,olleH\".\n",
      "\n",
      "If you have any other tasks or need further assistance, feel free to ask.\n",
      "\n",
      "TERMINATE\n",
      "\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33muser_proxy\u001b[0m (to analyzer):\n",
      "\n",
      "Copy the information from the TEXT that should be committed to memory. Add no explanation.\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33manalyzer\u001b[0m (to user_proxy):\n",
      "\n",
      "The string \"Hello, World!\" was successfully reversed to \"!dlroW ,olleH\".\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[93m\n",
      "REMEMBER THIS QUESTION-ANSWER PAIR\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "ChatResult(chat_id=None, chat_history=[{'content': 'Write a Python function that reverses a string.', 'role': 'assistant', 'name': 'user_proxy'}, {'content': 'Sure, I\\'ll provide you with a Python function that takes a string as input and returns the reversed string. Here is the complete code:\\n\\n```python\\n# filename: reverse_string.py\\n\\ndef reverse_string(s: str) -> str:\\n    \"\"\"\\n    This function takes a string as input and returns the reversed string.\\n    \\n    :param s: Input string to be reversed\\n    :return: Reversed string\\n    \"\"\"\\n    return s[::-1]\\n\\n# Example usage\\ninput_string = \"Hello, World!\"\\nreversed_string = reverse_string(input_string)\\nprint(f\"Original string: {input_string}\")\\nprint(f\"Reversed string: {reversed_string}\")\\n```\\n\\nSave the above code in a file named `reverse_string.py`, then execute it. This script defines the `reverse_string` function and demonstrates its usage by reversing the string \"Hello, World!\". It will print both the original and reversed strings.\\n\\n\\n# Memories that might help\\n- Prefers functions to have a descriptive docstring\\n- Prefers camelCase for variable names\\n- Prefers comments explaining each step\\n- Prefers code to be explicitly written with clear variable names\\n', 'role': 'user', 'name': 'assistant'}, {'content': 'exitcode: 0 (execution succeeded)\\nCode output: \\nOriginal string: Hello, World!\\nReversed string: !dlroW ,olleH\\n', 'role': 'assistant', 'name': 'user_proxy'}, {'content': 'The code executed successfully, and the output is correct. The string \"Hello, World!\" was successfully reversed to \"!dlroW ,olleH\".\\n\\nIf you have any other tasks or need further assistance, feel free to ask.\\n\\nTERMINATE\\n\\n\\n# Memories that might help\\n- Prefers functions to have a descriptive docstring\\n- Prefers comments explaining each step\\n- Task involves saving a script to a file, executing it, and demonstrating a function that reverses a string\\n- Prefers code to be explicitly written with clear variable names\\n- Code should be saved in a file named \\'reverse_string.py\\'\\n- Prefers camelCase for variable names\\n', 'role': 'user', 'name': 'assistant'}], summary='The code executed successfully, and the output is correct. The string \"Hello, World!\" was successfully reversed to \"!dlroW ,olleH\".\\n\\nIf you have any other tasks or need further assistance, feel free to ask.\\n\\n\\n', cost={'usage_including_cached_inference': {'total_cost': 0}, 'usage_excluding_cached_inference': {'total_cost': 0}}, human_input=[])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Initiate Chat w/ Teachability + Memory\n",
    "user_proxy.initiate_chat(gpt_assistant, message=user_query)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
