DocumentazioneStrumenti
Sviluppa
Strumenti
Descrivi le tue funzioni al modello. Decide lui quando chiamarne una, il tuo codice la esegue e gli restituisce il risultato.
In questa pagina
Il ciclo completo
Descrivi la funzione
Il suo nome, cosa fa e i suoi parametri, in JSON Schema.
Il modello richiede la chiamata
Invece di un testo, restituisce tool_calls con gli argomenti.
Il tuo codice la esegue
Il modello non esegue nulla da sé. Mantieni il controllo su ciò che accade.
Il modello risponde
Con il risultato rinviato in un messaggio tool, scrive la sua risposta.
Esempio
Una funzione che dice se una fattura è pagata. La risposta mostrata è quella della prima chiamata, quando il modello richiede la funzione.
import json
tools = [
{
"type": "function",
"function": {
"name": "invoice_status",
"description": "Payment status of an invoice.",
"parameters": {
"type": "object",
"properties": {"number": {"type": "string"}},
"required": ["number"],
},
},
}
]
messages = [
{
"role": "user",
"content": "Has invoice 2026-114 been paid?",
}
]
first = client.chat.completions.create(
model="flash", messages=messages, tools=tools
)
call = first.choices[0].message.tool_calls[0]
args = json.loads(call.function.arguments)
# Run the function, then return its result with the call id.
result = {
"number": args["number"],
"status": "paid",
"paid_on": "2026-10-02",
}
messages.append(first.choices[0].message)
messages.append(
{
"role": "tool",
"tool_call_id": call.id,
"content": json.dumps(result),
}
)
final = client.chat.completions.create(
model="flash", messages=messages, tools=tools
)
print(final.choices[0].message.content)const tools = [
{
type: "function",
function: {
name: "invoice_status",
description: "Payment status of an invoice.",
parameters: {
type: "object",
properties: { number: { type: "string" } },
required: ["number"],
},
},
},
]
const messages = [
{ role: "user", content: "Has invoice 2026-114 been paid?" },
]
const first = await client.chat.completions.create({
model: "flash",
messages,
tools,
})
const call = first.choices[0].message.tool_calls[0]
const args = JSON.parse(call.function.arguments)
// Run the function, then return its result with the call id.
const result = {
number: args.number,
status: "paid",
paid_on: "2026-10-02",
}
messages.push(first.choices[0].message)
messages.push({
role: "tool",
tool_call_id: call.id,
content: JSON.stringify(result),
})
const final = await client.chat.completions.create({
model: "flash",
messages,
tools,
})
console.log(final.choices[0].message.content)import type {
ChatCompletionMessageParam,
ChatCompletionTool,
} from "openai/resources"
const tools: ChatCompletionTool[] = [
{
type: "function",
function: {
name: "invoice_status",
description: "Payment status of an invoice.",
parameters: {
type: "object",
properties: { number: { type: "string" } },
required: ["number"],
},
},
},
]
const messages: ChatCompletionMessageParam[] = [
{ role: "user", content: "Has invoice 2026-114 been paid?" },
]
const first = await client.chat.completions.create({
model: "flash",
messages,
tools,
})
const call = first.choices[0].message.tool_calls?.[0]
if (call?.type !== "function")
throw new Error("no function call")
const args = JSON.parse(call.function.arguments) as {
number: string
}
// Run the function, then return its result with the call id.
const result = {
number: args.number,
status: "paid",
paid_on: "2026-10-02",
}
messages.push(first.choices[0].message)
messages.push({
role: "tool",
tool_call_id: call.id,
content: JSON.stringify(result),
})
const final = await client.chat.completions.create({
model: "flash",
messages,
tools,
})
console.log(final.choices[0].message.content)Risposta
{
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_7f3a",
"type": "function",
"function": { "name": "invoice_status", "arguments": "{\"number\": \"2026-114\"}" }
}
]
}Buono a sapersi
- Un elenco di strumenti vuoto viene rimosso insieme a tool_choice, come se non ne avessi inviati.
- parallel_tool_calls consente al modello di richiedere più funzioni alla volta.
- Verifica gli argomenti prima di agire. Il modello può sbagliare, decide il tuo codice.