Native JSON Output From GPT-4
When integrating LLMs into your products, you often want to generate structured data, like JSON. With the help of function calling (released June 13th 2023), this process has become much simpler!
In this post, I will explore the new API.
Function Calling
Function calling allows GPT to call a function instead of returning a string. At the time of writing, this feature is available for the chat models gpt-3.5-turbo-0613
and gpt-4-0613
.
For this feature, two new parameters have been introduced in the Chat Completions API:
functions
: A list of functions available to GPT, each with aname
,description
and a JSON Schema of theparameters
.function_call
: You can force GPT to use a specific function (or explicitly forbid calling any functions).
I realized that by setting the function_call
to a specific function, you can reliably expect JSON as the response from GPT calls. No more strings, yay!
Let’s see it in action with a demo app, Recipe Creator.
Recipe Creator
Recipe Creator is an app where the user inputs the name of a dish and is provided with instructions on how to cook it. Of course, it can be used to generate recipes for completely fictional…