Sovereign AI Ecosystem

'AI Travel Planner with Microsoft AutoGen: Multi-Agent Collaboration' [post] deterministic

![Image](/images/ComfyUI_00204_.png) # Building an AI Travel Planner with AutoGen: A Step-by-Step Guide This guide will help you create an AI-powered travel planner using Micro

Microsoft AutogenMulti-Agent SystemsOpenAITravel PlanningAI Collaboration

![Image](/images/ComfyUI_00204_.png)

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

1. [Introduction](#introduction) 2. [Prerequisites](#prerequisites) 3. [Project Setup](#project-setup) 4. [Installing Dependencies](#installing-dependencies) 5. [Creating the Agents](#creating-the-agents) - [1. UserAgent](#1-useragent) - [2. FlightAgent](#2-flightagent) - [3. HotelAgent](#3-hotelagent) - [4. ActivityAgent](#4-activityagent) 6. [Implementing the Main Program](#implementing-the-main-program) 7. [Running the Application](#running-the-application) 8. [Conclusion](#conclusion) 9. [Additional Notes](#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](https://platform.openai.com/account/api-keys).
  • **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:

bash mkdir ai_travel_planner cd ai_travel_planner

2. Initialize a Git Repository (Optional)

bash git init

3. Create a Virtual Environment

bash python3 -m venv venv

4. Activate the Virtual Environment

  • On **Linux/macOS**:

bash source venv/bin/activate

  • On **Windows**:

bash venv\Scripts\activate

---

Installing Dependencies

1. Upgrade pip

bash pip install --upgrade pip

2. Install AutoGen Packages

Install the required AutoGen packages and the OpenAI extension:

bash pip install 'autogen-agentchat==0.4.0.dev8' 'autogen-ext[openai]==0.4.0.dev8'

3. Install python-dotenv for Environment Variables

bash pip install python-dotenv

---

Creating the Agents

We'll create four agents:

1. **UserAgent**: Interacts with the user to gather preferences. 2. **FlightAgent**: Handles flight booking queries. 3. **HotelAgent**: Handles accommodation booking. 4. **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**

```python # 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**

```python # 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**

```python # 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**

```python # 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**

```python # 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:

bash touch .env

Add your OpenAI API key to the .env file:

ini # .env OPENAI_API_KEY=your_openai_api_key_here

**Note:** Replace your_openai_api_key_here with your actual API key.

Sources

DanielKliewer.com blog · source

Related (0)

No recorded relationships.

← all Blog