Solving Complex Tasks with A Sequence of Nested Chats
Solve complex tasks with one or more sequence chats nested as inner monologue.
This notebook shows how you can leverage nested chats to solve complex task with AG2. Nested chats is a sequence of chats created by a receiver agent after receiving a message from a sender agent and finished before the receiver agent replies to this message. Nested chats allow AG2 agents to use other agents as their inner monologue to accomplish tasks. This abstraction is powerful as it allows you to compose agents in rich ways. This notebook shows how you can nest a pretty complex sequence of chats among inner agents inside an outer agent.
Learn more about the various ways to configure LLM endpoints here.
Example Task
Suppose we want the agents to complete the following sequence of tasks:
Since the first task could be complex to solve, lets construct new agents that can serve as an inner monologue.
Step 1. Define Agents
A Group Chat for Inner Monologue
Below, we construct a group chat manager which manages an
inner_assistant
agent and an inner_code_interpreter
agent. Later we
will use this group chat inside another agent.
Inner- and Outer-Level Individual Agents
Now we will construct a number of individual agents that will assume role of outer and inner agents.
Step 2: Orchestrate Nested Chats to Solve Tasks
Outer Level
In the following code block, at the outer level, we have communication between:
user
-assistant_1
for solving the first task, i.e.,tasks[0]
.user
-assistant_2
for solving the second task, i.e.,tasks[1]
.
Inner Level (Nested Chats)
Since the first task is quite complicated, we created a sequence of nested chats as the inner monologue of Assistant_1.
-
assistant_1
-manager
: This chat intends to delegate the task received by Assistant_1 to the Manager to solve. -
assistant_1
-writer
: This chat takes the output from Nested Chat 1, i.e., Assistant_1 vs. Manager, and lets the Writer polish the content to make an engaging and nicely formatted blog post, which is realized through the writing_message function. -
assistant_1
-reviewer
: This chat takes the output from Nested Chat 2 and intends to let the Reviewer agent review the content from Nested Chat 2. -
assistant_1
-writer
: This chat takes the output from previous nested chats and intends to let the Writer agent finalize a blog post.
The sequence of nested chats can be realized with the
register_nested_chats
function, which allows one to register one or a
sequence of chats to a particular agent (in this example, the
assistant_1
agent).
Information about the sequence of chats can be specified in the
chat_queue
argument of the register_nested_chats
function. The
following fields are especially useful: - recipient
(required)
specifies the nested agent; - message
specifies what message to send
to the nested recipient agent. In a sequence of nested chats, if the
message
field is not specified, we will use the last message the
registering agent received as the initial message in the first chat and
will skip any subsequent chat in the queue that does not have the
message
field. You can either provide a string or define a callable
that returns a string. - summary_method
decides what to get out of the
nested chat. You can either select from existing options including
"last_msg"
and "reflection_with_llm"
, or or define your own way on
what to get from the nested chat with a Callable. - max_turns
determines how many turns of conversation to have between the concerned
agent pairs.