Function Calls
TaskingAI-Inference also supports passing function information to the model (only for models that support functions), and let the models decide when to use those functions.
To pass function information to the model, you need to add a functions
field in the body of the request. Each item in functions
is a function object following OpenAI's definition of function object.
For a detailed API reference for function call, please refer to API Reference.
Here's an example of passing a function that calculates the sum of two variables to the model:
{
"functions": [
{
"name": "add_two_number",
"description": "Add two number and return sum",
"parameters": {
"type": "object",
"properties": {
"a": {
"type": "number",
"description": "First number."
},
"b": {
"type": "number",
"description": "Second number."
}
},
"required": ["a", "b"]
}
}
],
... other parameters in body
}
When the model decides to call one of your functions, it will generate a JSON object with the arguments for the function.
An example of the generated JSON object indicating the calling the function above will look like:
{
"content": null,
"role": "assistant",
"function_calls": [
{
"arguments": {
"a": 16,
"b": 18
},
"name": "add_two_number",
"id": "call_123"
}
]
}
In this response, it states which function to call, and what the arguments will be. You should then perform the function and feed back the execution result to the model in the following format as the last message in Messages
:
{
"role": "function",
"content": "34",
"name": "add_two_number",
"id": "call_123"
}