tools,模型会在需要时返回 tool_calls,由你执行并把结果回传。
curl https://tokendog.io/v1/chat/completions \
-H "Authorization: Bearer $TOKENDOG_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model":"gpt-5",
"messages":[{"role":"user","content":"北京现在天气怎么样?"}],
"tools":[{"type":"function","function":{
"name":"get_weather",
"description":"查询某城市的当前天气",
"parameters":{"type":"object","properties":{"city":{"type":"string"}},"required":["city"]}
}}]
}'
from openai import OpenAI
client = OpenAI(base_url="https://tokendog.io/v1", api_key="YOUR_TOKENDOG_API_KEY")
resp = client.chat.completions.create(
model="gpt-5",
messages=[{"role":"user","content":"北京现在天气怎么样?"}],
tools=[{"type":"function","function":{
"name":"get_weather",
"description":"查询某城市的当前天气",
"parameters":{"type":"object","properties":{"city":{"type":"string"}},"required":["city"]},
}}],
)
print(resp.choices[0].message.tool_calls)
import OpenAI from "openai";
const client = new OpenAI({ baseURL:"https://tokendog.io/v1", apiKey:"YOUR_TOKENDOG_API_KEY" });
const resp = await client.chat.completions.create({
model:"gpt-5",
messages:[{role:"user",content:"北京现在天气怎么样?"}],
tools:[{type:"function",function:{
name:"get_weather",
description:"查询某城市的当前天气",
parameters:{type:"object",properties:{city:{type:"string"}},required:["city"]},
}}],
});
console.log(resp.choices[0].message.tool_calls);
模型返回
tool_calls 后,由你的代码执行函数,再把结果作为 role: "tool" 消息回传,模型据此生成最终答复。