Tool calling / function calling with Velqa — OpenAI style
Tool calling lets a model call your functions: you describe tools in the tools parameter, the model replies with tool_calls, you run the matching function, then send the result back in a role: "tool" message. Velqa follows the OpenAI format exactly — just point your base_url at https://api.velqa.dev/v1.
The 4-step cycle
- You send the request with the
toolslist (function schemas). - The model replies with
finish_reason: "tool_calls"and one or moretool_calls. - You run the function(s) in your code.
- You send the result back in a
{"role": "tool", ...}message, then call the model again to get the final answer.
Full Python example (get_weather tool)
import json
from openai import OpenAI
client = OpenAI(base_url="https://api.velqa.dev/v1", api_key="sk-...")
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Return the current weather for a city.",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name"}
},
"required": ["city"],
},
},
}
]
# Your real implementation would call a weather API.
def get_weather(city):
return {"city": city, "temp_c": 24, "condition": "sunny"}
messages = [{"role": "user", "content": "What's the weather in Rabat?"}]
# Steps 1-2: the model decides to call the tool
resp = client.chat.completions.create(
model="kimi-k2.6",
messages=messages,
tools=tools,
)
msg = resp.choices[0].message
messages.append(msg) # keep the tool call in the history
# Step 3: run each tool_call
for call in msg.tool_calls:
args = json.loads(call.function.arguments)
result = get_weather(**args)
messages.append({
"role": "tool",
"tool_call_id": call.id,
"content": json.dumps(result),
})
# Step 4: the model produces the final answer from the result
final = client.chat.completions.create(
model="kimi-k2.6",
messages=messages,
tools=tools,
)
print(final.choices[0].message.content)Shape of a tool_call
{
"id": "call_abc123",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\": \"Rabat\"}"
}
}The arguments field is a JSON string — remember to parse it (json.loads) before use.
Best practices
- Always add the
assistantmessage containing thetool_callsto the history before thetoolmessages, otherwise the model loses the link. - The
tool_call_idof thetoolmessage must match theidof thetool_call. - A model can request multiple tools in parallel: loop over all
tool_calls.
Recommended models for tools
Tool calling is especially reliable with kimi-k2.6, minimax-m3 and glm-4.7. See the models list.
See also: chat overview, streaming.
