Swarm
Swarms are a versatile pattern that provide flows between agents that are determined at the swarm or agent-level. The control of the flow is managed through various mechanisms, including hand-offs, post-tool transitions, post-work transitions, or an internal group chat manager, all of which determine the next agent (or end the swarm).
The simplest swarm comprises of a group of agents and utilizes the swarm’s internal manager (a GroupChatManager
) to decide, based on the conversation messages and the agent’s descriptions, who should be the next agent.
Here’s an example.
In this case, we create three agents, each with a detailed system message that will aid our swarm manager in working out the next best agent. There will also be a user agent (you) that may be transitioned to if the swarm manager thinks it needs further information or has finished.
Let’s examine the code:
-
Create our three agents
-
Initiate the chat
- All our agents are passed in to
agents
- Our starting message is set in
messages
- As we’re using our swarm manager to select agents, we need to give them an LLM configuration, so we pass it in
swarm_manager_args
- When each agent has spoken it will utilise the swarm’s
AfterWork
setting, in this caseAfterWorkOption.SWARM_MANAGER
set with theafter_work
parameter, indicating that the swarm manager is responsible for selecting the next agent.
- All our agents are passed in to
In this example, the swarm manager’s LLM is solely responsible for deciding the next agent at each turn.
Controlling swarm transitions
To gain more control over the transitions between agents, utilise hand-offs and after work transitions.
When designing your swarm, think about your agents in a diagram with the lines between agents being your hand-offs. Each line will have a condition statement which an LLM will evaluate. Control stays with an agent while they execute their tools and once they’ve finished with their tools the conditions to transition will be evaluated.
One of the unique aspects of a swarm is a shared context. ConversableAgents have a context dictionary but in a swarm that context is made common across all agents, allowing a state of the workflow to be maintained and viewed by all agents. This context can also be used within the hand off condition statements, providing more control of transitions.
AG2’s swarm has a number of unique capabilities, find out more in our Swarm deep-dive.
Here’s our lesson planner workflow using AG2’s Swarm.
-
Our shared context, available in function calls and on agents.
-
Functions that represent the work the agents carry out, these the update shared context and, optionally, managed transitions.
-
Agents setup with their tools,
functions
, and a system message and LLM configuration. -
The important hand-offs, defining the conditions for which to transfer to other agents and what to do after their work is finished (equivalent to no longer calling tools). Transfer conditions can be turned on/off using the
available
parameter. -
Kick off the swarm with our agents and shared context. Similar to
initiate_chat
,initiate_swarm_chat
returns the chat result (messages and summary) and the final shared context.