Open In Colab Open on GitHub

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.

In this notebook, we demonstrate how to use AssistantAgent and UserProxyAgent to solve a challenging math problem with human feedback. 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 a user to execute the code written by AssistantAgent. By setting human_input_mode properly, the UserProxyAgent can also prompt the user for feedback to AssistantAgent. For example, when human_input_mode is set to “ALWAYS”, the UserProxyAgent will always prompt the user for feedback. When user feedback is provided, the UserProxyAgent will directly pass the feedback to AssistantAgent. When no user feedback is provided, the UserProxyAgent will execute the code written by AssistantAgent and return the execution results (success or failure and corresponding outputs) to AssistantAgent.

Requirements

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

pip install pyautogen
# %pip install "pyautogen>=0.2.3"

Set your API Endpoint

The config_list_from_json function loads a list of configurations from an environment variable or a json file.

import json

import autogen

config_list = autogen.config_list_from_json("OAI_CONFIG_LIST")

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.

The config list looks like the following:

config_list = [
    {
        'model': 'gpt-4',
        'api_key': '<your OpenAI API key here>',
    },
    {
        'model': 'gpt-3.5-turbo',
        'api_key': '<your Azure OpenAI API key here>',
        'base_url': '<your Azure OpenAI API base here>',
        'api_type': 'azure',
        'api_version': '2024-02-01',
    },
    {
        'model': 'gpt-3.5-turbo-16k',
        'api_key': '<your Azure OpenAI API key here>',
        'base_url': '<your Azure OpenAI API base here>',
        'api_type': 'azure',
        'api_version': '2024-02-01',
    },
]

You can set the value of config_list in any way you prefer. Please refer to this notebook for full code examples of the different methods.

Construct Agents

We construct the assistant agent and the user proxy agent.

# create an AssistantAgent instance named "assistant"
assistant = autogen.AssistantAgent(
    name="assistant",
    llm_config={
        "cache_seed": 41,
        "config_list": config_list,
    },
)
# create a UserProxyAgent instance named "user_proxy"
user_proxy = autogen.UserProxyAgent(
    name="user_proxy",
    human_input_mode="ALWAYS",
    is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE"),
    code_execution_config={
        "use_docker": False
    },  # Please set use_docker=True if docker is available to run the generated code. Using docker is safer than running the generated code directly.
)

Perform a task

We invoke the initiate_chat() method of the user proxy agent to start the conversation. When you run the cell below, you will be prompted to provide feedback after receiving a message from the assistant agent. If you don’t provide any feedback (by pressing Enter directly), the user proxy agent will try to execute the code suggested by the assistant agent on behalf of you, or terminate if the assistant agent sends a “TERMINATE” signal at the end of the message.

math_problem_to_solve = """
Find $a + b + c$, given that $x+y \\neq -1$ and
\\begin{align}
    ax + by + c & = x + 7,\\
    a + bx + cy & = 2x + 6y,\\
    ay + b + cx & = 4x + y.
\\end{align}.
"""

# the assistant receives a message from the user, which contains the task description
user_proxy.initiate_chat(assistant, message=math_problem_to_solve)
user_proxy (to assistant):


Find $a + b + c$, given that $x+y \neq -1$ and 
\begin{align}
    ax + by + c & = x + 7,\
    a + bx + cy & = 2x + 6y,\
    ay + b + cx & = 4x + y.
\end{align}.


--------------------------------------------------------------------------------
assistant (to user_proxy):

To solve the system of linear equations, I will use NumPy library in Python to calculate the coefficients and constants and then use the linalg.solve() function to find the values of a, b, and c. Finally, I will add them up to find $a + b + c$.

Step 1: Define the coefficients and constants

Step 2: Solve the system of equations using NumPy

Step 3: Calculate the sum of a, b, and c

--------------------------------------------------------------------------------
user_proxy (to assistant):

use sympy

--------------------------------------------------------------------------------
assistant (to user_proxy):

Sure, I will use Sympy to solve the system of linear equations.

Step 1: Import Sympy library and define the symbols
``` python
from sympy import symbols, Eq, solve

a, b, c, x, y = symbols('a b c x y')
```

Step 2: Define the equations
``` python
eq1 = Eq(a * x + b * y + c, x + 7)
eq2 = Eq(a + b * x + c * y, 2 * x + 6 * y)
eq3 = Eq(a * y + b + c * x, 4 * x + y)
```

Step 3: Solve the system of equations
``` python
solutions = solve((eq1, eq2, eq3), (a, b, c))
```

Step 4: Calculate the sum of a, b, and c
``` python
a_value, b_value, c_value = solutions[a], solutions[b], solutions[c]
sum_abc = a_value + b_value + c_value
```



--------------------------------------------------------------------------------

>>>>>>>> NO HUMAN INPUT RECEIVED.

>>>>>>>> USING AUTO REPLY...

>>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...
user_proxy (to assistant):

exitcode: 1 (execution failed)
Code output: 
  File "", line 2
    Step 2: Define the equations
         ^
SyntaxError: invalid syntax


--------------------------------------------------------------------------------
assistant (to user_proxy):

Apologies for the confusion. I should have provided the code in a single block without the step-by-step comments. Here is the code to solve the system of linear equations using Sympy:

```python
from sympy import symbols, Eq, solve

a, b, c, x, y = symbols('a b c x y')

eq1 = Eq(a * x + b * y + c, x + 7)
eq2 = Eq(a + b * x + c * y, 2 * x + 6 * y)
eq3 = Eq(a * y + b + c * x, 4 * x + y)

solutions = solve((eq1, eq2, eq3), (a, b, c))

a_value, b_value, c_value = solutions[a], solutions[b], solutions[c]
sum_abc = a_value + b_value + c_value
print(sum_abc)
```

Please run the provided code to find the sum of a, b, and c.

--------------------------------------------------------------------------------

>>>>>>>> NO HUMAN INPUT RECEIVED.

>>>>>>>> USING AUTO REPLY...

>>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...
user_proxy (to assistant):

exitcode: 0 (execution succeeded)
Code output: 
(2*x**2 - 3*x + y)/(x**2 - x*y - x + y**2 - y + 1) + (x**2 - 3*x*y + 2*x + y**2 - y)/(x**2 - x*y - x + y**2 - y + 1) + (4*x**2 - 4*x*y - 6*x + 6*y**2 - 7*y + 7)/(x**2 - x*y - x + y**2 - y + 1)


--------------------------------------------------------------------------------
assistant (to user_proxy):

I apologize for the confusion. The code provided the sum of a, b, and c in a symbolic representation. To get an answer, we need to put some constraints on x and y for which x + y is not equal to -1.

We can choose any values for x and y that don't satisfy x + y = -1, and then substitute them into the equation to find the corresponding sum of a, b, and c. Let's use x = 1 and y = 0, which satisfy the given condition.

Here is the updated code with the chosen values for x and y:

```python
from sympy import symbols, Eq, solve

a, b, c, x, y = symbols('a b c x y')

eq1 = Eq(a * x + b * y + c, x + 7)
eq2 = Eq(a + b * x + c * y, 2 * x + 6 * y)
eq3 = Eq(a * y + b + c * x, 4 * x + y)

solutions = solve((eq1, eq2, eq3), (a, b, c))

chosen_x = 1
chosen_y = 0

a_value, b_value, c_value = solutions[a].subs(x, chosen_x).subs(y, chosen_y), solutions[b].subs(x, chosen_x).subs(y, chosen_y), solutions[c].subs(x, chosen_x).subs(y, chosen_y)
sum_abc = a_value + b_value + c_value
print(sum_abc)
```

Please run the provided code again to find the sum of a, b, and c.

--------------------------------------------------------------------------------

>>>>>>>> NO HUMAN INPUT RECEIVED.

>>>>>>>> USING AUTO REPLY...

>>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...
user_proxy (to assistant):

exitcode: 0 (execution succeeded)
Code output: 
7


--------------------------------------------------------------------------------
assistant (to user_proxy):

Great! Now we have the sum of a, b, and c. The answer is:

$$a + b + c = 7$$

TERMINATE

--------------------------------------------------------------------------------

>>>>>>>> NO HUMAN INPUT RECEIVED.

Analyze the conversation

The human user can provide feedback at each step. When the human user didn’t provide feedback, the code was executed. The executed results and error messages are returned to the assistant, and the assistant is able to modify the code based on the feedback. In the end, the task is complete and a “TERMINATE” signal is sent from the assistant. The user skipped feedback in the end and the conversation is finished.

After the conversation is finished, we can save the conversations between the two agents. The conversation can be accessed from user_proxy.chat_messages.

print(user_proxy.chat_messages[assistant])
json.dump(user_proxy.chat_messages[assistant], open("conversations.json", "w"), indent=2)