Fix all lint errors (#2627)

This commit is contained in:
Dev Khant
2025-05-06 01:16:02 +05:30
committed by GitHub
parent 725a1aa114
commit ec1d7a45d3
50 changed files with 586 additions and 570 deletions

View File

@@ -7,10 +7,12 @@
# forked from autogen.agentchat.contrib.capabilities.teachability.Teachability
from typing import Dict, Optional, Union
from autogen.agentchat.assistant_agent import ConversableAgent
from autogen.agentchat.contrib.capabilities.agent_capability import AgentCapability
from autogen.agentchat.contrib.text_analyzer_agent import TextAnalyzerAgent
from termcolor import colored
from mem0 import Memory

View File

@@ -1,42 +1,116 @@
{
"cells": [
{
"metadata": {},
"cell_type": "code",
"execution_count": null,
"id": "1e8a980a2e0b9a85",
"metadata": {},
"outputs": [],
"source": [
"%pip install --upgrade pip\n",
"%pip install mem0ai pyautogen flaml"
],
"id": "1e8a980a2e0b9a85",
"outputs": [],
"execution_count": null
]
},
{
"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"
}
},
"cell_type": "code",
"outputs": [],
"source": [
"# Set up ENV Vars\n",
"import os\n",
"os.environ['OPENAI_API_KEY'] = \"sk-xxx\"\n"
],
"id": "d437544fe259dd1b",
"outputs": [],
"execution_count": 11
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "initial_id",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2024-09-25T20:30:03.914245Z",
"start_time": "2024-09-25T20:29:53.236601Z"
}
},
"collapsed": true
},
"cell_type": "code",
"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",
@@ -93,90 +167,40 @@
"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)"
],
"id": "initial_id",
"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"
}
],
"execution_count": 12
]
},
{
"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"
}
},
"cell_type": "code",
"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",
@@ -207,61 +231,28 @@
"# 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)"
],
"id": "c2fe6fd02324be37",
"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"
}
],
"execution_count": 16
]
},
{
"metadata": {},
"cell_type": "markdown",
"id": "fb6d6a8f36aedfd6",
"metadata": {},
"source": [
"## Option 1: \n",
"Using Direct Prompt Injection:\n",
"`user memory example`"
],
"id": "fb6d6a8f36aedfd6"
]
},
{
"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"
}
},
"cell_type": "code",
"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(f\"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)"
],
"id": "29be484c69093371",
"outputs": [
{
"name": "stderr",
@@ -280,7 +271,7 @@
"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",
"\u001b[33muser_proxy\u001b[0m (to assistant):\n",
"\n",
"Write a Python function that reverses a string.\n",
" Coding Preferences: \n",
@@ -289,7 +280,7 @@
"Prefers code to be explicitly written with clear variable names\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001B[33massistant\u001B[0m (to user_proxy):\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",
@@ -324,9 +315,9 @@
"\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",
"\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",
@@ -335,7 +326,7 @@
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001B[33massistant\u001B[0m (to user_proxy):\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",
@@ -354,26 +345,38 @@
]
}
],
"execution_count": 17
"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)"
]
},
{
"metadata": {},
"cell_type": "markdown",
"id": "fc0ae72d0ef7f6de",
"metadata": {},
"source": [
"## Option 2:\n",
"Using UserProxyAgent: \n",
"`agent memory example`"
],
"id": "fc0ae72d0ef7f6de"
]
},
{
"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"
}
},
"cell_type": "code",
"outputs": [],
"source": [
"# UserProxyAgent in AutoGen:\n",
"# - Acts as intermediary between humans and AI agents in the AutoGen framework.\n",
@@ -405,39 +408,24 @@
" self.memory.add(MEMORY_DATA, agent_id=self.agent_id)\n",
" return response\n",
" "
],
"id": "bfd9342cf2096ca5",
"outputs": [],
"execution_count": 18
]
},
{
"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"
}
},
"cell_type": "code",
"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)"
],
"id": "6d2a757d1cf65881",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001B[33mchicory.ai\u001B[0m (to assistant):\n",
"\u001b[33mchicory.ai\u001b[0m (to assistant):\n",
"\n",
"Write a Python function that reverses a string.\n",
" Coding Preferences: \n",
@@ -460,7 +448,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"\u001B[33massistant\u001B[0m (to chicory.ai):\n",
"\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",
@@ -487,9 +475,9 @@
"\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",
"\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",
@@ -497,7 +485,7 @@
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001B[33massistant\u001B[0m (to chicory.ai):\n",
"\u001b[33massistant\u001b[0m (to chicory.ai):\n",
"\n",
"Great! The function has successfully reversed the string as expected.\n",
"\n",
@@ -518,26 +506,41 @@
]
}
],
"execution_count": 19
"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)"
]
},
{
"metadata": {},
"cell_type": "markdown",
"id": "7706c06216ca4374",
"metadata": {},
"source": [
"# Option 3:\n",
"Using Teachability:\n",
"`agent memory example`"
],
"id": "7706c06216ca4374"
]
},
{
"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"
}
},
"cell_type": "code",
"outputs": [],
"source": [
"# building on top of existing Teachability package from autogen\n",
"# from autogen.agentchat.contrib.capabilities.teachability import Teachability\n",
@@ -564,24 +567,18 @@
" memory_client = MEM0_MEMORY_CLIENT,\n",
" )\n",
"teachability.add_to_agent(user_proxy)"
],
"id": "ae6bb87061877645",
"outputs": [],
"execution_count": 20
]
},
{
"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"
}
},
"cell_type": "code",
"source": [
"# Initiate Chat w/ Teachability + Memory\n",
"user_proxy.initiate_chat(gpt_assistant, message=user_query)"
],
"id": "36c9bcbedcd406b4",
"outputs": [
{
"name": "stderr",
@@ -594,12 +591,12 @@
"name": "stdout",
"output_type": "stream",
"text": [
"\u001B[33muser_proxy\u001B[0m (to assistant):\n",
"\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",
"\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",
@@ -626,9 +623,9 @@
"\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",
"\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",
@@ -655,19 +652,19 @@
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001B[33muser_proxy\u001B[0m (to analyzer):\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",
"\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",
"\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",
@@ -694,32 +691,32 @@
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001B[33muser_proxy\u001B[0m (to analyzer):\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",
"\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",
"\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",
"\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",
"\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",
"\u001b[93m\n",
"MEMOS APPENDED TO LAST MESSAGE...\n",
"\n",
"# Memories that might help\n",
@@ -728,8 +725,8 @@
"- 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",
"\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",
@@ -756,17 +753,17 @@
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001B[33muser_proxy\u001B[0m (to analyzer):\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",
"\u001b[33manalyzer\u001b[0m (to user_proxy):\n",
"\n",
"Yes\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001B[33muser_proxy\u001B[0m (to analyzer):\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",
@@ -793,17 +790,17 @@
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001B[33muser_proxy\u001B[0m (to analyzer):\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",
"\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",
"\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",
@@ -830,34 +827,34 @@
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001B[33muser_proxy\u001B[0m (to analyzer):\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",
"\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",
"\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",
"\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",
"\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",
"\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",
@@ -884,17 +881,17 @@
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001B[33muser_proxy\u001B[0m (to analyzer):\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",
"\u001b[33manalyzer\u001b[0m (to user_proxy):\n",
"\n",
"Yes\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001B[33muser_proxy\u001B[0m (to analyzer):\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",
@@ -921,17 +918,17 @@
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001B[33muser_proxy\u001B[0m (to analyzer):\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",
"\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",
"\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",
@@ -958,12 +955,12 @@
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001B[33muser_proxy\u001B[0m (to analyzer):\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",
"\u001b[33manalyzer\u001b[0m (to user_proxy):\n",
"\n",
"```python\n",
"# filename: reverse_string.py\n",
@@ -985,11 +982,11 @@
"```\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",
"\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",
@@ -998,7 +995,7 @@
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001B[33massistant\u001B[0m (to user_proxy):\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",
@@ -1008,9 +1005,9 @@
"\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",
"\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",
@@ -1020,19 +1017,19 @@
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001B[33muser_proxy\u001B[0m (to analyzer):\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",
"\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",
"\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",
@@ -1042,32 +1039,32 @@
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001B[33muser_proxy\u001B[0m (to analyzer):\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",
"\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",
"\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",
"\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",
"\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",
"\u001b[93m\n",
"MEMOS APPENDED TO LAST MESSAGE...\n",
"\n",
"# Memories that might help\n",
@@ -1078,8 +1075,8 @@
"- 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",
"\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",
@@ -1089,17 +1086,17 @@
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001B[33muser_proxy\u001B[0m (to analyzer):\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",
"\u001b[33manalyzer\u001b[0m (to user_proxy):\n",
"\n",
"Yes\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001B[33muser_proxy\u001B[0m (to analyzer):\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",
@@ -1109,17 +1106,17 @@
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001B[33muser_proxy\u001B[0m (to analyzer):\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",
"\u001b[33manalyzer\u001b[0m (to user_proxy):\n",
"\n",
"none\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001B[33muser_proxy\u001B[0m (to analyzer):\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",
@@ -1129,17 +1126,17 @@
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001B[33muser_proxy\u001B[0m (to analyzer):\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",
"\u001b[33manalyzer\u001b[0m (to user_proxy):\n",
"\n",
"Yes\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001B[33muser_proxy\u001B[0m (to analyzer):\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",
@@ -1149,17 +1146,17 @@
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001B[33muser_proxy\u001B[0m (to analyzer):\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",
"\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",
"\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",
@@ -1169,18 +1166,18 @@
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001B[33muser_proxy\u001B[0m (to analyzer):\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",
"\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"
"\u001b[93m\n",
"REMEMBER THIS QUESTION-ANSWER PAIR\u001b[0m\n"
]
},
{
@@ -1194,7 +1191,10 @@
"output_type": "execute_result"
}
],
"execution_count": 21
"source": [
"# Initiate Chat w/ Teachability + Memory\n",
"user_proxy.initiate_chat(gpt_assistant, message=user_query)"
]
}
],
"metadata": {