工具调用

工具与函数调用

描述您的函数,支持工具的模型将请求您运行其中一个。Oxyy 会将声明转换为各个提供商所需的格式,因此同一个 tools 数组适用于 OpenAI、Anthropic、Google、xAI 以及其他所有平台。

参数类型必填描述
toolsarray可选函数声明:{type: "function", function: {name, description, parameters}},其中 parameters 是一个 JSON Schema 对象。
tool_choicestring|object可选模型调用工具的自由度。 以下之一:noneautorequired{type:"function",function:{name}} 默认 auto
parallel_tool_callsboolean可选允许每轮进行多个工具调用。 默认 true
import json, os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["OXYY_API_KEY"],
    base_url="https://api.oxyy.ai/v1"
)

tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Current weather for a city.",
        "parameters": {
            "type": "object",
            "properties": {"city": {"type": "string"}},
            "required": ["city"],
        },
    },
}]

messages = [{"role": "user", "content": "Weather in Dhaka?"}]
response = client.chat.completions.create(
    model="gpt-5.5", messages=messages, tools=tools, tool_choice="auto"
)

call = response.choices[0].message.tool_calls[0]
args = json.loads(call.function.arguments)

# Run the tool yourself, then send the result back as a tool message.
messages.append(response.choices[0].message)
messages.append({
    "role": "tool",
    "tool_call_id": call.id,
    "content": json.dumps({"temp_c": 31, "sky": "humid"}),
})

final = client.chat.completions.create(model="gpt-5.5", messages=messages, tools=tools)
print(final.choices[0].message.content)
import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: process.env.OXYY_API_KEY,
  baseURL: 'https://api.oxyy.ai/v1'
});

const tools = [{
  type: 'function',
  function: {
    name: 'get_weather',
    description: 'Current weather for a city.',
    parameters: {
      type: 'object',
      properties: { city: { type: 'string' } },
      required: ['city'],
    },
  },
}];

const messages = [{ role: 'user', content: 'Weather in Dhaka?' }];
const res = await client.chat.completions.create({
  model: 'gpt-5.5', messages, tools, tool_choice: 'auto',
});

const call = res.choices[0].message.tool_calls[0];
const args = JSON.parse(call.function.arguments);

messages.push(res.choices[0].message, {
  role: 'tool',
  tool_call_id: call.id,
  content: JSON.stringify({ temp_c: 31, sky: 'humid' }),
});

const final = await client.chat.completions.create({ model: 'gpt-5.5', messages, tools });
console.log(final.choices[0].message.content);

工具调用轮次

响应
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1700000000,
  "model": "gpt-5.5",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": null,
        "tool_calls": [
          {
            "id": "call_abc123",
            "type": "function",
            "function": {
              "name": "get_weather",
              "arguments": "{\"city\":\"Dhaka\"}"
            }
          }
        ],
        "refusal": null
      },
      "logprobs": null,
      "finish_reason": "tool_calls"
    }
  ],
  "usage": { "prompt_tokens": 61, "completion_tokens": 18, "total_tokens": 79, "cost": 0.00029 }
}
循环。 追加助手消息 原封不动地返回,然后一个 {role:"tool", tool_call_id, content}每次调用发送一条消息,并再次发送整个对话。 finish_reason 为 tool_calls当存在调用时,即使在报告的提供商上也是如此 stop. 在推理模型上,发回任何 reasoning_details 保持未修改,以便模型自身的思考能够在往返中保留。
仅限 Function tools。 Oxyy 不运行自己的托管工具 —— 没有可启用的网页搜索、代码解释器或文件搜索。请在您的应用程序中声明并运行函数。Anthropic 的服务器工具是个例外:它们会原封不动地转发给提供商,通过 Anthropic SDK.