AI Travel Planner with Microsoft AutoGen: Multi-Agent Collaboration
Daniel Kliewer
Author, Sovereign AI


Building an AI Travel Planner with AutoGen: A Step-by-Step Guide
This guide will help you create an AI-powered travel planner using Microsoft's AutoGen framework. The application will utilize multiple AI agents to collaborate and plan a personalized travel itinerary based on user preferences. We'll use Python and the AgentChat API of AutoGen to build this system.
Table of Contents
- Introduction
- Prerequisites
- Project Setup
- Installing Dependencies
- Creating the Agents
- Implementing the Main Program
- Running the Application
- Conclusion
- Additional Notes
Introduction
AutoGen is an open-source framework for building AI agent systems. It simplifies the creation of event-driven, distributed, scalable, and resilient agentic applications. In this guide, we'll build an AI Travel Planner where different AI agents collaborate to plan a travel itinerary based on user input.
Use Case: An AI Travel Planner that interacts with the user to gather preferences and coordinates multiple specialized agents (FlightAgent, HotelAgent, ActivityAgent) to plan flights, accommodations, and activities.
Prerequisites
- Python 3.8+ installed on your machine.
- OpenAI API Key: Obtain one from OpenAI.
- Terminal Access: Ability to run commands in your operating system's terminal.
- Git (optional): For version control.
- Basic Knowledge of Python: Understanding of Python programming and asynchronous programming with
asyncio.
Project Setup
1. Create a Project Directory
Open your terminal and create a new directory for the project:
bash1mkdir ai_travel_planner2cd ai_travel_planner
2. Initialize a Git Repository (Optional)
bash1git init
3. Create a Virtual Environment
bash1python3 -m venv venv
4. Activate the Virtual Environment
-
On Linux/macOS:
bash1source venv/bin/activate -
On Windows:
bash1venv\Scripts\activate
Installing Dependencies
1. Upgrade pip
bash1pip install --upgrade pip
2. Install AutoGen Packages
Install the required AutoGen packages and the OpenAI extension:
bash1pip install 'autogen-agentchat==0.4.0.dev8' 'autogen-ext[openai]==0.4.0.dev8'
3. Install python-dotenv for Environment Variables
bash1pip install python-dotenv
Creating the Agents
We'll create four agents:
- UserAgent: Interacts with the user to gather preferences.
- FlightAgent: Handles flight booking queries.
- HotelAgent: Handles accommodation booking.
- ActivityAgent: Suggests activities based on destination.
1. UserAgent
This agent will initiate the conversation with the user, gather preferences, and coordinate with other agents.
Code: user_agent.py
python1# user_agent.py23from autogen_agentchat.agents import UserProxyAgent4from autogen_agentchat.message import AssistantMessage56class UserAgent(UserProxyAgent):7 pass # Inherits functionality from UserProxyAgent
2. FlightAgent
Handles flight-related queries and bookings.
Code: flight_agent.py
python1# flight_agent.py23import asyncio4from autogen_agentchat.agents import AssistantAgent5from autogen_ext.models import OpenAIChatCompletionClient67async def search_flights(departure_city: str, destination_city: str, departure_date: str, return_date: str):8 # Mock implementation of flight search9 await asyncio.sleep(1) # Simulate network delay10 return f"Found flights from {departure_city} to {destination_city} departing on {departure_date} and returning on {return_date}."1112flight_agent = AssistantAgent(13 name="FlightAgent",14 model_client=OpenAIChatCompletionClient(15 model="gpt-4",16 # api_key will be loaded from environment variable17 ),18 instructions="""19You are an AI agent specialized in booking flights. Assist in finding flights based on user preferences.20""",21 tools=[search_flights],22)
3. HotelAgent
Handles accommodation queries and bookings.
Code: hotel_agent.py
python1# hotel_agent.py23import asyncio4from autogen_agentchat.agents import AssistantAgent5from autogen_ext.models import OpenAIChatCompletionClient67async def search_hotels(destination_city: str, check_in_date: str, check_out_date: str):8 # Mock implementation of hotel search9 await asyncio.sleep(1) # Simulate network delay10 return f"Found hotels in {destination_city} from {check_in_date} to {check_out_date}."1112hotel_agent = AssistantAgent(13 name="HotelAgent",14 model_client=OpenAIChatCompletionClient(15 model="gpt-4",16 ),17 instructions="""18You are an AI agent specialized in booking accommodations. Assist in finding hotels based on user preferences.19""",20 tools=[search_hotels],21)
4. ActivityAgent
Suggests activities at the destination.
Code: activity_agent.py
python1# activity_agent.py23import asyncio4from autogen_agentchat.agents import AssistantAgent5from autogen_ext.models import OpenAIChatCompletionClient67async def suggest_activities(destination_city: str, interests: str):8 # Mock implementation of activity suggestions9 await asyncio.sleep(1) # Simulate processing time10 return f"Suggested activities in {destination_city} based on your interests ({interests}): Visit the museum, explore downtown, enjoy local cuisine."1112activity_agent = AssistantAgent(13 name="ActivityAgent",14 model_client=OpenAIChatCompletionClient(15 model="gpt-4",16 ),17 instructions="""18You are an AI agent specialized in suggesting activities and attractions. Provide recommendations based on user interests.19""",20 tools=[suggest_activities],21)
Implementing the Main Program
We'll now create the main script that ties everything together.
Code: main.py
python1# main.py23import asyncio4import os5from dotenv import load_dotenv6from autogen_agentchat.agents import UserProxyAgent7from autogen_agentchat.teams import SequentialTeam8from autogen_agentchat.task import Console9from autogen_ext.models import OpenAIChatCompletionClient1011# Import agents12from flight_agent import flight_agent13from hotel_agent import hotel_agent14from activity_agent import activity_agent1516# Load environment variables17load_dotenv()18openai_api_key = os.getenv("OPENAI_API_KEY")1920# Ensure API key is set21if not openai_api_key:22 raise ValueError("OPENAI_API_KEY is not set in the environment variables.")2324# Set the API key for model clients25flight_agent.model_client.api_key = openai_api_key26hotel_agent.model_client.api_key = openai_api_key27activity_agent.model_client.api_key = openai_api_key2829async def main():30 # Create the user agent31 user_agent = UserProxyAgent(32 name="UserAgent",33 )3435 # Define the travel planning team36 travel_team = SequentialTeam(37 agents=[38 flight_agent,39 hotel_agent,40 activity_agent,41 ],42 user_agent=user_agent,43 )4445 # Initial user message46 user_message = input("You: ")4748 # Run the team49 stream = travel_team.run_stream(task=user_message)50 await Console(stream)5152if __name__ == "__main__":53 asyncio.run(main())
Running the Application
1. Set Up Environment Variables
Create a .env file in your project directory:
bash1touch .env
Add your OpenAI API key to the .env file:
ini1# .env2OPENAI_API_KEY=your_openai_api_key_here
Note: Replace your_openai_api_key_here with your actual API key.
2. Run the Application
bash1python main.py
3. Interact with the Travel Planner
Example Interaction:
text1You: I want to plan a trip to Paris from New York next month.23FlightAgent: Found flights from New York to Paris departing on 2024-12-01 and returning on 2024-12-10.45HotelAgent: Found hotels in Paris from 2024-12-01 to 2024-12-10.67ActivityAgent: Suggested activities in Paris based on your interests (art, history): Visit the Louvre Museum, explore the Eiffel Tower, enjoy local French cuisine.
Conclusion
You've successfully built an AI Travel Planner using AutoGen! This application demonstrates how multiple AI agents can collaborate to perform complex tasks. Each agent specializes in a particular domain and communicates to provide a cohesive service to the user.
Additional Notes
- Asynchronous Programming: The use of
asyncioallows agents to perform tasks concurrently. - Mock Implementations: The functions
search_flights,search_hotels, andsuggest_activitiesare mock implementations. In a real-world application, you'd integrate with actual APIs. - Error Handling: For production use, add proper error handling and input validation.
- Extensibility: You can extend this application by adding more agents, such as a
CarRentalAgentorRestaurantAgent.
Happy Coding!

Sovereign AI: Building Local-First Intelligent Systems
by Daniel Kliewer · Paperback · 72 pages
The hands-on guide to building AI that runs on your hardware, keeps your data private, and eliminates cloud dependence. Working code included.