Skip to main content
Open In Colab Open on GitHub AG2 offers conversable agents, powered by LLMs, tools or a human, that can perform tasks collectively via an automated chat. Recently, OpenAI released a Swarm framework that focuses on making agent coordination and execution lightweight. In AG2 we offer a simple programming interface to build and orchestrate a swarm of agents. Please check the Documentation and Blog for more details. After learning the fundamentals of AG2’s swarm in this notebook, check out this notebook where we take on some more advanced techniques that provide greater control and predicability for your swarms. In this notebook, we implement OpenAI’s airline customer service example in AG2.
This notebook has been updated as swarms can now accommodate any ConversableAgent.
Install ag2:
Note: If you have been using autogen or ag2, all you need to do is upgrade it using:
or
as autogen, and ag2 are aliases for the same PyPI package.
For more information, please refer to the installation guide.

Set your API Endpoint

The config_list_from_json function loads a list of configurations from an environment variable or a json file.

Prompts & Utility Functions

The prompts and utility functions remain unchanged from the original example.

Define Agents and register functions

Register Handoffs

Now we register the handoffs for the agents. Note that you don’t need to define the transfer functions and pass them in. Instead, you can directly register the handoffs using the OnCondition class.

Run the code

Finally, call initiate_swarm_chat to start the conversation. For this example, it requires human interaction with the agents. So we pass in a user agent, and set the after_work parameter to AfterWorkOption.REVERT_TO_USER. This means, each time a swarm agent finishes its work, the conversation will be reverted back to the user to give the next input.
You need to interact with the agents for this example. (You can try different inputs to see how they react!)
Here is a sequence of messages entered in this example: To initialize the conversation, we pass I want to cancel flight to the initiate_swarm_chat function. 1. please cancel it (Ask for reason but don’t provide one) 2. 1919 (The flight number) 3. I want flight credits 4. No (No further questions) 5. exit (End the conversation)