OpenAI’s “function calls” may be the most innovative and underappreciated feature ever released by a software company.
Features allow you to: Convert unstructured data to structured data. This may not sound all that groundbreaking, but given that 90% of data processing and data entry jobs around the world exist for this very reason, it’s not something that gets much attention. This is a very innovative feature.
have you ever found yourself begging Do you want GPT (3.5 or 4) to just spit out the answers you need and nothing else? No “Yes, this is your…” or other unnecessary drivel surrounding the core answer. GPT functions are the solution you were looking for.
How do Functions work?
OpenAI’s documentation on function calls is very limited. You’ll find yourself searching developer forums for examples of how to use it. I looked through the forums for you and a lot of examples came up.
Below is one of the few examples you can find in the 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 is a strict JSON format that defines the function name, description, and parameters. In this case, the function is intended to retrieve the current weather. Obviously, GPT can’t call this real API (because it doesn’t exist), but this structured response allows us to virtually connect to it.
However, at a high level, functions provide two layers of inference.
Select the function itself:
You may notice that functions are passed to OpenAI API calls as arrays. You specify a name and description for each function so that GPT determines which one to use based on the prompts you provide. Providing multiple functions in an API call is like giving GPT a Swiss Army knife and asking it to cut the tree in half. Even if you have pliers, scissors, and a knife, you know you should use a saw.
Function definitions contribute to the number of tokens. Passing hundreds of functions not only takes up a large portion of your token limit, but also leads to poor response quality. In most cases, you don’t use this feature and just pass one function that forces its use. However, it can be very useful in certain use cases.
Select parameter values based on prompts.
This is the real magic in my opinion. GPT’s ability to choose the tools in its tool kit is amazing and is definitely the focus of feature announcements, but I think this applies to more use cases.
Imagine a function that passes a form to fill out in GPT.. Use reasoning, situational context, and field names/descriptions to decide how to fill in each field. You can be creative in designing the form and any additional information you pass.
One of the most common purposes I use functions is to extract a specific value from a large chunk of text. Email sender addresses, blog post founder names, and landing page phone numbers.
I imagine looking for a needle in a haystack, but LLM burns the haystack and leaves only the needle.
Use case: Processing thousands of contest submissions
I built an automation that iterates over thousands of contest submissions. Before saving these to Google Sheets, I wanted to extract the emails associated with the submissions. This is the function call I used to extract the emails.
"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"
]
Assigning scores to unstructured data based on dynamic natural language criteria is a great use case for functions. You can score comments during sentiment analysis, essays based on a custom scoring rubric, and loan applications for risk based on key factors. A recent use case I applied scoring was to score sales prospects from 0 to 100 based on their likelihood of action.
Use case: Scoring sales leads
A few months ago, we had hundreds of leads in one Google sheet and wanted to work on them in order of importance. Each lead included information such as company size, contact name, job title, and industry.
We used the following function to score each lead from 0 to 100 based on need and sorted from best to worst.
"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"
]
When you define custom buckets, GPT carefully considers each piece of data you give it and places it in the correct bucket. This can be used for labeling tasks, such as selecting categories for YouTube videos, or individual grading tasks, such as assigning letter grades to homework.
Example use: Labeling news articles.
A very common first step in a data processing workflow is to separate incoming data into different streams. An automation I recently built did just this using news articles collected from the web. I wanted to categorize them based on the theme of the article and once again include the rationale for the decision. The functions I used are:
"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"
]
When processing data, you often want to give GPT many possible options and let it choose the best one based on your needs. All you need is the selected value, no surrounding fluff or additional thought. Functions are perfect for this.
Use case: Find the “most interesting AI news articles” from Hacker News
I wrote another Medium article here about how to automate your entire Twitter account using GPT. Part of that process involves selecting the most relevant posts from the Hacker News front page. This post-selection step leverages functions.
To summarize the functional part of our use case, we scrape the first n pages of hacker news and ask GPT to select the most relevant posts for “AI news or technology news.” GPT returns only selected links via headings and functions, so you can scrape the website and generate tweets from it.
Pass the user-defined query as part of the message and use 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 is a subset of classification that categorizes items as true or false based on natural language conditions. A condition like “This is Spanish” can filter out all Spanish comments, articles, etc. using a simple function and a conditional statement immediately following.
Use case: Filtering contest submissions
The same automation described in the Data Extraction section used AI-powered filtering to exclude contest submissions that did not meet the transaction-violating criteria. Things like “you must use typescript” were absolutely mandatory for the coding contest at hand. We used functions to filter our submissions and reduced the overall set we were processing by 90%. Here is the function definition used.
"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"
]
If you’re interested in why I love functions so much or what I’ve built with them, check out AgentHub.
AgentHub is Startup with Y Combinator I co-founded a company that allows you to automate repetitive or complex workflows using AI through a simple drag-and-drop no-code platform.
“Imagine Zapier: AI-first and cutting-edge technology.” – myself
Automation is built on individual nodes called “operators” that are linked together to create a power AI pipeline. We have a catalog of AI-powered operators that leverage our internal capabilities.
To see examples of how to use AgentHub’s functions, check out these templates: Scoring, Classification, Option Selection,
If you want to start building AgentHub, AgentHub is publicly available and ready to use. We are active in the Discord community and would be happy to help you build automation as needed.
Please feel free to follow me AgentHub official Twitter Update and I myself am in charge of AI-related content.