Skip to content
Planning a Trip with Open AI API
Planning a Trip with Open AI API
This creation will become a virtual Travels expert, delivering valuable insights into the city's iconic landmarks and hidden treasures. The AI will respond intelligently to a set of common questions, providing a more engaging and immersive travel planning experience for the clientele of Peterman Reality Tours.
The ultimate aspiration is a user-friendly, AI-driven travel guide that significantly enhances the exploration of your destination. Users will be able to pre-define their questions and receive well-informed answers from the AI, providing a seamless and intuitive travel planning process.
import os
from openai import OpenAI
# Define the model to use
model = "gpt-3.5-turbo"
# Define the client
client = OpenAI(api_key=os.environ["OPEN_AI"])def generate_trip_planning(budget, destination, date, nb_days, interests_string, nb_pers) :
# Define the conversation
conversation = [
{
"role": "system",
"content": "You are a travel guide designed to provide information about landmarks that tourists should explore in " + destination + ". You speak in a concise manner."
},
{
"role": "user",
"content": "give me a list of the most famous landmarks in " + destination + " ?"
},
]
# Make the API call to get a list of the most famous landmarks
response = client.chat.completions.create(
model=model,
messages=conversation,
temperature=0.0,
max_tokens=100
)
resp = response.choices[0].message.content
# Define a list of questions
questions = [
"""knowing that I have a budget of " + budget + " and that I will be in " + destination + " in " + date + " and that I'm interested in " + interests_string+ " , plan a trip of " + nb_days + " days to " + resp + " for "+ nb_pers+" person return the addresses and the (longitude, latitude) of the best destinations with the transport to take , the tickets and it's prices, the weather ? The response should be in format json. and day by day. for example : {
"trip": {
"start_date": "12/12/2024",
"duration": "10 days",
"budget_per_person": "3000€",
"interests": ["culture", "nature", "food"],
"travelers": 3
},
"itinerary": [
{
"day": 1,
"destination": "Tokyo Tower",
"address": "4 Chome-2-8 Shibakoen, Minato City, Tokyo 105-0011, Japan",
"coordinates": {
"latitude": 35.6586,
"longitude": 139.7454
},
"transport": "Subway - Oedo Line to Akabanebashi Station",
"ticket_price": "¥900",
"ticket_link": "link url",
"weather": "Average temperature: 10°C, partly cloudy"
},
{
"day": 2,
"destination": "Senso-ji Temple in Asakusa",
"address": "2 Chome-3-1 Asakusa, Taito City, Tokyo 111-0032, Japan",
"coordinates": {
"latitude": 35.7146,
"longitude": 139.7966
},
"transport": "Subway - Ginza Line to Asakusa Station",
"ticket_price": "Free admission",
"ticket_link": "link url",
"weather": "Average temperature: 12°C, sunny"
}} """
]
titles = ['**** Plan of a trip to ' + destination + "**** * Budget : " + budget + ' * Depart : ' + date + ' for ' + nb_days + ' days :']
# Initialize variable to store full response
full_response = ""
# Loop through each question to generate responses
for question, title in zip(questions, titles):
# Format the user input into dictionary form
input_dict = {"role": "user", "content": question}
# Add the user input dictionary to the conversation
conversation.append(input_dict)
# Make the next API call
response = client.chat.completions.create(
model=model,
messages=conversation,
temperature=0.0,
max_tokens=1000
)
# Get response from the model
resp = response.choices[0].message.content
# Append response to full_response
full_response += title + "\n" + resp + "\n\n"
# Convert the response into the dictionary
resp_dict = {"role": "assistant", "content": resp}
# Append the response to the conversation
conversation.append(resp_dict)
# Return the full response
return full_response
destination = 'Tokyo'
budget = '3000€/pers'
nb_days = '10'
date = '12/12/2024'
nb_personnes = '3'
interests=['culture','nature','food']
# Create a string with elements separated by ', '
interests_string = ', '.join(interests)
# Generate trip planning text
trip_planning = generate_trip_planning(budget, destination, date, nb_days, interests_string , nb_personnes)
# Print the generated trip planning text
print(trip_planning)