Перейти к основному содержанию

Documentation Index

Fetch the complete documentation index at: https://docs.crazyrouter.com/llms.txt

Use this file to discover all available pages before exploring further.

Overview

Function Calling allows models to call functions you define, enabling interaction with external systems. The model does not execute functions directly — it returns the function name and arguments, and your code executes them and returns the results to the model.

Complete Example

import json
from openai import OpenAI

client = OpenAI(api_key="sk-xxx", base_url="https://crazyrouter.com/v1")

# 1. Define tools
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get weather information for a specified city",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {
                        "type": "string",
                        "description": "City name, e.g.: New York"
                    },
                    "unit": {
                        "type": "string",
                        "enum": ["celsius", "fahrenheit"],
                        "description": "Temperature unit"
                    }
                },
                "required": ["city"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "search_products",
            "description": "Search for products",
            "parameters": {
                "type": "object",
                "properties": {
                    "query": {"type": "string", "description": "Search keyword"},
                    "max_price": {"type": "number", "description": "Maximum price"}
                },
                "required": ["query"]
            }
        }
    }
]

# 2. Mock function implementations
def get_weather(city, unit="celsius"):
    return {"city": city, "temperature": 22, "unit": unit, "condition": "Sunny"}

def search_products(query, max_price=None):
    return [{"name": f"{query} Product A", "price": 99}, {"name": f"{query} Product B", "price": 199}]

# 3. Conversation loop
messages = [{"role": "user", "content": "What's the weather like in New York today? Also search for umbrellas"}]

response = client.chat.completions.create(
    model="gpt-5.4",
    messages=messages,
    tools=tools,
    tool_choice="auto"
)

message = response.choices[0].message

# 4. Handle tool calls
if message.tool_calls:
    messages.append(message)

    for tool_call in message.tool_calls:
        func_name = tool_call.function.name
        args = json.loads(tool_call.function.arguments)

        if func_name == "get_weather":
            result = get_weather(**args)
        elif func_name == "search_products":
            result = search_products(**args)

        messages.append({
            "role": "tool",
            "tool_call_id": tool_call.id,
            "content": json.dumps(result, ensure_ascii=False)
        })

    # 5. Get final response
    final = client.chat.completions.create(
        model="gpt-5.4",
        messages=messages,
        tools=tools
    )
    print(final.choices[0].message.content)

Force a Specific Function Call

response = client.chat.completions.create(
    model="gpt-5.4",
    messages=messages,
    tools=tools,
    tool_choice={"type": "function", "function": {"name": "get_weather"}}
)

Supported Models

For the OpenAI-compatible Function Calling examples in this page, use the following verified baseline first:
  • gpt-5.4
Tool calling with gpt-5.4 was verified in Crazyrouter production on March 23, 2026. For other vendors, check their dedicated pages first because request formats and support details can differ.