Note: Up to September 2023 you need to use either the “gpt-3.5-turbo-0613” or “gpt-4-0613” model to use function calling.
To get JSON output from ChatGPT, instead of directly telling it “give me a JSON object with properties x, y, z”, we tell it the function we’re planning to use the JSON object in, in other words “my function requires parameters x, y, z – give me output in that format”.
To achieve that with code, we send a functions
array to the ChatGPT API alongside the model
, messages
, etc. in our usual API request.
The Process
Start with the end in mind:
- Decide on the properties you want in the JSON object.
- Think of the properties as parameters for a function (even if that function is an imaginary one you’ll never use).
- Send details of that function, including those parameters, to ChatGPT.
The more detail you give it, the more likely you are to get the output you want.
Example
Here’s an example where I want a summary of some text, and the JSON output should have title
and summary
properties. I’ve pretended I want these properties for a write_post()
function.
# Definition of our local function(s).
# This is effectively telling ChatGPT what we're going to use its JSON output for.
functions = [
{
"name": "write_post",
"description": "Shows the title and summary of some text.",
"parameters": {
"type": "object",
"properties": {
"title": {
"type": "string",
"description": "Title of the text output."
},
"summary": {
"type": "string",
"description": "Summary of the text output."
}
}
}
}
]
Now put that functions
array in your usual API request like this:
# The request to the ChatGPT API.
response = openai.chat.completions.create(
model = "gpt-3.5-turbo-0613",
messages = [
{
"role": "system",
"content": "You are a useful assistant."
},
{
"role": "user",
"content": f"Here is an article: {article}. Please return a title and summary."
}
],
functions = functions,
function_call = {
"name": functions[0]["name"]
}
)
There’s an optional function_call
property where you can explicitly tell ChatGPT whether to use function calling or not. The values can be:
auto
: Leave it to ChatGPT to decide.none
: Don’t use function calling.- The function name: Enforce function calling (my preferred option).
Reliability
Although not perfect, I’ve found this to be a great improvement over the pre-function-calling era. Occasionally ChatGPT will miss a property in the JSON object, or it will return an additional, unrequested property, so my advice is to have good error handling for such cases.
Tip: For dealing with KeyError
in particular, I recommend using Python’s get()
function to access object or dictionary keys, instead of using square brackets.
Leave a comment