Skip to main content

Function Calls

For models that supports function calls, you can pass functions information to the model/assistant, just as the original OpenAI does.

The model/assistant will then call the function when they think necessary, and use the result in the conversation.

Here's an example of a function that fetches current weather information for a given location:

functions = [
{
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
}
]

Let's pass it to the assistant or model and request it to generate a response, with OpenAI Python client as gateway:

response = client.chat.completions.create(
model="YOUR_TASKINGAI_MODEL_ID",
messages=[
{"role": "system", "content": "You can ask me about the latest weather information of a city."},
{"role": "user", "content": "How's the weather now in Seattle?"}
],
functions=functions,
)

Tool calls compatible with OpenAI

Function call was deprecated in OpenAI API, and tool_calls was introduced as a replacement. TaskingAI's OpenAI-compatible API supports both functions and tool_calls for backward compatibility.

Here's an example of a tool call that calculates the sum of two variables to the model. Note that the schema is different than using functions:

tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
}
}
]

response = client.chat.completions.create(
model="YOUR_TASKINGAI_MODEL_ID",
messages=[
{"role": "system", "content": "You can ask me about the latest weather information of a city."},
{"role": "user", "content": "How's the weather now in Seattle?"}
],
tools=tools,
)