استدعاء الأدوات

استدعاء الأدوات والدوال

صِف دوالك، وسيقوم نموذج يدعم الأدوات بالطلب منك تشغيل إحداها. تترجم 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);

دور استدعاء الأداة (tool-call)

الاستجابة
{
  "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 دون تعديل ليبقى تفكير النموذج الخاص سليماً في رحلة الذهاب والعودة.
أدوات الدوال فقط. لا تشغل Oxyy أي أدوات استضافة خاصة بها — فلا توجد ميزة البحث في الويب، أو مترجم الكود، أو البحث في الملفات لتفعيلها. قم بتعريف دالة وتشغيلها في تطبيقك. أدوات خادم Anthropic هي الاستثناء: يتم إعادة توجيهها إلى المزود دون تغيير عبر Anthropic SDK.