Introduction
Swarm is a lightweight, experimental multi-agent orchestration framework by OpenAI that allows you to coordinate and execute tasks between different agents. It's designed for developers who want fine-grained control over agent interactions. In this blog, we'll explore how to build a simple Travel Agent using Swarm that can help users search for the best hotels, flights, provide budget estimates, and even make bookings.
What is Swarm?
Swarm is an experimental Python framework aimed at building and orchestrating multi-agent systems. It's based on the Handoffs & Routines pattern, where agents can pass tasks to each other based on their specialties.
While Swarm is not production-ready, it's an exciting tool for exploring agentic workflows and multi-agent collaboration. Swarm agents use OpenAI's Chat Completions API
, meaning they are stateless between calls but can execute functions and coordinate tasks efficiently.
Setting Up Swarm
You'll need Python 3.10+ and Swarm installed. Run the following command to install:
bashCopy codepip install git+https://github.com/openai/swarm.git
Once installed, you can start building agents.
Travel Agent: Search Best Hotels, Flights, and Handle Bookings
For our example, we'll create a Travel Agent that can:
Search for the best flights.
Find top-rated hotels.
Provide budget estimates.
Make bookings.
We'll use Swarm to hand off these tasks between specialized agents.
Step-by-Step Code Implementation
- Create the Travel Agent
pythonCopy codefrom swarm import Swarm, Agent
# Initialize Swarm client
client = Swarm()
# Define the agents for the Travel Agent workflow
# Agent A: Flight Finder
def find_flights():
# Sample data for demonstration purposes
flights = [
{"flight": "Flight 101", "price": 500, "duration": "5h"},
{"flight": "Flight 202", "price": 450, "duration": "4.5h"}
]
return f"Best Flight: {flights[1]['flight']}, Price: ${flights[1]['price']}, Duration: {flights[1]['duration']}"
agent_flight_finder = Agent(
name="Flight Finder",
instructions="You search for the best flights.",
functions=[find_flights]
)
# Agent B: Hotel Finder
def find_hotels():
# Sample data for demonstration purposes
hotels = [
{"hotel": "Luxury Suites", "price_per_night": 200},
{"hotel": "Budget Inn", "price_per_night": 80}
]
return f"Best Hotel: {hotels[0]['hotel']}, Price per night: ${hotels[0]['price_per_night']}"
agent_hotel_finder = Agent(
name="Hotel Finder",
instructions="You search for the best hotels.",
functions=[find_hotels]
)
# Agent C: Budget Estimator
def calculate_budget(context_variables):
flight_price = context_variables.get("flight_price", 0)
hotel_price_per_night = context_variables.get("hotel_price_per_night", 0)
nights = context_variables.get("nights", 1)
total_budget = flight_price + (hotel_price_per_night * nights)
return f"Estimated Budget: ${total_budget}"
agent_budget_calculator = Agent(
name="Budget Estimator",
instructions="You calculate the total travel budget.",
functions=[calculate_budget]
)
# Agent D: Booking Agent
def book_trip():
return "Your booking is confirmed!"
agent_booking = Agent(
name="Booking Agent",
instructions="You handle travel bookings.",
functions=[book_trip]
)
# Travel Agent workflow
def travel_agent_workflow():
# Step 1: Find Flights
flight_response = client.run(
agent=agent_flight_finder,
messages=[{"role": "user", "content": "Find me the best flights."}]
)
flight_details = flight_response.messages[-1]["content"]
print(flight_details)
# Step 2: Find Hotels
hotel_response = client.run(
agent=agent_hotel_finder,
messages=[{"role": "user", "content": "Find me the best hotels."}]
)
hotel_details = hotel_response.messages[-1]["content"]
print(hotel_details)
# Step 3: Calculate Budget (hand off)
budget_response = client.run(
agent=agent_budget_calculator,
messages=[{"role": "user", "content": "Give me a budget estimate."}],
context_variables={"flight_price": 450, "hotel_price_per_night": 200, "nights": 3}
)
budget_details = budget_response.messages[-1]["content"]
print(budget_details)
# Step 4: Book the trip
booking_response = client.run(
agent=agent_booking,
messages=[{"role": "user", "content": "Please book my trip."}]
)
booking_details = booking_response.messages[-1]["content"]
print(booking_details)
# Run the travel agent workflow
travel_agent_workflow()
Explanation
Flight Finder: Searches for the best flights and returns the best option.
Hotel Finder: Finds top-rated hotels and provides pricing information.
Budget Estimator: Uses context variables to estimate the total budget for the trip.
Booking Agent: Confirms the travel booking.
The code demonstrates how agents collaborate by handing off tasks and updating context variables.
Running the Example
When you run the travel_agent_workflow
function, the Travel Agent will:
Find the best flight.
Recommend a hotel.
Calculate the total budget based on flight and hotel prices.
Complete the booking.
Conclusion
Swarm is an exciting framework for orchestrating multi-agent workflows. It allows for flexibility and control over how agents interact and hand off tasks. While it's still experimental, Swarm is a great tool for developers who want to explore agent-based systems in a lightweight environment.
In this example, we've built a Travel Agent that can find flights, hotels, calculate budgets, and handle bookings. Imagine expanding this to more complex travel planning tasks like transportation, tours, or even personalized recommendations!
Stay tuned for more exciting developments in the world of agentic AI!