API verwenden
Tool-Aufrufe
Sie beschreiben Ihre Funktionen, das Modell entscheidet, wann und mit welchen Argumenten es sie aufruft, und Ihr Code führt sie aus. Alle Modelle unterstützen das, in allen drei API-Formaten.
Der Ablauf
- Senden Sie
tools: jedes mitname,descriptionund seinenparametersals JSON Schema. - Will das Modell ein Tool nutzen, enthält die Antwort
finish_reason: "tool_calls"und eine Listetool_calls, jeder Aufruf mit eineridund seinen Argumenten als JSON-String. - Fügen Sie die Assistant-Nachricht unverändert zur Unterhaltung hinzu, danach pro Aufruf eine Nachricht mit
role: "tool"und der zugehörigentool_call_id. - Senden Sie die Unterhaltung erneut. Das Modell liest die Ergebnisse und antwortet oder fordert weitere Aufrufe an.
import json
import os
from openai import OpenAI
client = OpenAI(base_url="https://api.libremodel.ai/v1", api_key=os.environ["LIBREMODEL_API_KEY"])
tools = [{
"type": "function",
"function": {
"name": "get_order_status",
"description": "Status of an order, by its id.",
"parameters": {
"type": "object",
"properties": {"order_id": {"type": "string"}},
"required": ["order_id"],
},
},
}]
messages = [{"role": "user", "content": "Where is order A-1042?"}]
first = client.chat.completions.create(model="deepseek-v4.1-flash-1017", messages=messages, tools=tools)
reply = first.choices[0].message
# 1. Keep the assistant turn exactly as it came back, thinking included.
messages.append(reply)
# 2. Run each call and answer it with its tool_call_id.
for call in reply.tool_calls or []:
args = json.loads(call.function.arguments)
result = {"order_id": args["order_id"], "status": "shipped", "eta": "Thursday"}
messages.append({"role": "tool", "tool_call_id": call.id, "content": json.dumps(result)})
# 3. Ask again: now the model answers with what your tool returned.
final = client.chat.completions.create(model="deepseek-v4.1-flash-1017", messages=messages, tools=tools)
print(final.choices[0].message.content)import OpenAI from 'openai'
const client = new OpenAI({ baseURL: 'https://api.libremodel.ai/v1', apiKey: process.env.LIBREMODEL_API_KEY })
const tools: OpenAI.ChatCompletionTool[] = [
{
type: 'function',
function: {
name: 'get_order_status',
description: 'Status of an order, by its id.',
parameters: { type: 'object', properties: { order_id: { type: 'string' } }, required: ['order_id'] },
},
},
]
const messages: OpenAI.ChatCompletionMessageParam[] = [{ role: 'user', content: 'Where is order A-1042?' }]
const first = await client.chat.completions.create({ model: 'deepseek-v4.1-flash-1017', messages, tools })
const reply = first.choices[0].message
// 1. Keep the assistant turn exactly as it came back, thinking included.
messages.push(reply)
// 2. Run each call and answer it with its tool_call_id.
for (const call of reply.tool_calls ?? []) {
if (call.type !== 'function') continue
const { order_id } = JSON.parse(call.function.arguments)
messages.push({ role: 'tool', tool_call_id: call.id, content: JSON.stringify({ order_id, status: 'shipped', eta: 'Thursday' }) })
}
// 3. Ask again: now the model answers with what your tool returned.
const final = await client.chat.completions.create({ model: 'deepseek-v4.1-flash-1017', messages, tools })
console.log(final.choices[0].message.content)Festlegen, welches Tool läuft
| tool_choice | Was das Modell tut |
|---|---|
"auto" (Standard) | Entscheidet selbst, ob es ein Tool aufruft oder antwortet. |
"none" | Antwortet, ohne ein Tool aufzurufen, auch wenn welche definiert sind. |
"required" | Ruft mindestens ein Tool auf. |
{"type": "function", "function": {"name": "..."}} | Ruft genau dieses Tool auf. |
"auto" und "none" funktionieren bei allen Modellen. "required" und ein benanntes Tool gehen an jedes Modell, aber nur Modelle mit tool_choice in supported_parameters (GET /v1/models) halten sie immer ein. Mit "provider": {"require_parameters": true} erhalten Sie 404 statt eines Modells, das sie womöglich nicht einhält.
Eine einzige Antwort kann mehrere Aufrufe gleichzeitig anfordern. Führen Sie sie in beliebiger Reihenfolge oder parallel aus und beantworten Sie jeden mit seiner eigenen tool_call_id.
Im Format von Anthropic
Auf /v1/messages funktioniert es genauso, mit den Formaten von Anthropic: Das Modell liefert tool_use-Blöcke, und Sie antworten mit tool_result-Blöcken in der nächsten Nutzernachricht.
Server-Tools
Zwei Tools laufen bei uns, ohne Code von Ihnen: die Websuche und der Web-Abruf, verfügbar bei Standardmodellen. Alle anderen Tools laufen in Ihrem Code.