Skip to content

Updated AI-Travel-Agent sample with latest changes #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
724 changes: 304 additions & 420 deletions AI-Travel-Agent/AI_Travel_Agent.ipynb

Large diffs are not rendered by default.

36 changes: 20 additions & 16 deletions AI-Travel-Agent/AI_Travel_Agent_streamlit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
# streamlit run AI_Travel_Agent_streamlit.py

# Importing necessary modules

import os
import re
import time
import streamlit as st
from amadeus import Client
from dotenv import load_dotenv
from huggingface_hub import hf_hub_download
from langchain_community.llms import LlamaCpp
from langchain_core.callbacks import CallbackManager, StreamingStdOutCallbackHandler
from langchain_community.utilities import GoogleSerperAPIWrapper
Expand All @@ -27,10 +27,10 @@
def create_llm():
"""
Create and initialize the LlamaCpp with the selected model.
Parameters can be changed based on the end user requirements.
Here we are using Meta Llama 3.1(Q4_K_S) model which is configured using some hyperparameters.
For example, GPU Layers to be offloaded on all the available layers for inference.
Context Length of 4096 tokens. Temperature set as 0 for deterministic output.
Parameters can be changed based on the end user's requirements.
Here we are using the Meta Llama 3.1(Q4_K_S) model, which is configured using some hyperparameters.
For example, GPU Layers are to be offloaded to all the available layers for inference.
Context Length of 4096 tokens. The temperature is set as 0 for deterministic output.
Top-P Sampling as 0.95 for controlled randomness, Batch Size as 512 for parallel processing

Returns:
Expand All @@ -40,10 +40,12 @@ def create_llm():
Exception: If there is any error during model loading, a Streamlit error is displayed.
"""
try:
model_path = hf_hub_download(repo_id="bartowski/Meta-Llama-3.1-8B-Instruct-GGUF", filename="Meta-Llama-3.1-8B-Instruct-Q4_K_S.gguf") # Downloading the model here

llm = LlamaCpp(
# Path to the Llama model file
model_path="./models/Meta-Llama-3.1-8B-Instruct-Q4_K_S.gguf",
# Number of layers to be loaded into gpu memory (default: 0)
model_path=model_path,
# Number of layers to be loaded into GPU memory (default: 0)
n_gpu_layers=-1,
# Random number generator (RNG) seed (default: -1, -1 = random seed)
seed=512,
Expand Down Expand Up @@ -112,7 +114,8 @@ def create_prompt_template():

FORMAT_INSTRUCTIONS = """Use a json blob to specify a tool by providing an action key (tool name) and an action_input key (tool input).

Use the closest_airport tool and single_flight_search tool for any flight related queries. Give all the flight details including Flight Number, Carrier, Departure time, Arrival time and Terminal details to the human.
Use the closest_airport tool and single_flight_search tool for any flight related queries.
Give all the flight details including Flight Number, Carrier, Departure time, Arrival time and Terminal details to the human.
Use the Google Search tool and knowledge base for any itinerary-related queries. Give all the detailed information on tourist attractions, must-visit places, and hotels with ratings to the human.
Use the Google Search tool for distance calculations. Give all the web results to the human.
Always consider the traveler's preferences, budget constraints, and any specific requirements mentioned in their query.
Expand Down Expand Up @@ -144,7 +147,8 @@ def create_prompt_template():
}}}}
```[/INST]"""

SUFFIX = """Begin! Reminder to ALWAYS respond with a valid json blob of a single action. Use tools if necessary. Respond directly if appropriate. Format is Action:```$JSON_BLOB```then Observation:.
SUFFIX = """Begin! Reminder to ALWAYS respond with a valid json blob of a single action. Use tools if necessary. Respond directly if appropriate.
Format is Action:```$JSON_BLOB```then Observation:.
Thought:[INST]"""

HUMAN_MESSAGE_TEMPLATE = "{input}\n\n{agent_scratchpad}"
Expand Down Expand Up @@ -251,7 +255,8 @@ def streamlit_UI():
Exception: If there is any error during the fetching or streaming the response, a streamlit error is displayed.
"""
st.title(":earth_africa::airplane: AI Travel Agent")
st.write("This Langchain-powered AI Travel Agent is designed to assist you with quick travel-related queries. You can request **flight details** for a specific day or find **nearby airports** by location. For other questions, we use **Google Search** for the latest information.")
st.write("This Langchain-powered AI Travel Agent is designed to assist you with quick travel-related queries. \
You can request **flight details** for a specific day or find **nearby airports** by location. For other questions, we use **Google Search** for the latest information.")

# Sidebar with questions on the UI
st.sidebar.title(":bulb: Example Queries")
Expand Down Expand Up @@ -296,14 +301,10 @@ def streamlit_UI():
try:
with st.spinner("Generating answer..."):
with st.expander("Agent Execution", expanded=True):
# Create placeholder for streaming output
# response_placeholder = st.empty()

chunks = []
for chunk in agent_executor.stream({"input": question}):
chunks.append(chunk)
st.write(chunk)
# response = agent_executor.invoke({"input": question})

placeholder = st.empty()
content = chunks[2]['output']
Expand Down Expand Up @@ -345,8 +346,11 @@ def streamlit_UI():
PREFIX, FORMAT_INSTRUCTIONS, SUFFIX, HUMAN_MESSAGE_TEMPLATE = create_prompt_template()

# calling the create_agent function here
agent = create_agent(llm, tools, PREFIX, SUFFIX,
HUMAN_MESSAGE_TEMPLATE, FORMAT_INSTRUCTIONS)
try:
agent = create_agent(llm, tools, PREFIX, SUFFIX,
HUMAN_MESSAGE_TEMPLATE, FORMAT_INSTRUCTIONS)
except Exception as e:
st.error(f"Error loading the agent with tools : {e}")

# calling the run_agent function here
agent_executor = run_agent(agent, tools)
Expand Down
Loading