import os
import json
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("API_KEY") or "<YOUR API KEY>",
base_url="https://api.siflow.cn/model-api",
)
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a city",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string", "description": "City name"}},
"required": ["city"],
},
},
}]
def get_weather(city: str) -> dict:
# Replace with a real API call
return {"city": city, "temp_c": 21, "condition": "Sunny"}
messages = [{"role": "user", "content": "What's the weather like in Paris today?"}]
# 1) First call — the model decides to use the tool
resp = client.chat.completions.create(
model="Qwen/Qwen3-32B",
messages=messages,
tools=tools,
tool_choice="auto",
max_completion_tokens=1024,
)
msg = resp.choices[0].message
if msg.tool_calls:
messages.append(msg) # append the assistant turn with tool_calls
for call in msg.tool_calls:
args = json.loads(call.function.arguments)
result = get_weather(**args)
# 2) Return the tool result, linked by tool_call_id
messages.append({
"role": "tool",
"tool_call_id": call.id,
"content": json.dumps(result),
})
# 3) Second call — the model writes the final answer
final = client.chat.completions.create(
model="Qwen/Qwen3-32B",
messages=messages,
max_completion_tokens=1024,
)
print(final.choices[0].message.content)
else:
print(msg.content)