OpenAI’s “Function Calling” stands out as a highly impactful yet often underestimated feature in the realm of software development.
What are GPT Functions?
Functions enable the conversion of unstructured data into structured data. While this concept might seem straightforward, its significance becomes clear when considering that a large percentage of data processing and data entry tasks globally are dedicated to this very purpose. This makes function calling a revolutionary feature that has perhaps not received the attention it deserves.
For those who have struggled to get GPT models (like 3.5 or 4) to provide direct, concise answers without extraneous conversational filler, GPT Functions offer a precise solution.
How Functions Operate
OpenAI’s documentation for function calling can be quite sparse, often requiring users to search developer forums for practical examples. The following is one of the few examples typically found in their official documentation:
functions = [
{
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
}
]
A function definition adheres to a strict JSON format, specifying a function’s name, description, and parameters. The example above illustrates a function designed to retrieve current weather information. While GPT cannot directly invoke a non-existent API, this structured response enables a hypothetical connection to a real API.
Broadly, functions introduce two distinct layers of inference:
Function Selection
Functions are typically supplied to the OpenAI API call as an array. Each function includes a name and description, which GPT uses to determine the most appropriate function for a given prompt. This capability is akin to providing GPT with a multi-tool and asking it to perform a specific task; it intelligently selects the correct instrument.
It is important to note that function definitions contribute to the token count. Including a large number of functions can consume a significant portion of the token limit and may reduce response quality. In many scenarios, passing a single function and explicitly instructing GPT to use it can be more efficient, though the ability to select from multiple functions is valuable for specific applications.
Parameter Value Selection
The true power of function calling often lies in GPT’s ability to select parameter values based on a prompt. While the feature announcement highlighted GPT’s capacity to choose the right tool, its application in populating structured data extends to a broader range of scenarios.
Consider a function as a form presented to GPT. It leverages its reasoning, contextual understanding, and the field names/descriptions to accurately complete each field. The design of this “form” and the supplementary information provided are key areas for creative implementation.
5 Useful Applications
Data Extraction
A frequent application of functions involves extracting specific values from extensive text. This could include retrieving a sender’s address from an email, a founder’s name from a blog post, or a phone number from a landing page.
This process can be visualized as finding a needle in a haystack, where the LLM efficiently isolates the desired information.
Use Case: Processing Contest Submissions
An automation was developed to process numerous contest submissions. Before saving this data to a Google Sheet, it was necessary to extract the email address from each submission. The following function call was utilized for this extraction:
{
"name":"update_email",
"description":"Updates email based on the content of their submission.",
"parameters":{
"type":"object",
"properties":{
"email":{
"type":"string",
"description":"The email provided in the submission"
}
},
"required":[
"email"
]
}
}
Scoring
Functions are highly effective for assigning scores to unstructured data based on dynamic, natural language criteria. This can be applied to tasks such as sentiment analysis of comments, grading essays against a custom rubric, or assessing risk for loan applications based on critical factors. An example application involved scoring sales leads from 0 to 100 based on their viability.
Use Case: Scoring Sales Leads
A collection of prospective leads, stored in a Google Sheet, required prioritization. Each lead included details such as company size, contact name, position, and industry. The following function was used to score each lead from 0 to 100 according to specific criteria, enabling subsequent sorting by importance.
{
"name":"update_sales_lead_value_score",
"description":"Updates the score of a sales lead and provides a justification",
"parameters":{
"type":"object",
"properties":{
"sales_lead_value_score":{
"type":"number",
"description":"An integer value ranging from 0 to 100 that represents the quality of a sales lead based on these criteria. 100 is a perfect lead, 0 is terrible. Ideal Lead Criteria:\n- Medium sized companies (300-500 employees is the best range)\n- Companies in primary resource heavy industries are best, ex. manufacturing, agriculture, etc. (this is the most important criteria)\n- The higher up the contact position, the better. VP or Executive level is preferred."
},
"score_justification":{
"type":"string",
"description":"A clear and conscise justification for the score provided based on the custom criteria"
}
}
},
"required":[
"sales_lead_value_score",
"score_justification"
]
}
Categorization
Functions allow for the definition of custom categories, enabling GPT to thoughtfully analyze data and assign it to the appropriate bucket. This is useful for labeling tasks, such as categorizing YouTube videos, or for discrete scoring, like assigning letter grades to homework.
Use Case: Labeling News Articles
Separating incoming data into distinct streams is a common initial step in many data processing workflows. An automation was recently developed to categorize news articles scraped from the web. The goal was to sort articles by topic and provide a justification for each categorization. The function used for this purpose is shown below:
{
"name":"categorize",
"description":"Categorize the input data into user defined buckets.",
"parameters":{
"type":"object",
"properties":{
"category":{
"type":"string",
"enum":[
"US Politics",
"Pandemic",
"Economy",
"Pop culture",
"Other"
],
"description":"US Politics: Related to US politics or US politicians, Pandemic: Related to the Coronavirus Pandemix, Economy: Related to the economy of a specific country or the world. , Pop culture: Related to pop culture, celebrity media or entertainment., Other: Doesn't fit in any of the defined categories. "
},
"justification":{
"type":"string",
"description":"A short justification explaining why the input data was categorized into the selected category."
}
},
"required":[
"category",
"justification"
]
}
}
Option-Selection
When processing data, it is often necessary to present GPT with multiple options and have it select the most suitable one based on specific requirements. Functions are ideal for this, as they can return only the chosen value without any additional commentary or extraneous information.
Use Case: Identifying the Most Interesting AI News Story from Hacker News
An earlier article detailed the automation of a Twitter account using GPT. A key component of this automation involved selecting the most relevant posts from Hacker News. This post selection process effectively utilizes functions.
In this use case, the initial pages of Hacker News would be scraped, and GPT would be prompted to identify the post most relevant to “AI news or tech news.” GPT would then return only the headline and the corresponding link through functions, allowing for further scraping of the website and tweet generation. The user-defined query would be included in the message, alongside the following function definition:
{
"name":"find_best_post",
"description":"Determine the best post that most closely reflects the query.",
"parameters":{
"type":"object",
"properties":{
"best_post_title":{
"type":"string",
"description":"The title of the post that most closely reflects the query, stated exactly as it appears in the list of titles."
}
},
"required":[
"best_post_title"
]
}
}
Filtering
Filtering is a specialized form of categorization where items are classified as either true or false based on a natural language condition. For instance, a condition like “is Spanish” can effectively filter out all Spanish comments or articles using a simple function followed by a conditional statement.
Use Case: Filtering Contest Submissions
The automation mentioned in the “Data Extraction” section also employed AI-powered filtering to remove contest submissions that did not meet essential criteria. For a coding contest, mandatory requirements such as “must use TypeScript” were enforced. Functions were instrumental in filtering out non-compliant submissions, reducing the total processing set by 90%. The function definition used for this is provided below:
{
"name":"apply_condition",
"description":"Used to decide whether the input meets the user provided condition.",
"parameters":{
"type":"object",
"properties":{
"decision":{
"type":"string",
"enum":[
"True",
"False"
],
"description":"True if the input meets this condition 'Does submission meet the ALL these requirements (uses typescript, uses tailwindcss, functional demo)', False otherwise."
}
},
"required":[
"decision"
]
}
}

