DokumentationTools
Entwickeln
Tools
Beschreiben Sie dem Modell Ihre Funktionen. Es entscheidet, wann es eine aufruft, Ihr Code führt sie aus und gibt ihm das Ergebnis zurück.
Auf dieser Seite
Der vollständige Ablauf
Sie beschreiben die Funktion
Ihr Name, was sie tut, und ihre Parameter, als JSON Schema.
Das Modell fordert den Aufruf an
Statt eines Textes gibt es tool_calls mit den Argumenten zurück.
Ihr Code führt sie aus
Das Modell führt selbst nichts aus. Sie behalten die Kontrolle über das, was geschieht.
Das Modell antwortet
Mit dem Ergebnis, das Sie in einer tool-Nachricht zurückgeben, schreibt es seine Antwort.
Beispiel
Eine Funktion, die angibt, ob eine Rechnung bezahlt ist. Die gezeigte Antwort stammt vom ersten Aufruf, wenn das Modell die Funktion anfordert.
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)Antwort
{
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_7f3a",
"type": "function",
"function": { "name": "invoice_status", "arguments": "{\"number\": \"2026-114\"}" }
}
]
}Gut zu wissen
- Eine leere Tool-Liste wird zusammen mit tool_choice entfernt, als hätten Sie keine gesendet.
- parallel_tool_calls erlaubt dem Modell, mehrere Funktionen gleichzeitig anzufordern.
- Prüfen Sie die Argumente, bevor Sie handeln. Das Modell kann sich irren, Ihr Code entscheidet.