The function doesn’t do the work of deciding when it is necessary and useful: That is what the AI does.
-
An AI model will typically respond to YOU the user.
-
However, AI can instead respond to a function tool recipient when it starts producing an output.
You can experiment with functions in the Responses’ Prompts Playground. (Note that Chat Completions also has function-calling; it just doesn’t have internal tools developed by OpenAI such as “web search”)
Here I have a simple AI setup where I have created several tools that are constantly being offered to the AI (the specification is provided by an internal message):
Let’s examine the structure of one of these:
{
"name": "fortune",
"description": "Returns a horoscope-like sentence randomly.",
"strict": true,
"parameters": {
"type": "object",
"required": [],
"properties": {},
"additionalProperties": false
}
}
It is the most simple of functions. The AI doesn’t have to write any parameters. Calling the function merely acts as a trigger.
Now let’s talk to the AI, and see if it can intuit a good function to employ:
Didn’t call a function. I just get what the AI knew it can already answer about.
Let’s be more explicit in my desires:
The playground environment has called a function. You can see the empty JSON, but the API call would return the function name along with its arguments to be satisfied.
In the playground, “tab” just fills in some AI simulation. My real code might have a long list of predictions from a fortune file. The example functions are meant to be easily programmed.
How it is actually serviced: The developer’s code must call the API again, sending back the assistant’s request (and its ID), along with the return value, which can be natural language - it doesn’t have to be JSON. Then there might be more function calls, or a final response to a user, where the AI has the language generation ability to employ that new knowledge obtained:
You can see the final response is also informed by the AI’s knowledge that it has fun facts also available as a function.
Here’s that playground preset, where you can talk to the AI, maybe as a D&D dungeon master who needs to find out if an attack was successful, or a programmer who needs a random seed value, and get an understanding of what your code can do to help the AI:
https://platform.openai.com/playground/p/8inCYbuAVpAXIu8trnEktGY0?mode=chat