> ## Documentation Index
> Fetch the complete documentation index at: https://doc.tokendog.io/llms.txt
> Use this file to discover all available pages before exploring further.

# 工具调用

> 用 tools 让模型调用函数

在请求中声明 `tools`，模型会在需要时返回 `tool_calls`，由你执行并把结果回传。

<CodeGroup>
  ```bash curl theme={null}
  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"]}
      }}]
    }'
  ```

  ```python Python theme={null}
  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)
  ```

  ```javascript Node theme={null}
  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);
  ```
</CodeGroup>

<Note>模型返回 `tool_calls` 后，由你的代码执行函数，再把结果作为 `role: "tool"` 消息回传，模型据此生成最终答复。</Note>
