Table of Contents
In January 2024, OpenAI introduced Function Calling, a powerful feature designed to allow developers to generate structured JSON output with GPT models. This feature addresses one of the biggest pain points developers have faced when using large language models: dealing with unstructured text. With OpenAI Function Calling, developers no longer need to rely on prompt engineering or regular expressions (RegEx) to parse responses into usable data formats. In this guide, we’ll walk through how to use Function Calling, its applications, and how it can help you build more reliable AI applications.
Before diving into OpenAI Function Calling, here’s a detailed guide on how to learn AI that you may refer to.
What is OpenAI Function Calling?
Before Function Calling, developers using the OpenAI API often faced challenges in working with unstructured data. When trying to extract structured information like names, grades, or other specific details from text, it was difficult to ensure consistent results. Developers had to depend on prompt engineering or RegEx to parse the text into structured formats like JSON. This approach worked but was inefficient and prone to errors.
OpenAI Function Calling allows GPT models like GPT-3.5 and GPT-4 to take user-defined functions and output structured JSON directly, which makes data extraction more reliable and consistent. With this capability, developers can integrate AI seamlessly into applications that require structured outputs, such as forms, databases, or other systems that require regular, predictable data.
Using OpenAI Without Function Calling
Before diving into function calling, let’s explore how to extract data without this feature.
Read this small guide on OpenAI API Key in case you need help with finding your API key.
First, you’ll need to set up the OpenAI Python API. If you haven’t already, follow the steps to obtain your API key and install the OpenAI library using:
bashCopy codepip install --upgrade openai -q
Once you’ve set up your API key and initialized the OpenAI client, you can generate outputs using a prompt:
pythonCopy codeimport os
from openai import OpenAI
client = OpenAI(
api_key=os.environ['OPENAI_API_KEY'],
)
student_1_description = "David Nguyen is a sophomore majoring in computer science at Stanford University..."
prompt1 = f'''
Please extract the following information from the given text and return it as a JSON object:
name
major
school
grades
club
This is the body of text to extract the information from:
{student_1_description}
'''
openai_response = client.chat.completions.create(
model='gpt-3.5-turbo',
messages=[{'role': 'user', 'content': prompt1}]
)
openai_response.choices[0].message.content
This method works well for straightforward prompts, but it can be inconsistent. For instance, if you use the same prompt with different data, the output structure might vary.
pythonCopy codestudent_2_description = "Ravi Patel is a sophomore majoring in computer science at the University of Michigan..."
When you compare outputs for multiple students, the results might not be the same. This inconsistency is what OpenAI Function Calling aims to resolve.
Here, we might get different JSON structures, such as returning a list of clubs for one student but not for another. These inconsistencies can be problematic when building applications that rely on structured, predictable outputs.
OpenAI Function Calling Example
Now, let’s see how Function Calling can resolve these issues. We will define a custom function that extracts student information in a structured format. The function defines parameters like name, major, school, GPA, and club.
pythonCopy codestudent_custom_functions = [
{
'name': 'extract_student_info',
'description': 'Get the student information from the body of the input text',
'parameters': {
'type': 'object',
'properties': {
'name': {
'type': 'string',
'description': 'Name of the person'
},
'major': {
'type': 'string',
'description': 'Major subject.'
},
'school': {
'type': 'string',
'description': 'The university name.'
},
'grades': {
'type': 'integer',
'description': 'GPA of the student.'
},
'club': {
'type': 'string',
'description': 'School club for extracurricular activities.'
}
}
}
}
]
student_description = [student_1_description, student_2_description]
for i in student_description:
response = client.chat.completions.create(
model='gpt-3.5-turbo',
messages=[{'role': 'user', 'content': i}],
functions=student_custom_functions,
function_call='auto'
)
json_response = json.loads(response.choices[0].message.function_call.arguments)
print(json_response)
This time, we get a consistent output:
jsonCopy code{'name': 'David Nguyen', 'major': 'computer science', 'school': 'Stanford University', 'grades': 3.8, 'club': 'Robotics Club'}
{'name': 'Ravi Patel', 'major': 'computer science', 'school': 'University of Michigan', 'grades': 3.7, 'club': 'Chess Club'}
This ensures the output is always structured in a predictable format, solving the inconsistencies from the previous method.
Multiple Custom Functions
OpenAI Function Calling allows you to use multiple functions. For example, you can extract both student and school information using two different functions.
Here’s how you can define an additional function to extract school information:
pythonCopy codecustom_functions = [
{
'name': 'extract_student_info',
'description': 'Get the student information from the body of the input text',
'parameters': {
'type': 'object',
'properties': {
'name': {
'type': 'string',
'description': 'Name of the person'
},
'major': {
'type': 'string',
'description': 'Major subject.'
},
'school': {
'type': 'string',
'description': 'The university name.'
},
'grades': {
'type': 'integer',
'description': 'GPA of the student.'
},
'club': {
'type': 'string',
'description': 'School club for extracurricular activities.'
}
}
}
},
{
'name': 'extract_school_info',
'description': 'Get the school information from the body of the input text',
'parameters': {
'type': 'object',
'properties': {
'name': {
'type': 'string',
'description': 'Name of the school.'
},
'ranking': {
'type': 'integer',
'description': 'QS world ranking of the school.'
},
'country': {
'type': 'string',
'description': 'Country of the school.'
},
'no_of_students': {
'type': 'integer',
'description': 'Number of students enrolled in the school.'
}
}
}
}
]
You can now pass both student and school descriptions to the model, and it will automatically choose the correct function based on the input data:
pythonCopy codedescription = [student_1_description, school_1_description]
for i in description:
response = client.chat.completions.create(
model='gpt-3.5-turbo',
messages=[{'role': 'user', 'content': i}],
functions=custom_functions,
function_call='auto'
)
json_response = json.loads(response.choices[0].message.function_call.arguments)
print(json_response)
With OpenAI Function Calling, the model consistently provides structured outputs, improving the reliability of your applications.
Applications of OpenAI Function Calling
There are numerous ways developers can utilize OpenAI Function Calling:
- Extracting structured data: Extract information from text in JSON format without manual prompt engineering.
- Interacting with APIs: Call external APIs and extract data from responses in a structured format.
- SQL and database operations: Automatically generate structured queries to interact with databases.
- Text summarization: Summarize content in a consistent, structured way using predefined functions.
For instance, you can summarize both student and school data using the custom functions we defined earlier:
pythonCopy codedef extract_student_info(name, major, school, grades, club):
return f"{name} is majoring in {major} at {school}. They have a {grades} GPA and are part of the {club}."
def extract_school_info(name, ranking, country, no_of_students):
return f"{name} is in {country}, ranked #{ranking} in the world, with {no_of_students} students."
By passing your descriptions through the model, you can generate precise, structured responses.
Conclusion
OpenAI Function Calling is a powerful tool that solves many of the challenges developers face when working with unstructured text. By allowing developers to define custom functions and generate structured JSON outputs, it eliminates the need for RegEx and reduces the inconsistencies that arise when relying on traditional prompt engineering. Whether you’re extracting structured data, interacting with APIs, or creating complex applications, Function Calling makes the process more efficient and reliable.
For developers looking to integrate this feature, it provides a clear path to building more robust and stable AI-driven applications.
By using OpenAI Function Calling, developers can unlock powerful capabilities and deliver better, more reliable AI applications with fewer inconsistencies and errors.