المظهر
اللغة

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

كيفية استخدام استدعاء الدوال (استخدام الأدوات) مع GetVibe API: المعاملان tools وtool_choice ومحادثة أدوات متعددة الأدوار. حلقة وكيل كاملة بـ Python وcurl.

يتيح استدعاء الدوال (استخدام الأدوات) للنموذج طلب استدعاءات إلى دوالك واستلام النتيجة. مدعوم في جميع النماذج المتوافقة مع OpenAI.

المعلمات

المعلمةالنوعالوصف
toolsarrayGPT · Claudeقائمة الأدوات. كل كائن: { type: "function", function: { name, description, parameters } }.
tool_choicestring | objectGPT · Claude"auto" (يقرره النموذج)، أو "none" (لا تستدعِه)، أو "required" (يجب الاستدعاء)، أو { type: "function", function: { name } } — أداة محددة بعينها.
parallel_tool_callsbooleanGPTtrue — يطلب النموذج عدة أدوات في آنٍ واحد. القيمة الافتراضية true. لا يدعم Claude هذا العلم لكنه ينفّذ الاستدعاءات المتوازية بنفسه.

مثال: استدعاء واحد

curl https://getvibeapi.com/v1/chat/completions \
  -H "Authorization: Bearer $GETVIBE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-6.1-sol",
    "tools": [{
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "Returns the current weather in a city",
        "parameters": {
          "type": "object",
          "properties": {
            "city": { "type": "string", "description": "City name" }
          },
          "required": ["city"]
        }
      }
    }],
    "tool_choice": "auto",
    "messages": [{"role": "user", "content": "What is the weather in London?"}]
  }'

جولات متعددة: إعادة تمرير النتيجة

بعد استدعاء الأداة أضف رسالة بدور tool تتضمن النتيجة وأرسل الطلب التالي.

# messages after the first response with tool_call:
messages = [
    {"role": "user", "content": "What is the weather in London?"},
    # model response with the tool call
    {"role": "assistant", "tool_calls": msg.tool_calls, "content": None},
    # the result of your function
    {
        "role": "tool",
        "tool_call_id": msg.tool_calls[0].id,
        "content": json.dumps({"city": "London", "temp": 18, "desc": "Cloudy"}),
    },
]
resp2 = client.chat.completions.create(model="gpt-6.1-sol", tools=tools, messages=messages)
print(resp2.choices[0].message.content)
# → "It is currently 18°C and cloudy in London."

استجابة مثال (tool_call)

{
  "choices": [{
    "message": {
      "role": "assistant",
      "content": null,
      "tool_calls": [{
        "id": "call_abc123",
        "type": "function",
        "function": {
          "name": "get_weather",
          "arguments": "{\"city\":\"London\"}"
        }
      }]
    },
    "finish_reason": "tool_calls"
  }]
}
الحقلان tools وtool_choice مقبولان بالصيغة المتوافقة مع OpenAI؛ لكن الدعم الفعلي يعتمد على النموذج المختار. لا تعتمد على تمرير البايتات حرفيًا ولا على حقول الاستجابة الخاصة بمزوّد بعينه.
استدعاء الدوال في GetVibe API — tools وtool_choice وأمثلة Python | GetVibe API