Skip to content

Function calling / Tool use

Шлюз поддерживает OpenAI-совместимый формат tools для всех моделей, у которых это умеет upstream (Anthropic, OpenAI, Google, xAI, Mistral, DeepSeek).

Пример

python
from openai import OpenAI

client = OpenAI(base_url="https://api.example.com/v1", api_key="sk-llmgw-...")

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Возвращает погоду в городе",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {"type": "string"},
                    "unit": {"type": "string", "enum": ["c", "f"], "default": "c"},
                },
                "required": ["city"],
            },
        },
    }
]

resp = client.chat.completions.create(
    model="anthropic/claude-sonnet-4.6",
    messages=[{"role": "user", "content": "Какая погода в Москве?"}],
    tools=tools,
    tool_choice="auto",
)

call = resp.choices[0].message.tool_calls[0]
print(call.function.name, call.function.arguments)

Как работает

  • Шлюз транслирует tools в нативный формат провайдера (tools для Anthropic Messages API, function_declarations для Gemini и т. д.).
  • В ответе вы получаете унифицированный OpenAI-формат tool_calls.
  • tool_choice поддерживает auto, none, required и явный выбор тулзы.

Параллельные вызовы

Если модель умеет (Claude 3.5+, GPT-4o+, Gemini 1.5+), шлюз вернёт массив tool_calls с несколькими элементами. Их можно обработать параллельно и вернуть результаты сообщениями role: "tool".

Структурированный вывод

Для жёстких JSON-ответов используйте response_format:

python
resp = client.chat.completions.create(
    model="openai/gpt-5",
    messages=[...],
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "extraction",
            "strict": True,
            "schema": {
                "type": "object",
                "properties": {"name": {"type": "string"}, "age": {"type": "integer"}},
                "required": ["name", "age"],
                "additionalProperties": False,
            },
        },
    },
)

© llmgw