Tool & function calling
Models can ask to call functions you define. You pass a tools schema, the model returns
tool_calls, you execute them and send the results back.
ℹ Note
Tool calling uses the standard OpenAI-compatible shape. When a request includes tools,
AIx automatically selects an eligible route that supports the complete tool-calling payload.
Pick a model known to support tools.
1 · Define tools and send
from openai import OpenAI
client = OpenAI(base_url="https://api.aix.theaimart.co/v1", api_key="$AIX_KEY")
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 Mumbai?"}]
resp = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V3.2",
messages=messages,
tools=tools,
tool_choice="auto",
)
2 · Execute the tool calls
msg = resp.choices[0].message
if msg.tool_calls:
messages.append(msg) # the assistant's tool-call turn
for call in msg.tool_calls:
args = json.loads(call.function.arguments)
result = get_weather(args["city"]) # your real function
messages.append({
"role": "tool",
"tool_call_id": call.id,
"content": json.dumps(result),
})
3 · Send results back for the final answer
final = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V3.2",
messages=messages,
tools=tools,
)
print(final.choices[0].message.content)
Notes
tool_choiceaccepts"auto","none","required", or a specific function.- The model may request several tool calls in one turn — execute all, append one
toolmessage pertool_call_id, then call again. - Loop steps 1–3 until the model returns content with no further
tool_calls.