Notebooks
A
Arize AI
Autogen Tutorial

Autogen Tutorial

arize-tutorialstracingLLMPythonautogen

Auto Generated Agent Chat: Task Solving with Code Generation, Execution & Debugging

AutoGen offers conversable LLM agents, which can be used to solve various tasks with human or automatic feedback, including tasks that require using tools via code. Please find documentation about this feature here.

In this notebook, we demonstrate how to use AssistantAgent and UserProxyAgent to write code and execute the code. Here AssistantAgent is an LLM-based agent that can write Python code (in a Python coding block) for a user to execute for a given task. UserProxyAgent is an agent which serves as a proxy for the human user to execute the code written by AssistantAgent, or automatically execute the code. Depending on the setting of human_input_mode and max_consecutive_auto_reply, the UserProxyAgent either solicits feedback from the human user or returns auto-feedback based on the result of code execution (success or failure and corresponding outputs) to AssistantAgent. AssistantAgent will debug the code and suggest new code if the result contains error. The two agents keep communicating to each other until the task is done.

Requirements

AutoGen requires Python>=3.8. To run this notebook example, please install:

pip install pyautogen
[ ]

Set your API Endpoint

The 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 gpt-4 models are kept in the list based on the filter condition.

The config list looks like the following:

config_list = [
    {
        'model': 'gpt-4',
        'api_key': '<your OpenAI API key here>',
    },
    {
        'model': 'gpt-4',
        'api_key': '<your Azure OpenAI API key here>',
        'api_base': '<your Azure OpenAI API base here>',
        'api_type': 'azure',
        'api_version': '2023-06-01-preview',
    },
    {
        'model': 'gpt-4-32k',
        'api_key': '<your Azure OpenAI API key here>',
        'api_base': '<your Azure OpenAI API base here>',
        'api_type': 'azure',
        'api_version': '2023-06-01-preview',
    },
]

If you open this notebook in colab, you can upload your files by clicking the file icon on the left panel and then choose "upload file" icon.

You can set the value of config_list in other ways you prefer, e.g., loading from a YAML file.

Arize Open AI Tracer for AutoGen

[ ]

Visualization of Prompts/Templates

[ ]

Example Task: Check Stock Price Change

In the example below, let's see how to use the agents in AutoGen to write a python script and execute the script. This process involves constructing a AssistantAgent to serve as the assistant, along with a UserProxyAgent that acts as a proxy for the human user. In this example demonstrated below, when constructing the UserProxyAgent, we select the human_input_mode to "NEVER". This means that the UserProxyAgent will not solicit feedback from the human user. It stops replying when the limit defined by max_consecutive_auto_reply is reached, or when is_termination_msg() returns true for the received message.

[ ]

The example above involves code execution. In AutoGen, code execution is triggered automatically by the UserProxyAgent when it detects an executable code block in a received message and no human user input is provided. This process occurs in a designated working directory, using a Docker container by default. Unless a specific directory is specified, AutoGen defaults to the autogen/extensions directory. Users have the option to specify a different working directory by setting the work_dir argument when constructing a new instance of the UserProxyAgent.

The whole chat is auto generated.

Example Task: Plot Chart

[ ]

Let's display the generated figure.

[ ]

Use a Different Code Execution Environment

The code execution happened in a separate process, so the plot is not directly displayed in the notebook. Is it possible to change the code execution environment into IPython?

Yes! In the following we demonstrate how to extend the UserProxyAgent to use a different code execution environment.

[ ]

The implementation overrides three functions in UserProxyAgent:

  • constructor. We get the ipython instance as the code execution environment.
  • generate_init_message. We generate a modified initial message to send to the assistant agent, by adding the info that the execution will be performed in IPython.
  • run_code. We execute the code with the ipython instance.

With the new IPythonUserProxyAgent, we are able to run the code within the current notebook environment and display plot directly.

[ ]