forked from rungalileo/sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
332 lines (279 loc) · 11.5 KB
/
Copy pathapp.py
File metadata and controls
332 lines (279 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import os
import streamlit as st
from galileo import (
log,
galileo_context,
openai,
)
from pydantic import BaseModel
import json
from typing import Callable
from dotenv import load_dotenv
load_dotenv()
client = openai.OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
class DestinationOverviewRequest(BaseModel):
destination: str
class ItineraryRequest(BaseModel):
destination: str
days: int
class WeatherRequest(BaseModel):
destination: str
class BudgetRequest(BaseModel):
destination: str
days: int
tools = [
{
"type": "function",
"function": {
"name": "get_weather_forecast",
"description": "Get the weather forecast for a given location.",
"parameters": {
"type": "object",
"properties": {
"destination": {
"type": "string",
"description": "City and country e.g. Bogotá, Colombia",
},
},
"required": [
"destination",
],
"additionalProperties": False,
},
"strict": True,
},
},
{
"type": "function",
"function": {
"name": "estimate_travel_budget",
"description": "Estimate the travel budget for a given location.",
"parameters": {
"type": "object",
"properties": {
"destination": {
"type": "string",
"description": "City and country e.g. Bogotá, Colombia",
},
"days": {
"type": "integer",
"description": "Number of days for the itinerary",
},
},
"required": ["destination", "days"],
"additionalProperties": False,
},
"strict": True,
},
},
{
"type": "function",
"function": {
"name": "generate_destination_overview",
"description": "Generate a destination overview.",
"parameters": {
"type": "object",
"properties": {
"destination": {
"type": "string",
"description": "City and country e.g. Bogotá, Colombia",
},
},
"required": [
"destination",
],
"additionalProperties": False,
},
"strict": True,
},
},
{
"type": "function",
"function": {
"name": "generate_itinerary",
"description": "Generate a travel itinerary for a destination.",
"parameters": {
"type": "object",
"properties": {
"destination": {
"type": "string",
"description": "City and country e.g. Bogotá, Colombia",
},
"days": {
"type": "integer",
"description": "Number of days for the itinerary",
},
},
"required": ["destination", "days"],
"additionalProperties": False,
},
"strict": True,
},
},
]
# =============================================================================
# Local function to simulate an API call for weather data.
# =============================================================================
@log(span_type="tool")
def get_weather_forecast(destination: str) -> str:
# Simulate a local function call that returns dummy weather data.
return f"Weather forecast for {destination}: Mostly sunny with a slight chance of rain."
# =============================================================================
# LLM call: Generate a destination overview.
# =============================================================================
def generate_destination_overview(destination: str) -> str:
prompt = f"Provide a brief overview of {destination}, including its top attractions, " "cultural highlights, and essential travel tips."
# Call the OpenAI API (assuming proper API key configuration)
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
)
# Extract and return the text from the response.
return response.choices[0].message.content.strip()
# =============================================================================
# LLM call: Generate a day-by-day travel itinerary.
# =============================================================================
def generate_itinerary(destination: str, days: int) -> str:
prompt = f"""
Plan a {days}-day travel itinerary for a trip to {destination}.
Include daily sightseeing activities, dining suggestions, and local experiences.
Important:
- Every plan must contain a destination overview and an itinerary.
- Only include a travel budget and weather info if the user requests it.
"""
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
)
return response.choices[0].message.content.strip()
# =============================================================================
# LLM call: Estimate a travel budget.
# =============================================================================
def estimate_travel_budget(destination: str, days: int, itinerary: str | None = None) -> str:
itinerary_prompt = f"\n\nHere is the itinerary:\n\n {itinerary}" if itinerary else ""
prompt = (
f"Estimate a travel budget for a {days}-day trip to {destination} that covers accommodation, "
"food, transportation, and activities. Provide a rough breakdown of the costs (in USD)."
f"{itinerary_prompt}"
)
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
)
return response.choices[0].message.content.strip()
# =============================================================================
# LLM call: Decide which functions to call and assemble the itinerary.
# =============================================================================
@log
def assemble_travel_plan(query: str, info_callback: Callable[[str], None]) -> str:
#
# Develop a plan
#
function_calling_prompt = f"""
You are an expert travel planner. I've included a request that you need to process:
\"{query}\"
Important:
- Every plan MUST contain a destination overview and an itinerary. Make sure to always call the `generate_destination_overview` and `generate_itinerary` functions.
- Only include a travel budget (using the `estimate_travel_budget` function) and weather info (using the `get_weather_forecast` function) if the user requests it.
"""
response = client.chat.completions.create(
model="gpt-4-turbo",
messages=[{"role": "user", "content": function_calling_prompt}],
tools=tools,
)
# Extract function call responses
message_response = response.choices[0].message
if message_response.function_call:
function_calls = [message_response.function_call] # Single function case
else:
function_calls = message_response.tool_calls or [] # Multiple function calls
destination_overview = ""
itinerary = ""
budget = ""
weather = ""
#
# Process multiple function calls
#
for function_call in function_calls:
function_name = function_call.function.name
function_args = json.loads(function_call.function.arguments)
if function_name == "generate_destination_overview":
destination_overview_request = DestinationOverviewRequest(**function_args)
info_callback(f"Generating a destination overview for {destination_overview_request.destination}...")
destination_overview = destination_overview + "\n" + generate_destination_overview(destination_overview_request.destination)
elif function_name == "generate_itinerary":
itinerary_request = ItineraryRequest(**function_args)
info_callback(f"Generating an itinerary for {itinerary_request.destination}...")
itinerary = itinerary + "\n" + generate_itinerary(itinerary_request.destination, itinerary_request.days)
elif function_name == "estimate_travel_budget":
budget_request = BudgetRequest(**function_args)
info_callback(f"Generating a travel budget for {budget_request.destination}...")
budget = (
budget
+ "\n"
+ estimate_travel_budget(
destination=budget_request.destination,
days=budget_request.days,
itinerary=itinerary,
)
)
elif function_name == "get_weather_forecast":
weather_request = WeatherRequest(**function_args)
info_callback(f"Generating a weather forecast for {weather_request.destination}...")
weather = weather + "\n" + get_weather_forecast(weather_request.destination)
#
# Assemble the results
#
info_callback("Assembling the final plan...")
travel_budget = f"Travel budget: {budget}" if budget else ""
weather_forecast = f"Weather forecast: {weather}" if weather else ""
assembly_prompt = (
f"""
You are an expert travel planner.
My original request was: \"{query}\"
You have generated the following outputs:
Destination overview: {destination_overview}
Itinerary: {itinerary}
"""
+ travel_budget
+ weather_forecast
+ f"\n\nPlease package the information above into a plan that I can use for my next trip."
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": assembly_prompt}],
)
return response.choices[0].message.content.strip()
# =============================================================================
# Main Streamlit App
# =============================================================================
def main():
st.title("Travel Itinerary Planner")
st.write("Plan your next adventure with our AI-powered travel planner.")
with st.container():
# User inputs for the travel query.
query = st.text_input(
"Where would you like to go?",
"Rome for 5 days later this month, please include a trip budget and weather info",
)
# Create a button for planning the trip.
plan_trip_clicked = st.button("Plan My Trip")
info_placeholder = st.empty()
# Create an empty container for results so that previous outputs are cleared on re-run.
result_container = st.empty()
def info_callback(message):
info_placeholder.info(message)
if plan_trip_clicked:
if not query.strip():
info_placeholder.warning("Please enter a travel query.")
else:
result_container.empty()
info_placeholder.info("Generating your travel plan. Please wait...")
with galileo_context():
plan = assemble_travel_plan(query, info_callback)
# Clear the info message once the generation is complete.
info_placeholder.empty()
with result_container.container():
st.write(plan)
if __name__ == "__main__":
main()