3.7 KiB
3.7 KiB
Flight Ticket Price Agent
This project implements a conversational AI agent that can find and analyze flight ticket prices. Users can ask for flight prices between two cities in a natural language chat interface, and the agent will use a combination of web search and language models to provide a summary of the costs.
Features
- Conversational Interface: A user-friendly chat interface built with Gradio.
- Multi-Model Support: Can be configured to use different Large Language Models (LLMs) for analysis, including:
- OpenAI (e.g., GPT-4o-mini)
- Google Gemini (e.g., Gemini 2.5 Flash)
- Ollama (e.g., Llama 3.1)
- Tool-Based Architecture: The agent uses a
get_ticket_pricetool to understand when the user is asking for flight information. - Web Scraping & Analysis:
- Uses Google Custom Search to find relevant web pages with flight information.
- Scrapes the content of the search results.
- Leverages an LLM to analyze the scraped text and extract the lowest, highest, and average prices for one-way and round-trip flights.
- Caching: Caches search results to provide faster responses for repeated queries.
- Currency Conversion: Includes a basic currency conversion table to standardize prices to INR.
Requirements
The project is built with Python and requires the following libraries:
python-dotenvopenaigoogle-generativeaiollamagradiorequestsbeautifulsoup4google-api-python-clientipython
You can install these dependencies using pip:
pip install python-dotenv openai google-generativeai ollama gradio requests beautifulsoup4 google-api-python-client ipython
Setup
-
Clone the repository (optional):
git clone <repository_url> cd <repository_directory>/ticket_price_agent -
Create a
.envfile: Create a file named.envin theticket_price_agentdirectory and add your API keys:OPENAI_API_KEY="your_openai_api_key" GEMINI_API_KEY="your_gemini_api_key" GOOGLE_SEARCH_KEY="your_google_search_api_key" GOOGLE_CSE_ID="your_google_custom_search_engine_id"GOOGLE_SEARCH_KEYandGOOGLE_CSE_IDare required for the Google Custom Search API.
-
Install Dependencies: Run the
pip installcommand mentioned in the "Requirements" section.
Usage
- Open the Notebook: Launch Jupyter Notebook or JupyterLab and open
ticket_price_agent.ipynb. - Run the Cells: Execute the cells in the notebook sequentially.
- Interact with the Agent: The final cell will start a Gradio chat interface. You can select the model you want to use (OpenAI or Gemini) from the dropdown menu.
- Ask for Prices: Start a conversation by asking for flight prices, for example:
- "How much is a ticket from Delhi to Mumbai?"
- "What's the flight cost to Kathmandu from Delhi?"
How It Works
- User Input: The user enters a message in the Gradio chat interface.
- Model Selection: The selected LLM (OpenAI or Gemini) processes the input.
- Tool Call: The model's function-calling/tool-using capability identifies that the user is asking for a price and calls the
get_ticket_pricefunction with the extracted departure and destination cities. - Google Search: The
get_ticket_pricefunction constructs a search query and uses the Google Custom Search API to find relevant links. - Web Scraping: The agent scrapes the content from the top search result pages.
- Price Analysis: The scraped content is passed to another LLM instance with a specific prompt to analyze the text and extract price information (lowest, highest, average).
- Response Generation: The final price summary is returned to the main chat model, which then formulates a user-friendly response.