Skip to main content
If you haven’t had a chance to read about or use AG2’s GroupChat orchestration, check out the GroupChat Overview for more information.
GroupChat’s four built-in conversation patterns, automatic, round robin, random, and manual provide a good degree of flexibility to orchestrate agents in a group chat setting. However, there are two additional methods to control the flow in a GroupChat:
  1. Use a Callable as a speaker selection method
  2. Define a graph specifying valid transitions
Here’s a walk-through of each of these methods.

1. Callable speaker selection method

By specifying a Callable as the speaker selection method for a GroupChat, a function is called after each agent replies and the agent it returns will be the next agent.

Setting up

Agents and workflow

We have 5 agents in our workflow:
  • Planner: Give a plan and revise.
  • Engineer: Retrieves papers from the web by writing code.
  • Executor: Executes code.
  • Scientist: Reads papers and writes summaries.
  • Admin (us): Our Human-in-the-loop approving or ending the chat.
The planned workflow is:
  1. The planner interact with Admin (user) to revise a plan. Only when the Admin (human) types “Approve” can we move to the next step.
  2. The engineer will write code to retrieve papers. The code will be executed by executor.
  3. When the code is executed successfully, the scientist will read the papers and write a summary.
  4. The summary will be reviewed by the Admin and give comments. When the Admin types “TERMINATE”, the process will be terminated.
Here’s the magic function that will be used at each turn to determine the next agent. By using the chat’s messages, retrieved through groupchat.messages we can evaluate where we are up to in the workflow and the next agent. If all fails, we will revert back to the random speaker selection method.

Create GroupChat and run

Make sure to assign the speaker selection function to the GroupChat.
In the output we can see that:
  • It transitioned straight to the Planner
  • After receiving approval from us it transitioned to the Engineer
  • After the Engineer it finds Python code goes to the executor to execute the code
  • The code ran successfully so it transitioned to the Scientist
  • After the Scientist it returned to us and we ended the chat

2. Specified flows using graphs

Using a graph, you can specify the valid transitions using the automatic speaker selection method for a GroupChat (speaker_selection_method="auto", which is the default). A graph is simply a dictionary specifying, for each agent, which agents they can transition to.
At each turn, the LLM will be presented with the valid agents based on the graph, limiting the transitions to those you’ve specified.
There are a number of interesting transition paths you can create with a graph, see the Finite State Machine documentation to explore them.
In this walk-through we’re going to create a game with three teams, each team has three players (with the first as a team leader). Each player has a number of chocolates that no other player knows about, even the team leader. The goal of the game is to tally up all the chocolates across the three teams, with each team leader responsible for tallying up their team’s chocolate count. To control the flow of the conversation, team leaders can transition to each other and transitions within a team can only be with their team members. Here’s what that should look like with our three teams, A, B, and C. Agents labelled zero will be the team leaders. Swarm Enhanced Demonstration
Here we dictionaries for our agents, the graph, and the number of chocolates for each agent
This looks more complicated than it is, we’re creating each agent for the three teams, giving them a random number of chocolates and explicit instructions on what they need to do. For each team we create the allowable transitions for inside that team, which are just with each other.
Here we create the transitions between the three team leaders who can transition to any other team leader.

Create GroupChat and run

Here we set the graph to the GroupChat when creating it using the allowed_or_disallowed_speaker_transitions parameter. We also specify speaker_transitions_type to “allowed” to indicate the provided transitions are allowed (alternative is “disallowed”).
Looking at the output, we can see there were 20 chocolates across all the teams and the agents worked through it methodically team by team.

More GroupChat examples

API

GroupChat GroupChatManager