Building Swarm-based agents with AG2
AG2 now provides an implementation of the swarm orchestration from OpenAI’s Swarm framework, with some additional features!
Background: the swarm orchestration is a multi-agent collaboration where agents execute tasks and are responsible for handing them off to other agents.
Here are the key features of the swarm orchestration:
- Hand-offs: Agents can transfer control to another agent, enabling smooth and direct transitions within workflows.
- Context variables: Agents can dynamically update a shared memory through function calls, maintaining context and adaptability throughout the process.
Besides these core features, AG2 provides:
- Simple Interface for Tool Call/Handoff Registration: When creating a swarm agent, you can pass in a list of functions to be registered directly. We also provide a simple method to register handoffs.
- Transition Beyond Tool Calls: We enable the ability to automatically transfer to a nominated swarm agent when an agent has completed their task. We will extend this to allow other transitions in the future (e.g., use a function to determine the next agent ).
- Built-in human-in-the-loop: Adding a user agent (UserProxyAgent) to your swarm will allow swarm agents to transition back to the user. Provides a means to clarify and confirm with the user without breaking out of the swarm.
This feature builds on GroupChat, offering a simpler interface to use swarm orchestration. For comparison, see two implementations of the same example: one using swarm orchestration and another naive implementation with GroupChat (Legacy).
Handoffs
Before we dive into a swarm example, an important concept in swarm orchestration is when and how an agent hands off to another agent.
Providing additional flexibility, we introduce the capability to define an after-work handoff. Think of it as the agent’s next action after completing their task. It can be to hand off to another agent, revert to the user, stay with the agent for another iteration, or terminate the conversation.
The following are the prioritized handoffs for each iteration of the swarm.
- Agent-level: Calls a tool that returns a swarm agent: A swarm agent’s tool call returns the next agent to hand off to.
- Agent-level: Calls a pre-defined conditional handoff: A swarm agent has an
ON_CONDITION
handoff that is chosen by the LLM (behaves like a tool call). - Agent-level: After work hand off: When no tool calls are made it can use an, optional,
AFTER_WORK
handoff that is a preset option or a nominated swarm agent. - Swarm-level: After work handoff: If the agent does not have an
AFTER_WORK
handoff, the swarm’sAFTER_WORK
handoff will be used.
In the following code sample a SwarmAgent
named responder
has:
- Two conditional handoffs registered (
ON_CONDITION
), specifying the agent to hand off to and the condition to trigger the handoff. - An after-work handoff (
AFTER_WORK
) nominated using one of the preset options (TERMINATE
,REVERT_TO_USER
,STAY
). This could also be a swarm agent.
You can specify the swarm-level after work handoff when initiating the swarm (here we nominate to terminate):
Creating a swarm
- Define the functions that can be used by your
SwarmAgent
s. - Create your
SwarmAgent
s (which derives fromConversableAgent
). - For each swarm agent, specify the handoffs (transitions to another agent) and what to do when they have finished their work (termed After Work).
- Optionally, create your context dictionary.
- Call
initiate_swarm_chat
.
Example
This example of managing refunds demonstrates the context handling, swarm and agent-level conditional and after work hand offs, and the human-in-the-loop feature.
And here’s the output, showing the flow of agents.
Notes
- Behind-the-scenes, swarm agents are supported by a tool execution agent, that executes tools on their behalf. Hence, the appearance of
Tool Execution
in the output. - Currently only swarm agents can be added to a swarm. This is to maintain their ability to manage context variables, auto-execute functions, and support hand offs. Eventually, we may allow ConversableAgent to have the same capability and make “SwarmAgent” a simpler subclass with certain defaults changed (like AssistantAgent and UserProxyAgent).
- Would you like to enhance the swarm feature or have found a bug? Please let us know by creating an issue on the AG2 GitHub.