Unlocking AI Potential with OpenAI Function Calls: A Deep Dive into Simplifying Data Retrieval

Rafael Lotufo
6 min readJul 4, 2023

--

Photo by Pavan Trikutam on Unsplash

Harnessing the power of AI in the vast expanse of today’s data-driven landscape is no small feat. The complexity often lies in the intricate dance of information parsing, a process demanding a keen understanding of both the AI model’s capabilities and the nuances of data retrieval from external sources. Enter OpenAI Function Calls, a pioneering approach that redefines this dynamic, streamlining the conversation between user, AI, and the information universe beyond.

Function Calls stand as a beacon of innovation in this realm, offering a clear-cut method to solicit and utilize data that the AI model itself may not inherently possess. This technology essentially detects when a question asked to the model calls for a search beyond its base knowledge, indicating an opportunity for an external function call.

Through this introduction, we’ll explore the mechanics of OpenAI Function Calls, their advantages over traditional methods, and their practical application in real-world scenarios.

Understanding OpenAI Function Calls: A Use-case with Wikipedia

To truly appreciate the power of Function Calls, let’s dissect them using a familiar use-case: querying information from Wikipedia.

Imagine this scenario: A user poses a question to the AI model which is outside its knowledge cut-off. Traditionally, the model would respond to the best of its ability based on pre-existing knowledge, potentially leaving gaps in accuracy or depth of the answer. This is where Function Calls introduce a paradigm shift.

The AI model, via Function Calls, is capable of recognizing when a question is better suited for external data fetching. Say the user’s question is about a recent scientific discovery or a real-time event that occurred post-AI training; the model acknowledges that it may not have the most up-to-date or comprehensive answer. Instead of providing potentially outdated or incomplete information, it detects the necessity for an external function call, in this case, to Wikipedia.

However, it’s crucial to note that the model itself doesn’t make the function call. Rather, it identifies the best-suited function from an available pool, like a Wikipedia query function, and generates a suggestion for this function call.

The AI’s suggested function call is passed in a message with a function_call role. This message includes the specific function to be used and the parameters required for execution. It’s like a well-crafted instruction set, preparing the groundwork for seamless external data fetching.

This approach allows the model to greatly expand its knowledge range in real-time, effectively transforming it from a static information source to an interactive and dynamic one.

OpenAI Function Calls versus Traditional Method: A Comparative Analysis

Let’s further illuminate the advantage of function calls with a concrete example, by comparing traditional approach and the function calls approach.

The Traditional Approach

Suppose we want to use the model to get information about the the Silicon Valley Bank crash. One of the approaches to teach the model that it can suggest a query to an external data source could be something like this:

{
"role": "system",
"content": "You are a helpful assistant that can provide information by
querying Wikipedia. When you're asked for information that
you do not know about, imagine
that you use a function called 'query_wikipedia' to fetch the
information. Then, return it in a simple text format that's
easy to parse."
},
{
"role": "user",
"content": "What was the cause of the Silicon Valley Bank crash in 2023?"
}

Alternatively, we could provide a “few-shot” prompt approach, with examples showing the model that it can suggest a query to wikipedia. Both of these approaches require us to teach the model explicitly how to use and mimic function calls, and then for us to parse the output, which can be cumbersome and less reliable.

The Function Calls Approach

Now, let’s consider the same scenario, but with the use of OpenAI Function Calls:

messages: {
"role": "user",
"content": "What was the cause of the Silicon Valley Bank crash in 2023?"
}
functions: [{
"name": "query_wikipedia",
"description": "query wikipedia to fetch information that helps answer
questions about things.",
"parameters": {
"type": "object",
"properties": {
"entity": {
"type": "string",
"description": "person, place, or event the user is asking about"
}
},
"required": ["question", "entity"]
}]

You see that, along with the user’s question, we provide the model with a list of available function calls. Just by the names and descriptions of the functions and parameters, the model can detect the need to fetch information from Wikipedia and generate a function call suggestion:

{
"role": "assistant",
"content": {
"function": {
"name": "query_wikipedia",
"args": {
"entity": "Silicon Valley Bank crash 2023"
}
}
}
}

This message indicates that the assistant suggests using the query_wikipedia function with "Silicon Valley Bank crash 2023" as the query parameter.

Once the function is executed and the information is fetched, the results are passed back to the model in another message:

{
"role": "function",
"name": "query_wikipedia",
"content": "...<content fetched from wikipedia page>..."
}

With the above message, the model will then reason about the content that was fetched from Wikipedia and will then finish with answering the original question.

With Function Calls, the AI model doesn’t have to simulate a function — it actually suggests one. This process is less error-prone, more efficient, and allows for much more flexible and dynamic interactions.

Sample Demo Application

You can find a simple application that implements a simple example use of function calls at here. It is a sample application that forwards the user’s question to the model, along with a definition of the query_wikipedia function above. It then takes the entity parameter returned by the model, and queries Wikipedia, which returns a list of pages. The application then fetches the content for all of the pages, concatenates the text for all pages, and we send all of this content to the model, using the function role. The model then reasons about all of the content and answers the question.

For the query above about Silicon Valley Bank, the answer we get is something like:

> python wikipedia-ai.py "What was the cause of the Silicon Valley Bank crash in 2023?"

In March 2023, Silicon Valley Bank experienced a collapse and seizure by
the California Department of Financial Protection and Innovation (DFPI).
The collapse was triggered by a bank run on its deposits, which was caused
by central bank-endorsed interest rate hikes and concerns about the bank's
liquidity. The DFPI appointed the Federal Deposit Insurance Corporation (FDIC)
as the receiver of the bank. The FDIC established a bridge bank successor
called Silicon Valley Bridge Bank, which assumed ongoing business operations.
First Citizens Bank & Trust Company later acquired the commercial banking
business of Silicon Valley Bridge Bank from the FDIC.

Similarly, for the question “What is the Apple glasses that was announced in 2023 and what does it do?”, we get as answer:

> python wikipedia-ai.py "What is the Apple glasses that was announced in
2023 and what does it do?"

Apple Vision Pro, also known as Apple glasses, is an upcoming mixed
reality headset developed by Apple Inc. It was announced in June 2023
and is set to be available for purchase in 2024. The Vision Pro is a
standalone device that integrates digital media with the real world and
allows users to interact with the system through motion gestures, eye
tracking, and voice input. The headset runs on visionOS, a derivative
of iOS designed for extended reality apps. It can also be tethered to
a Macintosh. The development of the Vision Pro has been influenced by
the acquisition of augmented reality companies Metaio and Vrvana, as
well as collaborations with brands like Nike. The device features a
laminated glass display, an adjustable headband, and various sensors,
cameras, and microphones. It uses the Apple M2 system on a chip and
supports custom optical inserts for users with prescription glasses.
The software, visionOS, offers a 3D user interface and compatibility
with a wide range of apps, including familiar iPhone and iPad apps,
games, and virtual meeting platforms like Microsoft Teams and Zoom.

It is important to note that this application was meant as demonstration for how function calls work, and not to be a fool-proof application that will answer any question — it will not be able to answer many questions.

This is a very simple example of use for function calls, and it could have been implemented without function calls, as we’ve described above. However, for more complex examples and applications, using natural language to teach the model and parse out information sent back becomes cumbersome and error-prone, especially given the fact that the model produces non-deterministic results.

In a next post, I will show another example of the use of functions, showing how to use the arguments of the function call to greatly facilitate both parsing information back from the model, as well as instructing the work the model should do.

--

--