Function and Tool Calls
In most cases, you can integrate appropriate plugins into an assistant application directly on the platform. However, if you want to implement certain tool-calling logic locally, you can use OpenAI's function calling feature.
When the assistant's language model supports function calls, you can pass function information to the invocation payload, just like with OpenAI's original implementation. The assistant will call the function when deemed necessary and use the result in the conversation.
Function Calls
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"],
},
}
]
To pass this function to the assistant or model and request a response, use the OpenAI Python client as a 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
Function calls were deprecated in the OpenAI API, and tool_calls
were 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 gets the current weather for a location. Note that the schema is different compared to using function calls:
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,
)