Definiranje alata u JSON-u
Na ovoj stranici
Od teorije do prakse
Sada razumiješ koncept pozivanja funkcija i strukturu JSON shema. Vrijeme je za konkretnu implementaciju. U ovoj lekciji naučit ćeš kako izgraditi kompletan zahtjev s definicijama alata i kako obraditi odgovor modela.
Struktura zahtjeva s alatima
Evo potpunog zahtjeva Grok API-ju s definiranim alatom:
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
)
Obrati pažnju na ugniježđenu strukturu: objekt tools sadrži polje gdje svaki element ima type i objekt function s pojedinostima.
Ključne brojke implementacije
Obrada odgovora tool_call
Kada model odluči pozvati funkciju, odgovor sadrži 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 JSON niz znakova (nije Python objekt). Moraš ga parsirati:
import json
args = json.loads(tool_call.function.arguments)
city = args["city"]
unit = args.get("unit", "celsius")
Izvršavanje i vraćanje rezultata
Nakon što se funkcija izvrši, rezultat šalješ natrag modelu u drugom pozivu:
import json
# 1. Izvrši funkciju
weather_data = get_weather(city="Paris", unit="celsius")
# 2. Izgradi poruke s rezultatom
messages = [
{"role": "user", "content": "What's the weather like in Paris?"},
message, # Odgovor modela s tool_calls
{
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(weather_data)
}
]
# 3. Ponovno pozovi model s rezultatom
final_response = client.chat.completions.create(
model="grok-4.5",
messages=messages,
tools=tools
)
print(final_response.choices[0].message.content)
Poruka s ulogom tool mora sadržavati:
tool_call_id: jedinstveni identifikator tool_calla (za povezivanje sa zahtjevom)content: rezultat kao JSON niz znakova
Definiranje više alata
U polju tools možeš deklarirati onoliko alata koliko ti treba:
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 će automatski odabrati relevantne alate na temelju korisničkog zahtjeva.
Provjera finish_reason
Polje finish_reason u odgovoru označava je li model završio svoj odgovor ili čeka rezultat alata:
finish_reason = response.choices[0].finish_reason
if finish_reason == "tool_calls":
# Model čeka rezultat jednog ili više alata
handle_tool_calls(response)
elif finish_reason == "stop":
# Model je završio svoj odgovor
print(response.choices[0].message.content)
Ključne točke
- Zahtjev sadrži
messagesitools(definicije) - Argumenti tool_calla su JSON niz znakova koji se mora parsirati
- Rezultat pošalji u poruci s ulogom
tooluključujućitool_call_id - Provjeri
finish_reasonda znaš čeka li model rezultat - Možeš deklarirati više alata – model automatski bira