Function Calling
OpenAI function calling with Elasticsearch
Function calling in OpenAI refers to the capability of AI models to interact with external functions or APIs, allowing them to perform tasks beyond text generation. This feature enables the model to execute code, retrieve information from databases, interact with external services, and more, by calling predefined functions.
In this notebook we’re going to create two function:
fetch_from_elasticsearch() - Fetch data from Elasticsearch using natural language query.
weather_report() - Fetch a weather report for a particular location.
We'll integrate function calling to dynamically determine which function to call based on the user's query and generate the necessary arguments accordingly.
Setup
Elastic
Create an Elastic Cloud deployment to get all Elastic credentials.
ES_API_KEY: Create an API key.
ES_ENDPOINT: Copy endpoint of Elasticsearch.
Open AI
OPENAI_API_KEY: Setup an Open AI account and create a secret key.
GPT_MODEL: We’re going to use the gpt-4o model but you can check here which model is being supported for function calling.
Open-Meteo API
We will use the Open-Meteo API. Open-Meteo is an open-source weather API and offers free access for non-commercial use. No API key required.
OPEN_METEO_ENDPOINT: https://api.open-meteo.com/v1/forecast
Sample Data
After creating Elastic cloud deployment, Let’s add sample flight data on kibana. Sample data will be stored into the kibana_sample_data_flights index.
Install depndencies
pip install openai
Import packages
Add Credentials
Functions to get data from Elasticsearch
Get Index mapping
Get reference document
Generate Elasticsearch Query DSL based on user query
Execute Query on Elasticsearch
Function to get weather report
Function calling
Ask Query
Ask: details of last 10 delayed flights for Bangalore in tabular format and describe the current climate there.
### Last 10 Delayed Flights for Bangalore | Flight Number | Origin City | Flight Delay Type | Flight Delay Min | Airline | Avg Ticket Price | Origin Weather | Destination Weather | Status | |---------------|----------------------|----------------------------|------------------|------------------|------------------|----------------------|---------------------------|------------------------| | B2JWDRX | Catania | Security Delay | 60 | Kibana Airlines | 999.02 | Hail | Cloudy | On Time | | C9C7VBY | Frankfurt am Main | Security Delay | 285 | Logstash Airways | 807.13 | Rain | Rain | On Time | | 09P9K2Z | Paris | Late Aircraft Delay | 195 | Kibana Airlines | 942.35 | Clear | Sunny | On Time | | 0FXK4HG | Osaka | Carrier Delay | 195 | Logstash Airways | 957.95 | Sunny | Thunder & Lightning | On Time | | 5EYOHJR | Genova | NAS Delay | 360 | ES-Air | 963.11 | Rain | Rain | On Time | | X5HA5YJ | Bangor | Weather Delay | 330 | Kibana Airlines | 523.27 | Thunder & Lightning | Clear | On Time | | 4BZUCXP | Bogota | Late Aircraft Delay | 30 | ES-Air | 972.79 | Sunny | Thunder & Lightning | On Time | | O8I6UU8 | Catania | Late Aircraft Delay | 135 | ES-Air | 792.41 | Damaging Wind | Sunny | Cancelled | | 56HYVZQ | Denver | NAS Delay | 60 | Logstash Airways | 923.13 | Thunder & Lightning | Thunder & Lightning | On Time | | X4025SP | Paris | Late Aircraft Delay | 30 | Kibana Airlines | 576.87 | Thunder & Lightning | Clear | Cancelled | ### Current Climate in Bangalore - **Temperature:** 23.5°C - **Precipitation:** 0.0 mm - **Cloud Cover:** 49% - **Visibility:** 24,140 meters - **Wind Speed:** 8.6 km/h The current weather in Bangalore is partly cloudy with a temperature of 23.5°C. The visibility is excellent, and there is no precipitation.