"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",
"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",
"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."
"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",
"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/website/docs/llm_endpoint_configuration.ipynb) for full code examples of the different methods."
"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"
" system_message=\"For coding tasks, only use the functions you have been provided with. Reply TERMINATE when the task is done.\",\n",
" llm_config=llm_config,\n",
")\n",
"\n",
"# create a UserProxyAgent instance named \"user_proxy\"\n",
"user_proxy = autogen.UserProxyAgent(\n",
" 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={\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",
")\n",
"\n",
"\n",
"# define functions according to the function description\n",
"\n",
"\n",
"# one way of registering functions is to use the register_for_llm and register_for_execution decorators\n",
"@user_proxy.register_for_execution()\n",
"@chatbot.register_for_llm(name=\"python\", description=\"run cell in ipython and return the execution result.\")\n",
"def exec_python(cell: Annotated[str, \"Valid Python cell to execute.\"]) -> str:\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",
"\n",
"# another way of registering functions is to use the register_function\n",
"def exec_sh(script: Annotated[str, \"Valid Python cell to execute.\"]) -> str:\n",
" \"cell\": \"import matplotlib.pyplot as plt\\nimport matplotlib.patches as patches\\n\\n# Creating a simple scene for two agents chatting\\nfig, ax = plt.subplots()\\n\\n# Draw two circles representing the agents\\nax.add_patch(patches.Circle((2, 2), 0.5, fill=True, color='blue', label='Agent A'))\\nax.add_patch(patches.Circle((5, 2), 0.5, fill=True, color='green', label='Agent B'))\\n\\n# Example dialogues as text\\nax.text(1, 3, \\\"Hello!\\\", style='italic', bbox={'facecolor': 'red', 'alpha': 0.5, 'pad': 5})\\nax.text(4, 3, \\\"Hi there!\\\", style='italic', bbox={'facecolor': 'yellow', 'alpha': 0.5, 'pad': 5})\\n\\n# Setting the limits of the plot\\nax.set_xlim(0, 7)\\nax.set_ylim(0, 4)\\n\\n# Hiding the axes\\nax.axis('off')\\n\\n# Use this line just before the plt.show() if necessary\\nplt.savefig(\\\"agents_chatting.png\\\")\\n\\n# Don't add plt.show() as per the instructions\\n\"\n",