VOOZH about

URL: https://thenewstack.io/how-to-build-a-real-time-app-with-gpt-4o-function-calling/

⇱ How To Build a Real-Time App With GPT-4o Function Calling - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2024-05-24 08:51:56
How To Build a Real-Time App With GPT-4o Function Calling
tutorial,
AI / Large Language Models

How To Build a Real-Time App With GPT-4o Function Calling

A tutorial showing you how to bring real-time data to LLMs through function calling, using OpenAI's latest LLM GPT-4o.
May 24th, 2024 8:51am by Janakiram MSV
👁 Featued image for: How To Build a Real-Time App With GPT-4o Function Calling
Image via Unsplash+.

In our guide to function calling in LLMs, we discussed how to bring real-time data to chatbots and agents. Now we will explore this concept further by integrating the API from FlightAware.com with the new GPT-4o model, in order to track flight status in real-time.

FlightAware’s AeroAPI is a robust, RESTful API offering on-demand access to extensive flight tracking and status data. It allows developers to fetch real-time, historical, or future flight information via a simple query-based system. The API supports detailed requests based on flight identifiers, aircraft registration, or locations like airports or operators. It is designed to deliver precise, actionable aviation data in JSON format, supporting operational needs across the aviation industry, from airlines to airports.

👁 Image

Before you proceed further, sign up with FlightAware and get your API key, which is essential for invoking the REST API. The free personal tier is sufficient to complete this tutorial.

Step 1: Define the Function to Get Flight Status

Once you have the API key, create the below function in Python to retrieve the status of any flight.

Though the code is straightforward, let me explain the key steps.

The function,get_flight_status takes a flight parameter (assumed to be a flight identifier) and returns formatted flight details in JSON format. It queries the AeroAPI to fetch flight data based on the given flight identifier and formats key details such as the source, destination, departure time, arrival time, and status.

Let’s look at the components of the script:

API Credentials:
AEROAPI_BASE_URL is the base URL for the FlightAware AeroAPI.

AEROAPI_KEY is the API key used for authentication.

Session Management:
get_api_session: This nested function initializes a request. Session object sets the required header with the API key, and returns the session object. This session will handle all API requests.

Data Fetching:
fetch_flight_data: This function takes flight_id and session as arguments. It constructs the endpoint URL with appropriate date filters for fetching data for one day and sends a GET request to retrieve the flight data. The function handles the API response and extracts the relevant flight information.

Time Conversion:
utc_to_local: Converts UTC time (from the API response) to local time based on the provided timezone string. This function helps us get the arrival and departure times based on the city.

Data Processing:
The script determines keys for departure and arrival times based on the availability of estimated or actual times, with a fallback to scheduled times. It then constructs a dictionary containing formatted flight details.

👁 Image

The above screenshot shows the response we received from FlightAware API for the Emirates flight EK524 that flies from Dubai to Hyderabad. Notice that the arrival and departure times are local times based on the city.

Our goal is to integrate this function with GPT-4 Omni to give it real-time access to flight tracking information.

Step 2: Implementing Function Calling With GPT-4o

Let’s start by importing the OpenAI library and initializing it.

from openai import OpenAI
client = OpenAI()

This line creates an instance of the OpenAI class. This instance (client) will be used to interact with the OpenAI API.

We will define a list called tools, containing a dictionary that specifies the function get_flight_status. This function is intended to be used as a tool within the OpenAI API context, describing its parameters and required input.

tools = [
 {
 "type": "function",
 "function": {
 "name": "get_flight_status",
 "description": "Get status of a flight",
 "parameters": {
 "type": "object",
 "properties": {
 "flight": {
 "type": "string",
 "description": "Flight number"
 }
 },
 "required": ["flight"]
 }
 }
 }
]

The heavy lifting takes place in the below function, where the LLM inspects the prompt to determine if the function/tool needs to be called and then proceeds to generate an appropriate response.

def chatbot(prompt):
 # Step 1: send the conversation and available functions to the model
 messages = [{"role": "user", "content": prompt}]
 response = client.chat.completions.create(
 model="gpt-4o",
 messages=messages,
 tools=tools,
 tool_choice="auto"
 )
 response_message = response.choices[0].message
 tool_calls = response_message.tool_calls

 # Step 2: check if the model wanted to call a function
 if tool_calls:
 available_functions = {
 "get_flight_status": get_flight_status,
 } 
 messages.append(response_message) 
 
 # Step 3: send the function response to the model
 for tool_call in tool_calls:
 function_name = tool_call.function.name
 function_to_call = available_functions[function_name]
 function_args = json.loads(tool_call.function.arguments)
 function_response = function_to_call(flight=function_args.get("flight"))
 messages.append(
 {
 "tool_call_id": tool_call.id,
 "role": "tool",
 "name": function_name,
 "content": function_response,
 }
 ) 
 final_response = client.chat.completions.create(
 model="gpt-4o",
 messages=messages,
 ) 
 return final_response

This function, chatbot, takes a user prompt and processes it using the OpenAI API. It sends the prompt and the defined tools to the OpenAI model and processes the response.

The messages are created by embedding the prompt from the user and sending it to the OpenAI API (chat.completions.create). The API processes these messages using the specified tools, if applicable.

For example, when we send the prompt “What’s status of EK524?”, GPT-4o determines that it needs to call the function provided in the tools list and comes back with the below response:

👁 Image

Notice that the response includes the function (get_flight_status) and the parameter (EK226).

The next step checks if any tools were called (i.e., functions within tools). It executes these functions using the provided arguments, integrates their outputs into the conversation, and sends this updated information back to the OpenAI API for further processing.

 # Step 2: check if the model wanted to call a function
 if tool_calls:
 available_functions = {
 "get_flight_status": get_flight_status,
 } 
 messages.append(response_message) 
 
 # Step 3: send the info for each function call and function response to the model
 for tool_call in tool_calls:
 function_name = tool_call.function.name
 function_to_call = available_functions[function_name]
 function_args = json.loads(tool_call.function.arguments)
 function_response = function_to_call(flight=function_args.get("flight"))
 messages.append(
 {
 "tool_call_id": tool_call.id,
 "role": "tool",
 "name": function_name,
 "content": function_response,
 }
 ) 

At this point, the messages list includes the original prompt, the initial response with the function name and arguments, and the actual output from the function. The below screenshot shows the list with all the elements.

👁 Image

With the response from the tool appended to the history, we can invoke the chat completion endpoint to get the final answer from the LLM.

 final_response = client.chat.completions.create(
 model="gpt-4o",
 messages=messages,
 ) 
 return final_response

The final_response object has the answer we are looking for:

👁 Image

Sending the prompt to the function chatbot will respond with the real-time status of the specified flight.

👁 Image

Below is the complete code for this tutorial:

In this tutorial, we explored how to bring real-time data to LLMs through function calling. In the next part of this series, we will replace GPT-4o with Gemini Pro to explore the same concept but with a different model. Stay tuned.

TRENDING STORIES
Janakiram MSV (Jani) is a practicing architect, research analyst, and advisor to Silicon Valley startups. He focuses on the convergence of modern infrastructure powered by cloud-native technology and machine intelligence driven by generative AI. Before becoming an entrepreneur, he spent...
Read more from Janakiram MSV
SHARE THIS STORY
TRENDING STORIES
TNS owner Insight Partners is an investor in: OpenAI.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.