Build a travel agent with custom code
This guide walks you through creating a custom travel agent capable of planning trips end-to-end. You’ll learn how to:
- Use aiXplain to build an intelligent agent that integrates multiple tools (like weather and location lookups) for a seamless user experience.
- Fetch real-time flight details using a custom Python code that leverages SerpAPI for flight pricing, availability, and other key parameters.
By following the steps below, you’ll end up with a travel agent that can handle flights, local points of interest, and weather updates—ready to deploy and scale for real-world use cases.
Build Your Agent
To create an agent, define:
- A unique name and description for its purpose.
- The tools (optional) it will use, such as specific AI models or pipelines.
- The language model (optional) to enhance its performance. By default, aiXplain agents use OpenAI's GPT-4o.
Browse our marketplace for all kinds of assets. Add assets as tools to your agents using their ID.
from aixplain.modules.agent import ModelTool
from aixplain.factories import AgentFactory
from aixplain.factories import TeamAgentFactory
Custom Code
To provide flight details, we’ll use custom code that fetches flight information using SerpAPI. This tool will allow our agent to retrieve real-time flight availability, cost, and duration for specific routes.
def main(departure_id: str,
arrival_id: str,
outbound_date: str,
return_date: str,
currency: str = "USD"):
import requests
import json
SERPAPI_KEY = "<SERPAPI_KEY>"
params = {
"engine": "google_flights",
"api_key": SERPAPI_KEY,
"hl": "en",
"gl": "us",
"departure_id": departure_id,
"arrival_id": arrival_id,
"outbound_date": outbound_date,
"return_date": return_date,
"currency": currency
}
r = requests.get("https://serpapi.com/search", params=params)
data = r.json()
return data
To integrate our custom flight search functionality into the aiXplain framework, we’ll use the create_custom_python_code_tool
to use the code as a Utility Tool. This will allow our agent to call this tool dynamically when planning travel itineraries.
Travel Agent
Combine multiple models as tools to create a Travel Agent which provides flight details, local points of interest and weather information for the location.
from aixplain.factories import AgentFactory
from aixplain.modules.agent import ModelTool
from aixplain.enums import Function, Supplier
TRAVEL_AGENT_ROLE = """
You are a highly capable Travel Agent.
Your goal is to help users plan trips by providing:
- Flight details - including duration, time and airline
- Local points of interest - breakdown by day to day activities
- Weather info - provide weather in celsius.
- And any other travel-related assistance
ONLY PROVIDE OUTPUTS AS ITENIARIES
If you cannot fulfill a request or need more info, ask clarifying questions.
"""
travel_agent = AgentFactory.create(
name="Travel Agent",
description=TRAVEL_AGENT_ROLE,
tools=[
AgentFactory.create_model_tool(model="66f423426eb563fa213a3531"), # Scraper
AgentFactory.create_model_tool(model="66f6ac496eb563510e3503d1"), # Google Places API
AgentFactory.create_custom_python_code_tool(
code=main,
description="Provide flight details - time, cost and duration for the location and dates specified"
),
AgentFactory.create_model_tool(model="66f83c216eb563266175e201"), # OpenWeather
]
)
Run the Agent
Run and debug the agent till you are happy with the response!
from aixplain.modules.agent import OutputFormat
import json
response = travel_agent.run(
"Help me plan my 3 day trip to Baltimore next week with my sister from Dallas on the 2nd of February, 2025",
output_format=OutputFormat.MARKDOWN
)
print(json.dumps(response["data"], indent=4))
print(response["data"]["output"])
Deploy your Agent
Once you are happy with your agent, deploy your agent to use its endpoints!
travel_agent.deploy()