پرش به محتوا
همه مستندات

راهنما

فراخوانی ابزارها

هر دو مدل ابزارها را به قالب اوپن‌ای‌آی فرا می‌خوانند: تو تابع‌ها را توصیف می‌کنی، مدل یکی را می‌خواهد، تو اجرایش می‌کنی و نتیجه را برمی‌گردانی.

بررسی‌شده در ۴ آبان ۱۴۰۵

حلقه

  1. tools را بفرست: هر ابزار یک تابع با نام، توضیح و پارامترهای JSON Schema است.
  2. وقتی مدل ابزاری بخواهد، finish_reason پاسخ tool_calls است و message.tool_calls آن‌ها را فهرست می‌کند، با آرگومان‌ها به‌صورت رشتهٔ JSON.
  3. هرکدام را اجرا کن و یک پیام tool با tool_call_id آن و نتیجه اضافه کن.
  4. گفتگو را دوباره بفرست. تکرار کن تا finish_reason برابر stop شود.
Python
import json
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://nokeep.ai/v1",
    api_key=os.environ["NOKEEP_API_KEY"],
)

tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Today's weather in a city",
        "parameters": {
            "type": "object",
            "properties": {"city": {"type": "string"}},
            "required": ["city"],
        },
    },
}]

def get_weather(city):
    return {"city": city, "high_c": 21, "sky": "clear"}

messages = [{"role": "user", "content": "Should I bring a jacket in Lisbon today?"}]
while True:
    reply = client.chat.completions.create(model="deepseek-v4.1-flash-uncensored", messages=messages, tools=tools)
    message = reply.choices[0].message
    messages.append(message)
    if not message.tool_calls:
        print(message.content)
        break
    for call in message.tool_calls:
        result = get_weather(**json.loads(call.function.arguments))
        messages.append({"role": "tool", "tool_call_id": call.id, "content": json.dumps(result)})

tool_choice

مدلمی‌پذیرد
deepseek-v4.1-flash-uncensoredauto، none، required یا نام یک تابع. همچنین parallel_tool_calls.
glm-5.3-flash-uncensoredauto یا none. ابزارها را خودش انتخاب می‌کند؛ هر چیز دیگر 400 unsupported_tool_choice است.

نکات

  • هر پیام tool باید tool_call_id را داشته باشد که به آن پاسخ می‌دهد، وگرنه تماس با 400 invalid_messages رد می‌شود.
  • طرح‌واره‌های ابزار با هر درخواست فرستاده می‌شوند و به‌عنوان توکن ورودی حساب می‌شوند.
  • مدل‌ها خودشان چیزی اجرا نمی‌کنند. کد تو ابزارها را اجرا می‌کند، پس تو تعیین می‌کنی به چه چیزی دسترسی دارند.