First, you must specify the available functions using JSON schemas. Here is an example:
When using the Completions client, you can provide the list of tools as an argument:
In the model's response, a message might contain some number of function calls. These can be accessed as follows:
As an example, the function call returned by the model might have name get_temperature and arguments {"date": "2024-04-25", "location": "Ohio", "units": "fahrenheit"}.
After receiving the function call from the model, if this corresponds to a tool you have access to, you can pass the arguments from the tool and get a result. Then, if you wish to pass the result back to the model, you can use the tool role.
For instance, if you stored the return result in a variable temperature_result:
Using the same tools as above, here is an example of streaming with function calling. For simplicity, we assume there is only one function call.
When streaming, the name will always be provided before the arguments. If there are multiple function calls, you can detect the start of the next function call when the previous arguments has no unclosed curly braces.