Auto Generated Agent Chat: Collaborative Task Solving with Multiple Agents and Human Users
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 an application involving multiple
agents and human users to work together and accomplish a task.
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
. We create multiple
UserProxyAgent
instances that can represent different human users.
Requirements
AutoGen requires Python>=3.8
. To run this notebook example, please
install:
pip install autogen
# %pip install "autogen"
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 an environment variable of a specified name (“OAI_CONFIG_LIST” in this example), which needs to be a valid json string. If that variable is not found, it looks for a json file with the same name. It filters the configs by models (you can filter by other keys as well).
The json looks like the following:
[
{
"model": "gpt-4",
"api_key": "<your OpenAI API key here>"
},
{
"model": "gpt-4",
"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-4-32k",
"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.
import autogen
config_list = autogen.config_list_from_json(
"OAI_CONFIG_LIST",
filter_dict={
"model": ["gpt-4", "gpt4", "gpt-4-32k", "gpt-4-32k-0314", "gpt-4-32k-v0314"],
},
)
Construct Agents
We define ask_expert
function to start a conversation between two
agents and return a summary of the result. We construct an assistant
agent named “assistant_for_expert” and a user proxy agent named
“expert”. We specify human_input_mode
as “ALWAYS” in the user proxy
agent, which will always ask for feedback from the expert user.
def ask_expert(message):
assistant_for_expert = autogen.AssistantAgent(
name="assistant_for_expert",
llm_config={
"temperature": 0,
"config_list": config_list,
},
)
expert = autogen.UserProxyAgent(
name="expert",
human_input_mode="ALWAYS",
code_execution_config={
"work_dir": "expert",
"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.
)
expert.initiate_chat(assistant_for_expert, message=message)
expert.stop_reply_at_receive(assistant_for_expert)
# expert.human_input_mode, expert.max_consecutive_auto_reply = "NEVER", 0
# final message sent from the expert
expert.send("summarize the solution and explain the answer in an easy-to-understand way", assistant_for_expert)
# return the last message the expert received
return expert.last_message()["content"]
We construct another assistant agent named “assistant_for_student” and a
user proxy agent named “student”. We specify human_input_mode
as
“TERMINATE” in the user proxy agent, which will ask for feedback when it
receives a “TERMINATE” signal from the assistant agent. We set the
functions
in AssistantAgent
and function_map
in UserProxyAgent
to use the created ask_expert
function.
For simplicity, the ask_expert
function is defined to run locally. For
real applications, the function should run remotely to interact with an
expert user.
assistant_for_student = autogen.AssistantAgent(
name="assistant_for_student",
system_message="You are a helpful assistant. Reply TERMINATE when the task is done.",
llm_config={
"timeout": 600,
"cache_seed": 42,
"config_list": config_list,
"temperature": 0,
"functions": [
{
"name": "ask_expert",
"description": "ask expert when you can't solve the problem satisfactorily.",
"parameters": {
"type": "object",
"properties": {
"message": {
"type": "string",
"description": "question to ask expert. Ensure the question includes enough context, such as the code and the execution result. The expert does not know the conversation between you and the user unless you share the conversation with the expert.",
},
},
"required": ["message"],
},
}
],
},
)
student = autogen.UserProxyAgent(
name="student",
human_input_mode="TERMINATE",
max_consecutive_auto_reply=10,
code_execution_config={
"work_dir": "student",
"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.
function_map={"ask_expert": ask_expert},
)
Perform a task
We invoke the initiate_chat()
method of the student proxy agent to
start the conversation. When you run the cell below, you will be
prompted to provide feedback after the assistant agent sends a
“TERMINATE” signal at the end of the message. The conversation will
finish if you don’t provide any feedback (by pressing Enter directly).
Before the “TERMINATE” signal, the student proxy agent will try to
execute the code suggested by the assistant agent on behalf of the user.
# the assistant receives a message from the student, which contains the task description
student.initiate_chat(
assistant_for_student,
message="""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}.
""",
)
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}.
--------------------------------------------------------------------------------
To find the values of $a$, $b$, and $c$, we need to solve the system of equations. However, the system of equations you provided seems to be incomplete or incorrect. The equations are not properly formatted, and it's unclear how $x$ and $y$ are related to $a$, $b$, and $c$.
Could you please provide the correct system of equations?
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
--------------------------------------------------------------------------------
***** Suggested function Call: ask_expert *****
Arguments:
{
"message": "The user provided a system of equations and asked to find $a + b + c$, given that $x+y \\neq -1$ and \n\\begin{align}\n\tax + by + c & = x + 7,\\\n\ta + bx + cy & = 2x + 6y,\\\n\tay + b + cx & = 4x + y.\n\\end{align}. However, the system of equations seems to be incomplete or incorrectly formatted. How should I proceed?"
}
***********************************************
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
>>>>>>>> EXECUTING FUNCTION ask_expert...
The user provided a system of equations and asked to 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}. However, the system of equations seems to be incomplete or incorrectly formatted. How should I proceed?
--------------------------------------------------------------------------------
To proceed, we can first try to solve the given system of equations and see if we can find a unique solution for $a$, $b$, and $c$. If we can't find a unique solution, we can then determine if there's any additional information needed or if the system is indeed incomplete or incorrectly formatted.
Let's solve the given system of equations using Python's SymPy library.
--------------------------------------------------------------------------------
make sure you simplify the answer
--------------------------------------------------------------------------------
Sure, I will simplify the answer. Let's solve the given system of equations using Python's SymPy library and simplify the result.
--------------------------------------------------------------------------------
>>>>>>>> NO HUMAN INPUT RECEIVED.
>>>>>>>> USING AUTO REPLY...
--------------------------------------------------------------------------------
First, let's install the SymPy library if you haven't already. Execute the following command to install it:
```sh
pip install sympy
```
Now, let's solve the given system of equations using Python's SymPy library and simplify the result.
```python
from sympy import symbols, Eq, solve, simplify
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))
simplified_solutions = {key: simplify(value) for key, value in solutions.items()}
a_val, b_val, c_val = simplified_solutions[a], simplified_solutions[b], simplified_solutions[c]
sum_abc = simplify(a_val + b_val + c_val)
print(f"a: {a_val}")
print(f"b: {b_val}")
print(f"c: {c_val}")
print(f"a + b + c: {sum_abc}")
```
Please execute the above Python code to solve the system of equations and simplify the result.
--------------------------------------------------------------------------------
>>>>>>>> NO HUMAN INPUT RECEIVED.
>>>>>>>> USING AUTO REPLY...
>>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is sh)...
>>>>>>>> EXECUTING CODE BLOCK 1 (inferred language is python)...
exitcode: 0 (execution succeeded)
Code output:
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: sympy in /home/vscode/.local/lib/python3.9/site-packages (1.12)
Requirement already satisfied: mpmath>=0.19 in /home/vscode/.local/lib/python3.9/site-packages (from sympy) (1.3.0)
a: (x**2 - 3*x*y + 2*x + y**2 - y)/(x**2 - x*y - x + y**2 - y + 1)
b: (2*x**2 - 3*x + y)/(x**2 - x*y - x + y**2 - y + 1)
c: (4*x**2 - 4*x*y - 6*x + 6*y**2 - 7*y + 7)/(x**2 - x*y - x + y**2 - y + 1)
a + b + c: 7
--------------------------------------------------------------------------------
The given system of equations has been solved and simplified. The result is:
$$
a = \frac{x^2 - 3xy + 2x + y^2 - y}{x^2 - xy - x + y^2 - y + 1}, \\
b = \frac{2x^2 - 3x + y}{x^2 - xy - x + y^2 - y + 1}, \\
c = \frac{4x^2 - 4xy - 6x + 6y^2 - 7y + 7}{x^2 - xy - x + y^2 - y + 1}.
$$
The sum $a + b + c$ is equal to $7$. Since we were able to find a unique solution for $a$, $b$, and $c$, the system of equations is complete and correctly formatted.
TERMINATE
--------------------------------------------------------------------------------
>>>>>>>> NO HUMAN INPUT RECEIVED.
>>>>>>>> USING AUTO REPLY...
--------------------------------------------------------------------------------
TERMINATE
--------------------------------------------------------------------------------
>>>>>>>> NO HUMAN INPUT RECEIVED.
summarize the solution and explain the answer in an easy-to-understand way
--------------------------------------------------------------------------------
Sure, let me summarize the solution and explain the answer in an easy-to-understand way.
We were given a system of three equations with three unknowns, $a$, $b$, and $c$. The equations were:
1. $ax + by + c = x + 7$
2. $a + bx + cy = 2x + 6y$
3. $ay + b + cx = 4x + y$
We were asked to find the sum $a + b + c$ given that $x + y \neq -1$.
We used Python's SymPy library to solve the system of equations and found the following solutions for $a$, $b$, and $c$:
$$
a = \frac{x^2 - 3xy + 2x + y^2 - y}{x^2 - xy - x + y^2 - y + 1}, \\
b = \frac{2x^2 - 3x + y}{x^2 - xy - x + y^2 - y + 1}, \\
c = \frac{4x^2 - 4xy - 6x + 6y^2 - 7y + 7}{x^2 - xy - x + y^2 - y + 1}.
$$
These solutions are expressed in terms of $x$ and $y$. However, we were able to find the sum $a + b + c$ without knowing the values of $x$ and $y$. The sum $a + b + c$ simplifies to $7$.
In conclusion, the sum $a + b + c$ for the given system of equations is equal to $7$.
--------------------------------------------------------------------------------
***** Response from calling function "ask_expert" *****
Sure, let me summarize the solution and explain the answer in an easy-to-understand way.
We were given a system of three equations with three unknowns, $a$, $b$, and $c$. The equations were:
1. $ax + by + c = x + 7$
2. $a + bx + cy = 2x + 6y$
3. $ay + b + cx = 4x + y$
We were asked to find the sum $a + b + c$ given that $x + y \neq -1$.
We used Python's SymPy library to solve the system of equations and found the following solutions for $a$, $b$, and $c$:
$$
a = \frac{x^2 - 3xy + 2x + y^2 - y}{x^2 - xy - x + y^2 - y + 1}, \\
b = \frac{2x^2 - 3x + y}{x^2 - xy - x + y^2 - y + 1}, \\
c = \frac{4x^2 - 4xy - 6x + 6y^2 - 7y + 7}{x^2 - xy - x + y^2 - y + 1}.
$$
These solutions are expressed in terms of $x$ and $y$. However, we were able to find the sum $a + b + c$ without knowing the values of $x$ and $y$. The sum $a + b + c$ simplifies to $7$.
In conclusion, the sum $a + b + c$ for the given system of equations is equal to $7$.
*******************************************************
--------------------------------------------------------------------------------
The solution to the system of equations you provided is:
1. $a = \frac{x^2 - 3xy + 2x + y^2 - y}{x^2 - xy - x + y^2 - y + 1}$
2. $b = \frac{2x^2 - 3x + y}{x^2 - xy - x + y^2 - y + 1}$
3. $c = \frac{4x^2 - 4xy - 6x + 6y^2 - 7y + 7}{x^2 - xy - x + y^2 - y + 1}$
These solutions are expressed in terms of $x$ and $y$. However, we can find the sum $a + b + c$ without knowing the values of $x$ and $y$. The sum $a + b + c$ simplifies to $7$.
So, the sum $a + b + c$ for the given system of equations is equal to $7$.
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
--------------------------------------------------------------------------------
TERMINATE
--------------------------------------------------------------------------------
>>>>>>>> NO HUMAN INPUT RECEIVED.
When the assistant needs to consult the expert, it suggests a function
call to ask_expert
. When this happens, a line like the following will
be displayed:
***** Suggested function Call: ask_expert *****