OpenAI Swarm – launched in 2024, is an experimental framework designed to simplify the orchestration of multi-agent systems for developers. It aims to streamline the coordination of AI agents through scalable and user-friendly mechanisms, making it easier to manage interactions within complex workflows. As an open-source project available on GitHub, Swarm offers developers the opportunity to explore its features, experiment with its functionality, and contribute to its ongoing development. For machine learning professionals, it provides a powerful yet accessible tool to build and scale agent-based systems without the need for advanced orchestration skills.
This article was published as a part of the Data Science Blogathon.
OpenAI Swarm is a tool that helps manage multiple AI agents working together. It makes it easy to control and customize how these agents communicate and work on tasks. Think of it like a team of robots that can pass work between each other to get things done. Swarm is especially helpful when you need different agents to handle different parts of a task, or when the environment changes and agents need to make decisions based on that. It’s like having a team that can quickly adapt to solve problems.
Agents in CrewAI are structured with defined roles and responsibilities. Each agent is assigned a specific “Task” object that outlines the type of work it can perform, providing clarity and specificity in agent functions.
Swarm allows for more flexible agent behavior without imposing strict task limits. Agents can act independently and do not require a centralized manager as required in CrewAI, promoting a decentralized approach to task execution.
Autogen provides even more flexibility by emphasizing on dynamic collaboration among agents that can adjust their roles based on real-time task demands. Agents can work in pairs or groups, making the collaboration more fluid and adaptable compared to the more structured approach of OpenAI Swarm.
Swarm maintains persistent context by storing information through context_variables across agent interactions. Autogen provides a similar memory object to track relevant data for agent communication. CrewAI differentiates itself with an advanced memory object that manages both short- and long-term memory, automatically generating embeddings for key terms and memories.
Swarm defines functions with docstrings, which are useful for general purposes but can be challenging when detailed parameter descriptions are needed. Autogen uses function annotations to simplify customization of agent capabilities by specifying parameters. CrewAI allows agents to utilize tools from its own toolkit or Langchain, offering good compatibility.
AutoGen is particularly strong in code generation and managing intricate multi-agent programming workflows. On the other hand, OpenAI Swarm and CrewAI are designed to be user-friendly, making them perfect for beginners in multi-agent AI, as they don’t require complicated setups.
Also Read: How to Build AI Agents Using “Tool Use”?
Some of the potential use cases of a open AI Swarm could be the following –
┌─────────────────────────────────┐
│ SWARM ORCHESTRATOR │
│ - Receives user input │
│ - Initializes context_variables │
│ - Selects entry agent │
│ (wiki_agent) based on task │
└───────┬─────────────────────────┘
│ CONTEXT_VARIABLES SET
v
┌─────────────────────────────────┐
│ WIKI_AGENT │
│ Instructions: "Retrieve brand │
│ info from Wikipedia." │
└───────┬─────────────────────────┘
│ CALL FUNCTION: wikipedia_lookup(report_text)
v
┌─────────────────────────────────┐
│ WIKIPEDIA_LOOKUP() │
│ - Retrieves Wikipedia summary │
│ - Returns summary to WIKI_AGENT │
└───────┬─────────────────────────┘
│ SUMMARY RETURNED
v
┌─────────────────────────────────┐
│ WIKI_AGENT │
│ - Updates context_variables │
│ with retrieved summary │
│ - Handover to product_agent │
└───────┬─────────────────────────┘
│ CALL FUNCTION: transfer_to_product_agent()
v
┌─────────────────────────────────┐
│ PRODUCT_AGENT │
│ Instructions: "List key products │
│ from the retrieved summary only."│
└───────┬─────────────────────────┘
│ READ FROM context_variables (summary)
v
┌─────────────────────────────────┐
│ PRODUCT_AGENT (PROCESSING) │
│ - Extracts products from summary │
│ - Formats them into bullet points│
└───────┬─────────────────────────┘
│ RETURN FINAL RESULT
v
┌─────────────────────────────────┐
│ SWARM ORCHESTRATOR │
│ - Receives final product list │
│ - Returns response to user │
└───────┬─────────────────────────┘
│ RESPONSE
v
┌─────────────────────────────────┐
│ USER │
│ Receives bullet-pointed product │
│ list as final answer. │
└─────────────────────────────────┘
!pip install git+https://github.com/openai/swarm.git
!pip install wikipedia
We would be using the wikipedia library for this use case and hence would install it at this stage as well.
import os
os.environ['OPENAI_API_KEY']=''
from swarm import Swarm, Agent
import wikipedia
We would be using the wikipedia API for extracting information for this usecase. Hence, we install the wikipedia library as well.
def transfer_to_product_agent():
return product_agent
def wikipedia_lookup(context_variables):
try:
print("context variable",context_variables["report_text"])
summ = wikipedia.page(context_variables["report_text"]).summary
print("SUMM",summ)
return summ
except: return None
transfer_to_product_agent() – This function returns the product_agent object, which is defined in the next step (Step 5). The role of this function is to facilitate transferring control to a specialized agent for generating or handling summaries.
wikipedia_lookup() – This function attempts to retrieve a summary of a Wikipedia page based on the provided context_variables.
product_agent = Agent(
name="Product Agent",
instructions="LIST NAME OF the key PRODUCTS BY THE BRAND ONLY FROM THE RETRIEVED WIKIPEDIA INFORMATION in Bullet points. ONLY USE THE RETRIEVED INFORMATION to list the key products.DO NOT USE INFORMATION FROM OUTSIDE",
)
wiki_agent = Agent(
name="Agent",
instructions="""You are a helpful agent that answers user queries by finding and analysing information from Wikipedia.
You will be given a BRAND NAME and you must retrieve it's entry on Wikipedia and then hand over to the Summary Agent.""",
functions=[wikipedia_lookup, transfer_to_product_agent],
)
The provided code defines two agents, product_agent and wiki_agent, with specific roles and responsibilities
How it Works Together?
1. A user query provides a brand name.
2. `wiki_agent`:
– Searches for the brand on Wikipedia using the `wikipedia_lookup` function.
– Hands over the retrieved information to `product_agent` (or a similar processing agent).
3. `product_agent`:
– Extracts the product information from the Wikipedia content.
– Lists the products in bullet points.
client = Swarm()
# Run summary agent
text = "philips"
response = client.run(
agent=wiki_agent ,
messages=[{"role": "user", "content": text}],
context_variables={"report_text": text}
)
print(response.messages[-1]["content"])
client.run: The method run is called on the client object, indicating it’s responsible for sending a query to the AI agent and returning a response.
Parameters:
Output
As we can see from the output, all the key products of the Philips brand have been listed here. The key product information, as instructed in the agentic system, is retrieved from the wikipedia page. We can see that in the output text fetched from the wikipedia page.
#Fetch the name of the last executed agent
response.agent.name
As we can see from the image above, the last executed agent was “Summary agent”, even though we ran the client on the “wiki_agent”. This means internally transfer to the Summary agent was done post extraction of information from Wikipedia on the “Philips” brand. The Summary agent, as instructed, gave us the list of the key products of “Philips” from the extracted information.
In conclusion, OpenAI Swarm provides a robust framework for managing and coordinating multiple AI agents effectively. Its ability to assign specific roles, utilize JSON-based task structures, and enable seamless task handoffs ensures efficiency in handling complex workflows. The use of context variables further enhances collaboration by maintaining consistency and continuity among agents. Swarm’s adaptability makes it an invaluable tool for dynamic environments requiring flexible and intelligent task management. This innovation positions it as a versatile solution for streamlining multi-agent interactions.
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.
A. OpenAI Swarm is a system designed to manage multiple AI agents working collaboratively. Each agent is assigned a specific role and tasks, organized in JSON structures, which helps streamline their coordination and ensures clarity in their responsibilities.
A. Swarm facilitates smooth transitions between agents through handoff functions guided by conversational flow or predefined rules. Context variables play a key role in maintaining and updating shared information, ensuring all agents work with accurate and consistent data.
A. While tools like Autogen excel in handling complex workflows and code generation, OpenAI Swarm prioritizes simplicity and accessibility. This makes it an excellent choice for beginners, as it requires minimal configuration and offers straightforward agent management.
A. OpenAI Swarm is versatile, enabling applications such as virtual customer support, smart personal assistants, real-time data workflows, and enhanced retail experiences. It ensures efficiency by delegating tasks to specialized agents and adapting to dynamic scenarios.
A. Swarm uses context variables to enable agents to share and update important information. This ensures that all agents involved in the task or conversation maintain consistency and work with the most current data.