Skip to main content
Tool calling (also called function calling) lets a model decide to invoke functions you define, with arguments it generates. You run the function and feed the result back, so the model can use live data and take actions. Deepshi uses the OpenAI tool-calling format, so it works with the OpenAI SDKs and agent frameworks unchanged.

How it works

1

Define tools

Describe your functions with a JSON Schema for their parameters and pass them in tools.
2

Model requests a call

If the model decides a tool is needed, the response contains tool_calls with the function name and JSON arguments, and finish_reason is tool_calls.
3

You run the function

Execute the function in your own code with the supplied arguments.
4

Return the result

Append a tool message with the output and call the API again. The model uses it to produce a final answer.

Example

from openai import OpenAI
import json

client = OpenAI(base_url="https://api.deepshi.ai/v1", api_key="sk-bf-...")

tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get the current weather for a city.",
        "parameters": {
            "type": "object",
            "properties": {"city": {"type": "string"}},
            "required": ["city"],
        },
    },
}]

messages = [{"role": "user", "content": "What's the weather in Tokyo?"}]

resp = client.chat.completions.create(
    model="deepshi-3.0", messages=messages, tools=tools,
)

call = resp.choices[0].message.tool_calls[0]
args = json.loads(call.function.arguments)

# Run your real function here.
result = {"city": args["city"], "temp_c": 22, "conditions": "clear"}

messages.append(resp.choices[0].message)             # the assistant's tool call
messages.append({
    "role": "tool",
    "tool_call_id": call.id,
    "content": json.dumps(result),
})

final = client.chat.completions.create(
    model="deepshi-3.0", messages=messages, tools=tools,
)
print(final.choices[0].message.content)

Controlling tool use

Use tool_choice to steer the model:
ValueBehavior
"auto"The model decides whether to call a tool. Default when tools are present.
"none"The model never calls a tool and replies with text.
"required"The model must call at least one tool.
{ "type": "function", "function": { "name": "..." } }Force a specific tool.
Set parallel_tool_calls to false if you want at most one tool call per turn.
Tool calling is supported on models that list tools in their supported_features. Check the models catalog. For example, Deepshi 3.0 and the frontier models support tools (Deepshi 2.0 does not).