Function / Tool Calling

به مدل اجازه دهید یک یا چند تابع (ابزار) شما را با پارامترهای مناسب فراخوانی کند.

اندپوینت

POSThttps://api.darvareh.ir/v1/chat/completions

۱. تعریف ابزار

ابزار را با JSON Schema به مدل معرفی کنید.

{
  "model": "gpt-5.5",
  "messages": [
    {"role": "user", "content": "هوای تهران چطوره؟"}
  ],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "دریافت وضعیت آب‌وهوای یک شهر",
        "parameters": {
          "type": "object",
          "properties": {
            "city": { "type": "string", "description": "نام شهر به فارسی یا انگلیسی" },
            "unit": { "type": "string", "enum": ["c", "f"] }
          },
          "required": ["city"]
        }
      }
    }
  ],
  "tool_choice": "auto"
}

۲. پاسخ مدل شامل tool_calls

{
  "choices": [{
    "message": {
      "role": "assistant",
      "content": null,
      "tool_calls": [{
        "id": "call_01H...",
        "type": "function",
        "function": {
          "name": "get_weather",
          "arguments": "{\"city\":\"Tehran\",\"unit\":\"c\"}"
        }
      }]
    },
    "finish_reason": "tool_calls"
  }]
}

۳. اجرای ابزار و ادامۀ گفت‌وگو

خروجی ابزار را به‌عنوان پیام با نقش tool و همان tool_call_id برگردانید تا مدل جواب نهایی را تولید کند.

{
  "model": "gpt-5.5",
  "messages": [
    {"role": "user", "content": "هوای تهران چطوره؟"},
    {
      "role": "assistant",
      "tool_calls": [{
        "id": "call_01H...",
        "type": "function",
        "function": { "name": "get_weather", "arguments": "{\"city\":\"Tehran\"}" }
      }]
    },
    {
      "role": "tool",
      "tool_call_id": "call_01H...",
      "content": "{\"temp\": 24, \"unit\": \"c\", \"desc\": \"آفتابی\"}"
    }
  ]
}

نمونۀ کامل در Python

import json
from openai import OpenAI

client = OpenAI(api_key="…", base_url="https://api.darvareh.ir/v1")

def get_weather(city: str, unit: str = "c"):
    return {"temp": 24, "unit": unit, "desc": "آفتابی"}

tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {"type": "string"},
                "unit": {"type": "string", "enum": ["c","f"]},
            },
            "required": ["city"],
        },
    },
}]

messages = [{"role": "user", "content": "هوای تهران چطوره؟"}]
r = client.chat.completions.create(model="gpt-5.5", messages=messages, tools=tools)
msg = r.choices[0].message

if msg.tool_calls:
    call = msg.tool_calls[0]
    args = json.loads(call.function.arguments)
    result = get_weather(**args)

    messages += [
        msg,
        {"role": "tool", "tool_call_id": call.id, "content": json.dumps(result)},
    ]
    final = client.chat.completions.create(model="gpt-5.5", messages=messages)
    print(final.choices[0].message.content)

tool_choice

  • "auto" — مدل خودش تصمیم می‌گیرد ابزار را صدا بزند یا نه.
  • "none" — استفاده از ابزار غیرفعال.
  • شیء مشخص، مثلاً {"type":"function","function":{"name":"get_weather"}} — اجبار به فراخوانی یک ابزار خاص.