Вызов инструментов (tool calling)
Вызов инструментов (tool calling, function calling) позволяет модели попросить
ваш код выполнить функцию, а затем продолжить работу с её результатом.
Используется стандартный формат OpenAI tools; работает с моделями GPT, Claude и Gemini.
1. Опишите инструменты
Заголовок раздела «1. Опишите инструменты»import json
tools = [{ "type": "function", "function": { "name": "get_weather", "description": "Get the current weather for a city", "parameters": { "type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"], }, },}]
messages = [{"role": "user", "content": "What's the weather in Paris?"}]r = client.chat.completions.create(model="gpt-5.6-luna", messages=messages, tools=tools)2. Выполните запрошенную функцию
Заголовок раздела «2. Выполните запрошенную функцию»Когда модель решает вызвать инструмент, finish_reason равен tool_calls:
call = r.choices[0].message.tool_calls[0]print(call.function.name, call.function.arguments)# get_weather {"city":"Paris"}
args = json.loads(call.function.arguments)result = {"temp_c": 18, "sky": "clear"} # your real implementation here3. Отправьте результат обратно
Заголовок раздела «3. Отправьте результат обратно»messages.append(r.choices[0].message)messages.append({ "role": "tool", "tool_call_id": call.id, "content": json.dumps(result),})
final = client.chat.completions.create(model="gpt-5.6-luna", messages=messages, tools=tools)print(final.choices[0].message.content)# Paris is currently 18°C with clear skies.Параметры
Заголовок раздела «Параметры»| Параметр | Действие |
|---|---|
tool_choice: "auto" |
По умолчанию. Модель решает сама. |
tool_choice: "none" |
Никогда не вызывать инструменты. |
tool_choice: "required" |
Обязательно вызвать хотя бы один инструмент. |
tool_choice: {"type":"function","function":{"name":"get_weather"}} |
Принудительно вызвать конкретный инструмент. |
parallel_tool_calls: false |
Не более одного вызова инструмента за ход. |
Модель может вернуть несколько элементов в tool_calls; ответьте на каждый
отдельным сообщением tool с соответствующим tool_call_id.