راهنما
فراخوانی ابزارها
هر دو مدل ابزارها را به قالب اوپنایآی فرا میخوانند: تو تابعها را توصیف میکنی، مدل یکی را میخواهد، تو اجرایش میکنی و نتیجه را برمیگردانی.
بررسیشده در ۴ آبان ۱۴۰۵
حلقه
toolsرا بفرست: هر ابزار یک تابع با نام، توضیح و پارامترهای JSON Schema است.- وقتی مدل ابزاری بخواهد،
finish_reasonپاسخtool_callsاست وmessage.tool_callsآنها را فهرست میکند، با آرگومانها بهصورت رشتهٔ JSON. - هرکدام را اجرا کن و یک پیام
toolباtool_call_idآن و نتیجه اضافه کن. - گفتگو را دوباره بفرست. تکرار کن تا
finish_reasonبرابرstopشود.
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-uncensored | auto، none، required یا نام یک تابع. همچنین parallel_tool_calls. |
glm-5.3-flash-uncensored | auto یا none. ابزارها را خودش انتخاب میکند؛ هر چیز دیگر 400 unsupported_tool_choice است. |
نکات
- هر پیام
toolبایدtool_call_idرا داشته باشد که به آن پاسخ میدهد، وگرنه تماس با400 invalid_messagesرد میشود. - طرحوارههای ابزار با هر درخواست فرستاده میشوند و بهعنوان توکن ورودی حساب میشوند.
- مدلها خودشان چیزی اجرا نمیکنند. کد تو ابزارها را اجرا میکند، پس تو تعیین میکنی به چه چیزی دسترسی دارند.