2023-07-28 21:17:51 -07:00
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "ae1f50ec",
"metadata": {},
"source": [
2023-09-19 02:26:57 +00:00
"<a href=\"https://colab.research.google.com/github/microsoft/autogen/blob/main/notebook/agentchat_function_call.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
2023-07-28 21:17:51 -07:00
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "9a71fa36",
"metadata": {},
"source": [
"# Auto Generated Agent Chat: Task Solving with Provided Tools as Functions\n",
"\n",
2023-10-21 20:26:34 -04:00
"AutoGen offers conversable agents powered by LLM, tool, or human, which can be used to perform tasks collectively via automated chat. This framework allows tool use and human participation through multi-agent conversation. Please find documentation about this feature [here](https://microsoft.github.io/autogen/docs/Use-Cases/agent_chat).\n",
2023-07-28 21:17:51 -07:00
"\n",
2023-10-21 20:26:34 -04:00
"In this notebook, we demonstrate how to use `AssistantAgent` and `UserProxyAgent` to make function calls with the new feature of OpenAI models (in model version 0613). A specified prompt and function configs must be passed to `AssistantAgent` to initialize the agent. The corresponding functions must be passed to `UserProxyAgent`, which will execute any function calls made by `AssistantAgent`. Besides this requirement of matching descriptions with functions, we recommend checking the system message in the `AssistantAgent` to ensure the instructions align with the function call descriptions.\n",
2023-07-28 21:17:51 -07:00
"\n",
"## Requirements\n",
"\n",
2023-11-06 13:14:05 -08:00
"AutoGen requires `Python>=3.8`. To run this notebook example, please install `pyautogen`:\n",
2023-07-28 21:17:51 -07:00
"```bash\n",
2023-11-06 13:14:05 -08:00
"pip install pyautogen\n",
2023-07-28 21:17:51 -07:00
"```"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "2b803c17",
"metadata": {},
"outputs": [],
"source": [
2023-11-06 13:14:05 -08:00
"# %pip install \"pyautogen~=0.2.0b2\""
2023-07-28 21:17:51 -07:00
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "5ebd2397",
"metadata": {},
"source": [
"## Set your API Endpoint\n",
"\n",
2023-11-06 13:14:05 -08:00
"The [`config_list_from_json`](https://microsoft.github.io/autogen/docs/reference/oai/openai_utils#config_list_from_json) function loads a list of configurations from an environment variable or a json file."
2023-07-28 21:17:51 -07:00
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "dca301a4",
"metadata": {},
"outputs": [],
"source": [
2023-09-16 16:34:16 +00:00
"import autogen\n",
2023-07-28 21:17:51 -07:00
"\n",
2023-11-06 13:14:05 -08:00
"config_list = autogen.config_list_from_json(\n",
" \"OAI_CONFIG_LIST\",\n",
" filter_dict={\n",
" \"model\": [\"gpt-4\", \"gpt-3.5-turbo\", \"gpt-3.5-turbo-16k\"],\n",
" },\n",
")"
2023-07-28 21:17:51 -07:00
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "92fde41f",
"metadata": {},
"source": [
2023-11-06 13:14:05 -08:00
"It first looks for environment variable \"OAI_CONFIG_LIST\" which needs to be a valid json string. If that variable is not found, it then looks for a json file named \"OAI_CONFIG_LIST\". It filters the configs by models (you can filter by other keys as well). Only the models with matching names are kept in the list based on the filter condition.\n",
"\n",
2023-07-28 21:17:51 -07:00
"The config list looks like the following:\n",
"```python\n",
"config_list = [\n",
" {\n",
" 'model': 'gpt-4',\n",
" 'api_key': '<your OpenAI API key here>',\n",
2023-11-06 13:14:05 -08:00
" },\n",
2023-07-28 21:17:51 -07:00
" {\n",
" 'model': 'gpt-3.5-turbo',\n",
2023-11-06 13:14:05 -08:00
" 'api_key': '<your Azure OpenAI API key here>',\n",
" 'base_url': '<your Azure OpenAI API base here>',\n",
" 'api_type': 'azure',\n",
" 'api_version': '2023-08-01-preview',\n",
" },\n",
2023-07-28 21:17:51 -07:00
" {\n",
" 'model': 'gpt-3.5-turbo-16k',\n",
2023-11-06 13:14:05 -08:00
" 'api_key': '<your Azure OpenAI API key here>',\n",
" 'base_url': '<your Azure OpenAI API base here>',\n",
" 'api_type': 'azure',\n",
" 'api_version': '2023-08-01-preview',\n",
" },\n",
2023-07-28 21:17:51 -07:00
"]\n",
2023-11-06 13:14:05 -08:00
"```\n",
"\n",
"You can set the value of config_list in any way you prefer. Please refer to this [notebook](https://github.com/microsoft/autogen/blob/main/notebook/oai_openai_utils.ipynb) for full code examples of the different methods."
2023-07-28 21:17:51 -07:00
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "2b9526e7",
"metadata": {},
"source": [
"## Making Function Calls\n",
"\n",
"In this example, we demonstrate function call execution with `AssistantAgent` and `UserProxyAgent`. With the default system prompt of `AssistantAgent`, we allow the LLM assistant to perform tasks with code, and the `UserProxyAgent` would extract code blocks from the LLM response and execute them. With the new \"function_call\" feature, we define functions and specify the description of the function in the OpenAI config for the `AssistantAgent`. Then we register the functions in `UserProxyAgent`.\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "9fb85afb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2023-07-31 19:22:30 -07:00
"\u001b[33muser_proxy\u001b[0m (to chatbot):\n",
2023-07-28 21:17:51 -07:00
"\n",
"Draw two agents chatting with each other with an example dialog. Don't add plt.show().\n",
"\n",
"--------------------------------------------------------------------------------\n",
2023-07-31 19:22:30 -07:00
"\u001b[33mchatbot\u001b[0m (to user_proxy):\n",
2023-07-28 21:17:51 -07:00
"\n",
2023-07-31 19:22:30 -07:00
"\u001b[32m***** Suggested function Call: python *****\u001b[0m\n",
2023-07-28 21:17:51 -07:00
"Arguments: \n",
"{\n",
2023-11-06 13:14:05 -08:00
" \"cell\": \"import matplotlib.pyplot as plt\\n\n",
"# Initialize an empty figure and axis\\n\n",
"fig, ax = plt.subplots()\\n\n",
"# Create the chatboxes for messages\\n\n",
"ax.text(0.5, 0.6, 'Agent1: Hi!', bbox=dict(facecolor='red', alpha=0.5))\\n\n",
"ax.text(0.5, 0.5, 'Agent2: Hello!', bbox=dict(facecolor='blue', alpha=0.5))\\n\n",
"plt.axis('off')\"\n",
2023-07-28 21:17:51 -07:00
"}\n",
2023-07-31 19:22:30 -07:00
"\u001b[32m*******************************************\u001b[0m\n",
2023-07-28 21:17:51 -07:00
"\n",
2023-07-31 19:22:30 -07:00
"--------------------------------------------------------------------------------\n",
"\u001b[35m\n",
">>>>>>>> EXECUTING FUNCTION python...\u001b[0m\n"
2023-07-28 21:17:51 -07:00
]
},
{
"data": {
"text/plain": [
"(0.0, 1.0, 0.0, 1.0)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
2023-11-06 13:14:05 -08:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAgMAAAGFCAYAAABg2vAPAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy81sbWrAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAUuklEQVR4nO3dfZBVhZ3n4W/zotCoKLQY2sUYXwqJEUIQjGWhZLRYiDOzSbRiZWISqmICyZjZZK1Z45g3zIs6GUeTsqLUDpFyMo6aGRNTEV0lhjdfojINRhTUlAJBQLrBhk6roPT+IdNrj0kEBDv6e56q/qPPuefc371UcT997rnnNnR1dXUFACirT28PAAD0LjEAAMWJAQAoTgwAQHFiAACKEwMAUJwYAIDixAAAFCcGAKA4MQAAxYkBAChODABAcWIAAIoTAwBQnBgAgOLEAAAUJwYAoDgxAADFiQEAKE4MAEBxYgAAihMDAFCcGACA4sQAABQnBgCgODEAAMWJAQAoTgwAQHFiAACKEwMAUJwYAIDixAAAFCcGAKA4MQAAxYkBAChODABAcWIAAIoTAwBQnBgAgOLEAAAUJwYAoDgxAADFiQEAKE4MAEBxYgAAihMDAFCcGACA4sQAABQnBgCgODEAAMWJAQAoTgwAQHFiAACKEwMAUJwYAIDixAAAFCcGAKA4MQAAxYkBAChODABAcWIAAIoTAwBQnBgAgOLEAAAUJwYAoDgxAADFiQEAKE4MAEBxYgAAihMDAFCcGACA4sQAABQnBgCgODEAAMWJAQAoTgwAQHFiAACK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mzfts/9AbGrq6nD4LFaxbty6zLroo04cO3atfYfzthQtz7ZIlWfOlL+21fb6Vrdu6NbPa2jL90kszfPjw3h4HdolzBoDd8oMHH8z45uYMbWzMPatX57v33pvzJ0zo7bGAN0AMALvliba2fGvhwmx6/vkcMXhwLjj55Fz0X65SCLy1iAFgt1w5ZUqunDKlt8cA9iInEAJAcWIAAIoTAwBQnHMGoJiNnZ29PcLbmueXtyIxAEU0Njamf1NTbmltTZ5/vrfHeVvr39SUxsbG3h4DdpmLDkEh7e3t6fSX6z7X2NiYwa/6Uib4UycGAKA4JxACQHFiAACKEwMAUJwYAIDixAAAFCcGAKA4MQAAxYkBACjO5YihEFcgfOtyVUP2JTEARbS3t+eb37w6ra3be3sU9kBTU/989avnCwL2CTEARXR2dqa1dXsGDvxIGhsP7e1x2A2dnRvT2npLOjs7xQD7hBiAYhobD82BBw7v7THYTb5okn3JCYQAUJwYAIDixAAAFCcGAN6AmTMbsmLFT5Mkzz33dGbObMj69Ut7dSbYXWIA6GHNmvtyySV9c8MNZ/baDH/oRXXJkv+T666bmMsvPySXX35Irr/+jKxd+8Bu73/OnEm5444vvmb50qVzctllB+/Z0G/AH5oH3ixiAOihpWV2Jkz4QlatWpitW5/p7XF6WLVqft7zno/lU5/6ZT796fsyePCI/PM/T86WLWt7ezR4S/PRQqDbtm0dWb78pnzmMw+lo2N9li6dk4kT/67HbVau/FnuvPOCtLevyYgRJ2fMmGm59dZpufDCzRkw4OAkyerVi/OLX1yUZ555KI2NTTnuuA/n9NMvzX77DUqSXHXVkRk37rPZtOnJPProjzNgwCE59dSvZNy4zyZJvve9dyVJZs0amyR55ztPy7Rp8/ORj/xLj1n+4i/+KY8++u956qlfZMyYT+6T52TFiluzYMHMbNz4aA48sDljxnwqp556cfr02bX/Pp9+ekHuuutvs2HDsgwcOCRjxnwqf/Zn39rl7eHN4MgA0G358pvT1HRcmppGZvToc9P
2023-07-28 21:17:51 -07:00
"text/plain": [
2023-11-06 13:14:05 -08:00
"<Figure size 640x480 with 1 Axes>"
2023-07-28 21:17:51 -07:00
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
2023-07-31 19:22:30 -07:00
"\u001b[33muser_proxy\u001b[0m (to chatbot):\n",
2023-07-28 21:17:51 -07:00
"\n",
2023-07-31 19:22:30 -07:00
"\u001b[32m***** Response from calling function \"python\" *****\u001b[0m\n",
2023-07-28 21:17:51 -07:00
"(0.0, 1.0, 0.0, 1.0)\n",
2023-07-31 19:22:30 -07:00
"\u001b[32m***************************************************\u001b[0m\n",
2023-07-28 21:17:51 -07:00
"\n",
"--------------------------------------------------------------------------------\n"
]
}
],
"source": [
2023-07-31 19:22:30 -07:00
"llm_config = {\n",
2023-07-28 21:17:51 -07:00
" \"functions\": [\n",
" {\n",
" \"name\": \"python\",\n",
" \"description\": \"run cell in ipython and return the execution result.\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"cell\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Valid Python cell to execute.\",\n",
" }\n",
" },\n",
" \"required\": [\"cell\"],\n",
" },\n",
" },\n",
" {\n",
" \"name\": \"sh\",\n",
" \"description\": \"run a shell script and return the execution result.\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"script\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Valid shell script to execute.\",\n",
" }\n",
" },\n",
" \"required\": [\"script\"],\n",
" },\n",
" },\n",
" ],\n",
" \"config_list\": config_list,\n",
2023-11-03 21:01:49 -07:00
" \"timeout\": 120,\n",
2023-07-28 21:17:51 -07:00
"}\n",
2023-07-31 19:22:30 -07:00
"chatbot = autogen.AssistantAgent(\n",
2023-07-28 21:17:51 -07:00
" name=\"chatbot\",\n",
" system_message=\"For coding tasks, only use the functions you have been provided with. Reply TERMINATE when the task is done.\",\n",
2023-07-31 19:22:30 -07:00
" llm_config=llm_config,\n",
2023-07-28 21:17:51 -07:00
")\n",
"\n",
"# create a UserProxyAgent instance named \"user_proxy\"\n",
2023-07-31 19:22:30 -07:00
"user_proxy = autogen.UserProxyAgent(\n",
2023-07-28 21:17:51 -07:00
" name=\"user_proxy\",\n",
" is_termination_msg=lambda x: x.get(\"content\", \"\") and x.get(\"content\", \"\").rstrip().endswith(\"TERMINATE\"),\n",
" human_input_mode=\"NEVER\",\n",
" max_consecutive_auto_reply=10,\n",
" code_execution_config={\"work_dir\": \"coding\"},\n",
")\n",
"\n",
2023-10-21 20:26:34 -04:00
"# define functions according to the function description\n",
2023-07-28 21:17:51 -07:00
"from IPython import get_ipython\n",
"\n",
"def exec_python(cell):\n",
" ipython = get_ipython()\n",
" result = ipython.run_cell(cell)\n",
" log = str(result.result)\n",
" if result.error_before_exec is not None:\n",
" log += f\"\\n{result.error_before_exec}\"\n",
" if result.error_in_exec is not None:\n",
" log += f\"\\n{result.error_in_exec}\"\n",
" return log\n",
"\n",
"def exec_sh(script):\n",
" return user_proxy.execute_code_blocks([(\"sh\", script)])\n",
"\n",
"# register the functions\n",
"user_proxy.register_function(\n",
" function_map={\n",
" \"python\": exec_python,\n",
" \"sh\": exec_sh,\n",
" }\n",
")\n",
"\n",
"# start the conversation\n",
"user_proxy.initiate_chat(\n",
" chatbot,\n",
" message=\"Draw two agents chatting with each other with an example dialog. Don't add plt.show().\",\n",
")\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "flaml_dev",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
2023-11-06 13:14:05 -08:00
"version": "3.11.4"
2023-07-28 21:17:51 -07:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}