I am using the following python code to create a travel itinerary :
prompt = (f"Create a detailed travel itinerary from {start_date} to {end_date} spanning {num_days} days for {num_adults} adults "
f"This will be a round trip from {origin_city} to {destination_city}. Starting from {src_airport_departure} at {departure_flight_date_part1} and arriving on {dest_airport_departure} at {arrival_flight_date_part1}. And then the return flights goes from {src_airport_arrival} at {departure_flight_date_part2} to {dest_airport_arrival} at {arrival_flight_date_part2}"
f"This flight costs {flight_cost}"
f"For each day, please suggest activities in {destination_city}"
f"Preferred activities include: {', '.join(activities)}."
f"And the trip should definitely include: {trip_must_have}."
f" I'd like this in a list format, excluding all unnecessary data. and can you always start the itinerary with 'Here is your travel itinerary:\n\n'"
)
# Get the model's response
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant that creates travel itineraries."},
{"role": "user", "content": prompt}
]
)
return response['choices'][0]['message']['content'].strip()
I have used different prompts to try to get the response in a list format, where each list element should be a single day from the itinerary. But it keeps giving the response in string format.
Currently i have to use this code in Vue to split the data into different days, but the main issue being that it gives a new format on almost each run :
parseItinerary(text) {
// Extract main itinerary content and ignore the preamble and postamble
const mainContent = text.split("Here is your travel itinerary:")[1].split("\nPlease note")[0];
// Split content by day
const daySplit = mainContent.split("\n\n");
const days = [];
// Process each day
daySplit.forEach(dayText => {
if (!dayText) return; // Skip empty strings
// Extract heading and details
const headingEnd = dayText.indexOf(':');
const heading = dayText.split('\n')[0]
const detailsRaw = dayText.substring(headingEnd + 1).split('\n-').slice(1);
const details = detailsRaw.map(d => d.trim()).filter(Boolean);
// Push to days array
days.push({
heading: `${heading}`,
show: false,
details
});
});
return days;
}
My question is that, How can i instruct chat completion to give data in list format, something like this :
response = [ { heading : Day 1, 2023-10-11 : description : ...... }, { heading : Day 2 , ...... } ]