- January 9, 2025
- Posted by: Krishnan Srinivasan
- Category: AI/ML
In Part 1 of the multi-part blogs on Agentic AI, we saw a use case using CrewAI and created a strategy for logistics optimization using AI agents. In Part 2, we will explore AutoGen and create an Agentic AI-driven Travel Planner to plan itineraries for the intended destinations.
The Use Case – Planning the Itinerary and Collating Travel Tips:
We will build a fully functional AI-powered Travel Planner using AutoGen leveraging Agentic AI concepts. The Travel Planner will take user inputs for a travel destination and the number of days and return top itineraries and travel tips for the trip. The blog will guide you step-by-step through the code, from importing necessary libraries to creating agents and generating a downloadable travel plan.
What is AutoGen?
AutoGen is a cutting-edge framework from Microsoft Research designed to simplify the creation of agentic AI systems and multi-agent collaboration. It enables developers to build autonomous agents that can collaborate, make decisions, and interact with users and external systems in a seamless manner. The core concept of AutoGen revolves around creating dynamic workflows where agents, driven by specific objectives, work together to achieve a desired outcome.
Key Components of AutoGen:
- User Proxy Agent: The central coordinating agent that interacts with users and manages the flow of information between multiple assistant agents.
- Assistant Agents: Specialized agents responsible for handling specific tasks. These agents work independently but are coordinated by the User Proxy Agent.
- ConversableAgent: The ConversableAgent in AutoGen is a customizable class for building conversational AI agents capable of managing contextual interactions and collaborating with other agents in dynamic workflows. ConvesableAgents are not leveraged for this use case. It will be covered in an upcoming blog.
- External Tools and APIs: AutoGen agents often interact with external data sources, APIs, or web search tools to retrieve information and complete their tasks.
In the context of this use case, we will create two assistant agents – one each for preparing the Itinerary and Travel Tips respectively for the destination of choice. The Travel Planner user proxy agent will co-ordinate and manage the flow of information. The Serper API will be used as an external tool for searching the web and extracting the required information.
Let’s start with the step-by-step instructions.
Step 1: Importing Libraries
Before we begin building the agents, we need to import the necessary libraries that will be used throughout the code.
The main autogen library provides the AssistantAgent and UserProxyAgent classes.
Step 2: Creating the Web Search Tool
We need a tool to perform web searches and retrieve information. We will use the Serper API for this purpose. We load the SERPER_API_KEY from the environment variable stored locally.
The __init__ method initializes the tool and the search method sends a request to the Serper API with the provided query and returns the search results.
Step 3: Creating the Itinerary Agent
The Itinerary Agent is responsible for fetching top travel itineraries for a given destination and number of days.
The ItineraryAgent inherits from AssistantAgent.
The super() function is used in the constructor (__init__) to call the parent class’s constructor. This ensures that the AssistantAgent class is properly initialized with any necessary attributes or methods before adding additional functionality specific to the ItineraryAgent.
The provide_itineraries method takes the destination and number of days as inputs, constructs a query, and uses the WebSearchTool to fetch itineraries.
Note: The super() function in Python is used to call methods from a parent class. It allows a child class to inherit and extend the functionality of the parent class without rewriting the entire code.
Step 4: Creating the Travel Tips Agent
The Tips Agent fetches useful travel tips for the destination.
The TipsAgent inherits from AssistantAgent. The super() function is used to ensure that the base class AssistantAgent is properly initialized.
The provide_tips method constructs a query and uses the WebSearchTool to retrieve tips for the destination.
Step 5: Creating the Travel Planner User Proxy Agent
The TravelPlannerUserProxyAgent is the core class that orchestrates the entire workflow. It interacts with both the user and the assistant agents (ItineraryAgent and TipsAgent), consolidating the results into a structured travel plan.
The TravelPlannerUserProxyAgent plays a vital role in orchestrating the workflow by interacting with both assistant agents and consolidating their outputs into a structured travel plan. Here’s a breakdown of how this class works:
Initialization (__init__)
The __init__ method initializes two assistant agents:
ItineraryAgent: Handles fetching travel itineraries.
TipsAgent: Handles fetching travel tips.
The super() function ensures that the base UserProxyAgent class is properly initialized, inheriting any core functionality it provides.
User Interaction (initiate_trip_plan)
This method manages the interaction with the user and drives the entire trip planning process:
Prompts the user for inputs: The method asks the user to input their travel destination and the number of days for the trip.
Generates a structured output: It formats the user input into a header for the travel plan.
Fetching Itineraries
The method then calls provide_itineraries from the ItineraryAgent. It constructs a query based on the user input. If itineraries are found, they are formatted and added to the output.
Fetching Travel Tips
Similarly, the method calls provide_tips from the TipsAgent. It constructs a query to fetch travel tips for the destination.If tips are found, they are formatted and appended to the output.
To summarize, the results from both sections are formatted as follows:
– Itineraries Section: Lists each itinerary with an Itinerary Number, title, link, and description.
– Travel Tips Section: Lists each travel tip with a Travel Tip Number, title, link, and description.
Step 6: Saving the Output to a File
The save_to_txt method creates a text file with the itineraries and tips. The output is saved in a pipe-delimited format.
Step 7: Initialize and run the TravelPlannerUserProxyAgent. Print the output.
Step 8: Display the results in Markdown format for readability
A representative output for a user input with Switzerland as the destination for a three day trip is shown below.
Additionally, if required, a streamlit app can be built to better visualize the results. The screen shot of the travel planner app that was built for this use case is below. The destination and trip duration are user inputs, post which the respective assistant agents are invoked by the Travel Planner user proxy agent.
In this blog, we have built a Travel Planner using the AutoGen Agentic AI framework. The planner takes user inputs, fetches relevant itineraries and travel tips, and saves the output in a structured text file. This project demonstrates the power of AutoGen in building agent-driven applications. By leveraging AutoGen, developers can build intelligent systems that automate complex workflows, reduce manual intervention, and enhance user experiences.
If you are interested in exploring and implementing use cases in Data and AI, please feel free to reach out to us. Our expertise spans across various domains, including finance, travel, healthcare, retail, logistics, and more. We help businesses unlock the potential of their data through innovative data and AI solutions.