Preskok na glavno vsebino
Slovenščina— Izberite jezik
Meni

Definiranje orodij v JSON

Lekcija 4 od 14 Posodobljeno 29. julij 2026 Razvijalec
Na tej strani
  1. Od teorije do prakse
  2. Struktura zahtevka z orodji
  3. Ključne številke pri implementaciji
  4. Obdelava odgovora tool_call
  5. Izvedba in vrnitev rezultata
  6. Definiranje več orodij
  7. Preveri finish_reason
  8. Ključne ugotovitve

Od teorije do prakse

Zdaj razumeš koncept klicanja funkcij in strukturo shem JSON. Čas je za konkretno implementacijo. V tej lekciji se boš naučil, kako zgraditi popoln zahtevek z definicijami orodij in kako obdelati odgovor modela.

Struktura zahtevka z orodji

Tukaj je popoln zahtevek API-ju Grok z definiranim orodjem:

from openai import OpenAI

client = OpenAI(
    api_key="your-api-key",
    base_url="https://api.x.ai/v1"
)

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get the current weather for a city",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {
                        "type": "string",
                        "description": "City name"
                    },
                    "unit": {
                        "type": "string",
                        "enum": ["celsius", "fahrenheit"],
                        "default": "celsius"
                    }
                },
                "required": ["city"]
            }
        }
    }
]

response = client.chat.completions.create(
    model="grok-4.5",
    messages=[
        {"role": "user", "content": "What's the weather like in Paris?"}
    ],
    tools=tools
)

Opazi gnezdeno strukturo: predmet tools vsebuje polje, kjer ima vsak element type in predmet function s podrobnostmi.

Ključne številke pri implementaciji

200
Največ orodij na zahtevek
6
Podprti tipi JSON
3
Združljivi SDK-ji
1
Zahtevek = celoten potek

Obdelava odgovora tool_call

Ko se model odloči poklicati funkcijo, odgovor vsebuje polje tool_calls:

message = response.choices[0].message

if message.tool_calls:
    for tool_call in message.tool_calls:
        print(f"Function: {tool_call.function.name}")
        print(f"Arguments: {tool_call.function.arguments}")
        print(f"ID: {tool_call.id}")

Polje tool_call.function.arguments je niz JSON (ne predmet Python). Razčleniti ga moraš:

import json

args = json.loads(tool_call.function.arguments)
city = args["city"]
unit = args.get("unit", "celsius")

Izvedba in vrnitev rezultata

Ko je funkcija izvedena, rezultat pošlješ nazaj modelu v drugem klicu:

import json

# 1. Izvedi funkcijo
weather_data = get_weather(city="Paris", unit="celsius")

# 2. Zgradi sporočila z rezultatom
messages = [
    {"role": "user", "content": "What's the weather like in Paris?"},
    message,  # Odgovor modela z tool_calls
    {
        "role": "tool",
        "tool_call_id": tool_call.id,
        "content": json.dumps(weather_data)
    }
]

# 3. Ponovno pokliči model z rezultatom
final_response = client.chat.completions.create(
    model="grok-4.5",
    messages=messages,
    tools=tools
)

print(final_response.choices[0].message.content)

Sporočilo z vlogo tool mora vsebovati:

  • tool_call_id: edinstveni identifikator tool_call (za povezavo z zahtevkom)
  • content: rezultat kot niz JSON

Definiranje več orodij

V polju tools lahko deklariraš poljubno število orodij:

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get the current weather for a city",
            "parameters": { ... }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "search_restaurants",
            "description": "Search for restaurants by city and cuisine type",
            "parameters": { ... }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "book_table",
            "description": "Book a table at a restaurant",
            "parameters": { ... }
        }
    }
]

Model bo samodejno izbral ustrezno orodje (ali orodja) glede na uporabnikovo zahtevo.

Preveri finish_reason

Polje finish_reason v odgovoru kaže, ali je model zaključil svoj odgovor ali čaka na rezultat orodja:

finish_reason = response.choices[0].finish_reason

if finish_reason == "tool_calls":
    # Model čaka na rezultat enega ali več orodij
    handle_tool_calls(response)
elif finish_reason == "stop":
    # Model je zaključil svoj odgovor
    print(response.choices[0].message.content)

Ključne ugotovitve

  • Zahtevek vsebuje messages in tools (definicije)
  • Argumenti tool_call so niz JSON, ki ga je treba razčleniti
  • Rezultat pošlji v sporočilu z vlogo tool, ki vključuje tool_call_id
  • Preveri finish_reason, da ugotoviš, ali model čaka na rezultat
  • Deklariraš lahko več orodij – model samodejno izbere pravo