Three use-cases for OpenAI Function Calling with practical code examples.
Large Language Models (LLMs), like conversational UIs in general, is efficient at receiving highly unstructured data in the form of conversational natural language.
This unstructured data is then structured, processed and subsequently unstructured again in the form of natural language output.
OpenAI Function Calling structures output for machine consumption in the form of an API, as apposed to human consumption in the form of unstructured natural language.
With the API call, you can provide functions to gpt-3.5-turbo-0613 and gpt-4–0613, and have the model intelligently generate a JSON object containing arguments which you can then in turn use to call the function in your code.
The Chat Completion API does not call the function directly; instead it generates a JSON document which you can use in your code.
Read more about OpenAI Function Calling here.
With OpenAI Function Calling, the focus is on the JSON output.
Here are three practical examples of function calling:
Create chatbots that answer questions by calling external APIs:
In this example, an email is compiled with a few fields defined, which need to be populated from the natural language input.
If the JSON document is not defined correctly, an error will be raised by the OpenAI model. Hence there is a level of rigidity in terms of defining the JSON structure which needs to be populated.
The response from the model:
Convert unstructured natural language into API calls:
In this example an order is converted into a JSON structure with date, notes, order type, delivery address, etc.
Extract structured data from text:
This final example has a list of names and birthdays, which needs to be structured into JSON. Something I found interesting, is that the model does not iterate through the names and dates.
Only the first name and date is placed within the JSON, hence the set of data is not recognised and itterated through.
There are a few considerations, however:
Programmatically the chatbot/Conversational UI will have to know that the output to the LLM must be in JSON format. Hence there will have to be some classification or intent recognition of sorts to detect output type should be JSON.
A predefined template will have to exist and be defined, for input to the completion LLM. As seen in the examples, the JSON template guides the LLM on how to populate the values.
More importantly, as seen in the examples, the parameters which should be populated, must be well defined. Failure results in the following error:
“We could not parse the JSON body of your request. (HINT: This likely means you aren’t using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON. If you have trouble figuring out how to fix this, please contact us through our help center at help.openai.com.)”
I’m currently the Chief Evangelist @ HumanFirst. I explore and write about all things at the intersection of AI and language; ranging from LLMs, Chatbots, Voicebots, Development Frameworks, Data-Centric latent spaces and more.