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:
mkdir ai_travel_planner
cd ai_travel_planner
2. Initialize a Git Repository (Optional)
git init
3. Create a Virtual Environment
python3 -m venv venv
4. Activate the Virtual Environment
-
On Linux/macOS:
source venv/bin/activate
-
On Windows:
venv\Scripts\activate
Installing Dependencies
1. Upgrade pip
pip install --upgrade pip
2. Install AutoGen Packages
Install the required AutoGen packages and the OpenAI extension:
pip install 'autogen-agentchat==0.4.0.dev8' 'autogen-ext[openai]==0.4.0.dev8'
3. Install python-dotenv
for Environment Variables
pip 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
# user_agent.py
from autogen_agentchat.agents import UserProxyAgent
from autogen_agentchat.message import AssistantMessage
class UserAgent(UserProxyAgent):
pass # Inherits functionality from UserProxyAgent
2. FlightAgent
Handles flight-related queries and bookings.
Code: flight_agent.py
# flight_agent.py
import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_ext.models import OpenAIChatCompletionClient
async def search_flights(departure_city: str, destination_city: str, departure_date: str, return_date: str):
# Mock implementation of flight search
await asyncio.sleep(1) # Simulate network delay
return f"Found flights from {departure_city} to {destination_city} departing on {departure_date} and returning on {return_date}."
flight_agent = AssistantAgent(
name="FlightAgent",
model_client=OpenAIChatCompletionClient(
model="gpt-4",
# api_key will be loaded from environment variable
),
instructions="""
You are an AI agent specialized in booking flights. Assist in finding flights based on user preferences.
""",
tools=[search_flights],
)
3. HotelAgent
Handles accommodation queries and bookings.
Code: hotel_agent.py
# hotel_agent.py
import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_ext.models import OpenAIChatCompletionClient
async def search_hotels(destination_city: str, check_in_date: str, check_out_date: str):
# Mock implementation of hotel search
await asyncio.sleep(1) # Simulate network delay
return f"Found hotels in {destination_city} from {check_in_date} to {check_out_date}."
hotel_agent = AssistantAgent(
name="HotelAgent",
model_client=OpenAIChatCompletionClient(
model="gpt-4",
),
instructions="""
You are an AI agent specialized in booking accommodations. Assist in finding hotels based on user preferences.
""",
tools=[search_hotels],
)
4. ActivityAgent
Suggests activities at the destination.
Code: activity_agent.py
# activity_agent.py
import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_ext.models import OpenAIChatCompletionClient
async def suggest_activities(destination_city: str, interests: str):
# Mock implementation of activity suggestions
await asyncio.sleep(1) # Simulate processing time
return f"Suggested activities in {destination_city} based on your interests ({interests}): Visit the museum, explore downtown, enjoy local cuisine."
activity_agent = AssistantAgent(
name="ActivityAgent",
model_client=OpenAIChatCompletionClient(
model="gpt-4",
),
instructions="""
You are an AI agent specialized in suggesting activities and attractions. Provide recommendations based on user interests.
""",
tools=[suggest_activities],
)
Implementing the Main Program
We’ll now create the main script that ties everything together.
Code: main.py
# main.py
import asyncio
import os
from dotenv import load_dotenv
from autogen_agentchat.agents import UserProxyAgent
from autogen_agentchat.teams import SequentialTeam
from autogen_agentchat.task import Console
from autogen_ext.models import OpenAIChatCompletionClient
# Import agents
from flight_agent import flight_agent
from hotel_agent import hotel_agent
from activity_agent import activity_agent
# Load environment variables
load_dotenv()
openai_api_key = os.getenv("OPENAI_API_KEY")
# Ensure API key is set
if not openai_api_key:
raise ValueError("OPENAI_API_KEY is not set in the environment variables.")
# Set the API key for model clients
flight_agent.model_client.api_key = openai_api_key
hotel_agent.model_client.api_key = openai_api_key
activity_agent.model_client.api_key = openai_api_key
async def main():
# Create the user agent
user_agent = UserProxyAgent(
name="UserAgent",
)
# Define the travel planning team
travel_team = SequentialTeam(
agents=[
flight_agent,
hotel_agent,
activity_agent,
],
user_agent=user_agent,
)
# Initial user message
user_message = input("You: ")
# Run the team
stream = travel_team.run_stream(task=user_message)
await Console(stream)
if __name__ == "__main__":
asyncio.run(main())
Running the Application
1. Set Up Environment Variables
Create a .env
file in your project directory:
touch .env
Add your OpenAI API key to the .env
file:
# .env
OPENAI_API_KEY=your_openai_api_key_here
Note: Replace your_openai_api_key_here
with your actual API key.
2. Run the Application
python main.py
3. Interact with the Travel Planner
Example Interaction:
You: I want to plan a trip to Paris from New York next month.
FlightAgent: Found flights from New York to Paris departing on 2024-12-01 and returning on 2024-12-10.
HotelAgent: Found hotels in Paris from 2024-12-01 to 2024-12-10.
ActivityAgent: 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
asyncio
allows agents to perform tasks concurrently. - Mock Implementations: The functions
search_flights
,search_hotels
, andsuggest_activities
are 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
CarRentalAgent
orRestaurantAgent
.
Happy Coding!