Skip to content

Commit 7808847

Browse files
Added few shots prompting, changed weather api, changed model to (#254)
* Added few shots prompting, changed weather api, changed model to * Added few shots prompting, changed weather api, changed model to * Added few shots prompting, changed weather api, changed model to
1 parent 399e6cf commit 7808847

File tree

1 file changed

+112
-34
lines changed

1 file changed

+112
-34
lines changed

notebooks/integrations/openai/function-calling.ipynb

Lines changed: 112 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@
4848
"`OPENAI_API_KEY`: Setup an [Open AI account and create a secret key](https://platform.openai.com/docs/quickstart). \n",
4949
"`GPT_MODEL`: We’re going to use the `gpt-4o` model but you can check [here](https://platform.openai.com/docs/guides/function-calling) which model is being supported for function calling.\n",
5050
"\n",
51-
"### Weather Union\n",
51+
"### Open-Meteo API\n",
5252
"\n",
53-
"We will use the [Weather Union API](https://weatherunion.com). Which gives the weather reports of different cities in India. \n",
54-
"`WU_API_KEY` & `WU_ENDPOINT` = [Create an account](https://www.weatherunion.com/login) and generate API Key.\n",
53+
"We will use the [Open-Meteo API](https://open-meteo.com/). Open-Meteo is an open-source weather API and offers free access for non-commercial use. No API key required. \n",
54+
"\n",
55+
"`OPEN_METEO_ENDPOINT`: `https://api.open-meteo.com/v1/forecast`\n",
5556
"\n",
5657
"### Sample Data\n",
5758
"After creating Elastic cloud deployment, Let’s [add sample flight data](https://www.elastic.co/guide/en/kibana/8.13/get-started.html#gs-get-data-into-kibana) on kibana. Sample data will be stored into the `kibana_sample_data_flights` index.\n",
@@ -103,14 +104,13 @@
103104
"client = OpenAI(\n",
104105
" api_key=OPENAI_API_KEY,\n",
105106
")\n",
106-
"GPT_MODEL = input(\"GPT Model:\")\n",
107+
"GPT_MODEL = \"gpt-4o\"\n",
107108
"\n",
108109
"ES_API_KEY = getpass(\"Elastic API Key:\")\n",
109110
"ES_ENDPOINT = input(\"Elasticsearch Endpoint:\")\n",
110111
"ES_INDEX = \"kibana_sample_data_flights\"\n",
111112
"\n",
112-
"WU_API_KEY = getpass(\"Weather Union API Key:\")\n",
113-
"WU_API_ENDPOINT = input(\"Weather Union API Endpoint:\")"
113+
"OPEN_METEO_ENDPOINT = \"https://api.open-meteo.com/v1/forecast\""
114114
]
115115
},
116116
{
@@ -203,6 +203,70 @@
203203
" index_mapping = get_index_mapping()\n",
204204
" ref_document = get_ref_document()\n",
205205
"\n",
206+
" few_shots_prompt = \"\"\"\n",
207+
" 1. User Query - Average delay time of flights going to India\n",
208+
" Elasticsearch Query DSL: \n",
209+
" {\n",
210+
" \"size\": 0,\n",
211+
" \"query\": {\n",
212+
" \"bool\": {\n",
213+
" \"filter\": {\n",
214+
" \"term\": {\n",
215+
" \"DestCountry\": \"IN\"\n",
216+
" }\n",
217+
" }\n",
218+
" }\n",
219+
" },\n",
220+
" \"aggs\": {\n",
221+
" \"average_delay\": {\n",
222+
" \"avg\": {\n",
223+
" \"field\": \"FlightDelayMin\"\n",
224+
" }\n",
225+
" }\n",
226+
" }\n",
227+
" }\n",
228+
"\n",
229+
" 2. User Query - airlines with the highest delays\n",
230+
" Elasticsearch Query DSL: \n",
231+
" {\n",
232+
" \"size\": 0,\n",
233+
" \"aggs\": {\n",
234+
" \"airlines_with_highest_delays\": {\n",
235+
" \"terms\": {\n",
236+
" \"field\": \"Carrier\",\n",
237+
" \"order\": {\n",
238+
" \"average_delay\": \"desc\"\n",
239+
" }\n",
240+
" },\n",
241+
" \"aggs\": {\n",
242+
" \"average_delay\": {\n",
243+
" \"avg\": {\n",
244+
" \"field\": \"FlightDelayMin\"\n",
245+
" }\n",
246+
" }\n",
247+
" }\n",
248+
" }\n",
249+
" }\n",
250+
" }\n",
251+
"\n",
252+
" 3. User Query - Which was the last flight that got delayed for Bangalore\n",
253+
" Elasticsearch Query DSL: \n",
254+
" {\n",
255+
" \"query\": {\n",
256+
" \"bool\": {\n",
257+
" \"must\": [\n",
258+
" { \"match\": { \"DestCityName\": \"Bangalore\" } },\n",
259+
" { \"term\": { \"FlightDelay\": true } }\n",
260+
" ]\n",
261+
" }\n",
262+
" },\n",
263+
" \"sort\": [\n",
264+
" { \"timestamp\": { \"order\": \"desc\" } }\n",
265+
" ],\n",
266+
" \"size\": 1\n",
267+
" }\n",
268+
" \"\"\"\n",
269+
"\n",
206270
" prompt = f\"\"\"\n",
207271
" Use below index mapping and reference document to build Elasticsearch query:\n",
208272
"\n",
@@ -212,11 +276,18 @@
212276
" Reference elasticsearch document:\n",
213277
" {ref_document}\n",
214278
"\n",
215-
" Return single line Elasticsearch Query DSL according to index mapping for the below search query. Just return Query DSL without REST specification (e.g. GET, POST etc.) and json markdown format (e.g. ```json):\n",
279+
" Return single line Elasticsearch Query DSL according to index mapping for the below search query related to flights.:\n",
216280
"\n",
217281
" {nl_query}\n",
218282
"\n",
219283
" If any field has a `keyword` type, Just use field name instead of field.keyword.\n",
284+
"\n",
285+
" Just return Query DSL without REST specification (e.g. GET, POST etc.) and json markdown format (e.g. ```json)\n",
286+
"\n",
287+
" few example of Query DSL\n",
288+
"\n",
289+
" {few_shots_prompt}\n",
290+
" \n",
220291
" \"\"\"\n",
221292
"\n",
222293
" resp = client.chat.completions.create(\n",
@@ -287,15 +358,13 @@
287358
"source": [
288359
"def weather_report(latitude, longitude):\n",
289360
"\n",
290-
" url = f\"\"\"{WU_API_ENDPOINT}?latitude={latitude}&longitude={longitude}\"\"\"\n",
291-
"\n",
292-
" headers = {\"x-zomato-api-key\": f\"\"\"{WU_API_KEY}\"\"\"}\n",
361+
" url = f\"\"\"{OPEN_METEO_ENDPOINT}?latitude={latitude}&longitude={longitude}&current=temperature_2m,precipitation,cloud_cover,visibility,wind_speed_10m\"\"\"\n",
293362
"\n",
294-
" resp = requests.request(\"GET\", url, headers=headers)\n",
363+
" resp = requests.request(\"GET\", url)\n",
295364
" resp = json.loads(resp.text)\n",
296365
" json_resp = json.dumps(resp, indent=4)\n",
297366
"\n",
298-
" # print(f\"\"\"\\n\\nWeatherUnion response: ==== \\n\\n {json_resp}\"\"\")\n",
367+
" # print(f\"\"\"\\n\\nOpen-Meteo response: ==== \\n\\n {json_resp}\"\"\")\n",
299368
" return json_resp"
300369
]
301370
},
@@ -361,7 +430,7 @@
361430
" messages.append(\n",
362431
" {\n",
363432
" \"role\": \"system\",\n",
364-
" \"content\": \"If no data received from any function. Just say there is issue fetching details from function(function_name)\",\n",
433+
" \"content\": \"If no data received from any function. Just say there is issue fetching details from function(function_name).\",\n",
365434
" }\n",
366435
" )\n",
367436
"\n",
@@ -419,12 +488,12 @@
419488
" }\n",
420489
" )\n",
421490
"\n",
422-
" second_response = client.chat.completions.create(\n",
423-
" model=GPT_MODEL,\n",
424-
" messages=messages,\n",
425-
" )\n",
491+
" second_response = client.chat.completions.create(\n",
492+
" model=GPT_MODEL,\n",
493+
" messages=messages,\n",
494+
" )\n",
426495
"\n",
427-
" return second_response.choices[0].message.content"
496+
" return second_response.choices[0].message.content"
428497
]
429498
},
430499
{
@@ -437,41 +506,50 @@
437506
},
438507
{
439508
"cell_type": "code",
440-
"execution_count": 27,
509+
"execution_count": 137,
441510
"id": "bbf1a2dc-49f8-4bfd-8317-7f28c5ef4715",
442511
"metadata": {},
443512
"outputs": [
444513
{
445514
"name": "stdin",
446515
"output_type": "stream",
447516
"text": [
448-
"Ask: last 10 flight delay to bangalore, show in table\n"
517+
"Ask: details of last 10 delayed flights for Bangalore in tabular format and describe the current climate there.\n"
449518
]
450519
},
451520
{
452521
"name": "stdout",
453522
"output_type": "stream",
454523
"text": [
455-
"Here are the last 10 flight delays to Bangalore:\n",
524+
"### Last 10 Delayed Flights for Bangalore\n",
525+
"\n",
526+
"| Flight Number | Origin City | Flight Delay Type | Flight Delay Min | Airline | Avg Ticket Price | Origin Weather | Destination Weather | Status |\n",
527+
"|---------------|----------------------|----------------------------|------------------|------------------|------------------|----------------------|---------------------------|------------------------|\n",
528+
"| B2JWDRX | Catania | Security Delay | 60 | Kibana Airlines | 999.02 | Hail | Cloudy | On Time |\n",
529+
"| C9C7VBY | Frankfurt am Main | Security Delay | 285 | Logstash Airways | 807.13 | Rain | Rain | On Time |\n",
530+
"| 09P9K2Z | Paris | Late Aircraft Delay | 195 | Kibana Airlines | 942.35 | Clear | Sunny | On Time |\n",
531+
"| 0FXK4HG | Osaka | Carrier Delay | 195 | Logstash Airways | 957.95 | Sunny | Thunder & Lightning | On Time |\n",
532+
"| 5EYOHJR | Genova | NAS Delay | 360 | ES-Air | 963.11 | Rain | Rain | On Time |\n",
533+
"| X5HA5YJ | Bangor | Weather Delay | 330 | Kibana Airlines | 523.27 | Thunder & Lightning | Clear | On Time |\n",
534+
"| 4BZUCXP | Bogota | Late Aircraft Delay | 30 | ES-Air | 972.79 | Sunny | Thunder & Lightning | On Time |\n",
535+
"| O8I6UU8 | Catania | Late Aircraft Delay | 135 | ES-Air | 792.41 | Damaging Wind | Sunny | Cancelled |\n",
536+
"| 56HYVZQ | Denver | NAS Delay | 60 | Logstash Airways | 923.13 | Thunder & Lightning | Thunder & Lightning | On Time |\n",
537+
"| X4025SP | Paris | Late Aircraft Delay | 30 | Kibana Airlines | 576.87 | Thunder & Lightning | Clear | Cancelled |\n",
538+
"\n",
539+
"### Current Climate in Bangalore\n",
456540
"\n",
457-
"| Flight Number | Origin City | Carrier | Delay (Minutes) | Delay Type | Date & Time |\n",
458-
"|---------------|-------------|-------------------|------------------|------------------------|----------------------|\n",
459-
"| B2JWDRX | Catania | Kibana Airlines | 60 | Security Delay | 2024-03-03 13:32:15 |\n",
460-
"| C9C7VBY | Frankfurt | Logstash Airways | 285 | Security Delay | 2024-03-01 05:20:01 |\n",
461-
"| 09P9K2Z | Paris | Kibana Airlines | 195 | Late Aircraft Delay | 2024-02-29 06:02:38 |\n",
462-
"| 0FXK4HG | Osaka | Logstash Airways | 195 | Carrier Delay | 2024-02-23 03:34:21 |\n",
463-
"| 5EYOHJR | Genova | ES-Air | 360 | NAS Delay | 2024-02-21 15:51:26 |\n",
464-
"| X5HA5YJ | Bangor | Kibana Airlines | 330 | Weather Delay | 2024-02-19 13:50:58 |\n",
465-
"| 4BZUCXP | Bogota | ES-Air | 30 | Late Aircraft Delay | 2024-02-16 05:59:37 |\n",
466-
"| O8I6UU8 | Catania | ES-Air | 135 | Late Aircraft Delay | 2024-02-09 03:12:49 |\n",
467-
"| 56HYVZQ | Denver | Logstash Airways | 60 | NAS Delay | 2024-02-08 15:52:44 |\n",
468-
"| X4025SP | Paris | Kibana Airlines | 30 | Late Aircraft Delay | 2024-02-08 10:57:29 |\n",
541+
"- **Temperature:** 23.5°C\n",
542+
"- **Precipitation:** 0.0 mm\n",
543+
"- **Cloud Cover:** 49%\n",
544+
"- **Visibility:** 24,140 meters\n",
545+
"- **Wind Speed:** 8.6 km/h\n",
469546
"\n",
470-
"If you need more details or further assistance, please let me know.\n"
547+
"The current weather in Bangalore is partly cloudy with a temperature of 23.5°C. The visibility is excellent, and there is no precipitation.\n"
471548
]
472549
}
473550
],
474551
"source": [
552+
"# Ask: details of last 10 delayed flights for Bangalore in tabular format and describe the current climate there.\n",
475553
"i = input(\"Ask:\")\n",
476554
"answer = run_conversation(i)\n",
477555
"print(answer)"

0 commit comments

Comments
 (0)