Mit Function Calling kann ein Modell strukturierte Tool-Aufrufe auswählen, die deine Anwendung ausführt. Das Modell führt die Funktion nicht selbst aus. Es gibt den Funktionsnamen und die Argumente zurück, dein Code führt die Funktion aus und du sendest das Ergebnis an das Modell zurück.
Verwende Function Calling, wenn das Modell Live-Daten, Anwendungsaktionen, Datenbankabfragen oder deterministische Berechnungen benötigt.
Definiere Tools mit dem OpenAI-kompatiblen tools-Array:
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["VENICE_API_KEY"],
base_url="https://api.venice.ai/api/v1",
)
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather in a location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City and state, such as San Francisco, CA",
}
},
"required": ["location"],
},
},
}
]
response = client.chat.completions.create(
model="zai-org-glm-5",
messages=[{"role": "user", "content": "What is the weather in San Francisco?"}],
tools=tools,
)
print(response.choices[0].message.tool_calls)
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.VENICE_API_KEY,
baseURL: "https://api.venice.ai/api/v1",
});
const tools = [
{
type: "function",
function: {
name: "get_weather",
description: "Get the current weather in a location",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "City and state, such as San Francisco, CA",
},
},
required: ["location"],
},
},
},
];
const response = await client.chat.completions.create({
model: "zai-org-glm-5",
messages: [{ role: "user", content: "What is the weather in San Francisco?" }],
tools,
});
console.log(response.choices[0].message.tool_calls);
curl https://api.venice.ai/api/v1/chat/completions \
-H "Authorization: Bearer $VENICE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "zai-org-glm-5",
"messages": [
{"role": "user", "content": "What is the weather in San Francisco?"}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather in a location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City and state, such as San Francisco, CA"
}
},
"required": ["location"]
}
}
}
]
}'
Wenn das Modell ein Tool auswählt, prüfe message.tool_calls, parse die Argumente, führe deine Anwendungsfunktion aus und sende das Ergebnis als tool-Nachricht zurück.
import json
message = response.choices[0].message
tool_call = message.tool_calls[0]
arguments = json.loads(tool_call.function.arguments)
weather = get_weather(arguments["location"])
follow_up = client.chat.completions.create(
model="zai-org-glm-5",
messages=[
{"role": "user", "content": "What is the weather in San Francisco?"},
message.model_dump(),
{
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(weather),
},
],
tools=tools,
)
print(follow_up.choices[0].message.content)
Ein Modell auswählen
Die Unterstützung für Function Calling ist modellabhängig. Nutze die Seite Textmodelle oder die Models-API, um Modelle mit supportsFunctionCalling zu finden.
Behandle Tool-Argumente als nicht vertrauenswürdige Eingaben. Validiere Argumente, bevor du sie in Datenbankabfragen, Shell-Befehlen, Zahlungen oder anderen Aktionen mit Nebenwirkungen verwendest.
Design-Tipps
- Halte Tool-Namen und Beschreibungen kurz und wörtlich.
- Verwende JSON Schema, damit das Modell gültige Argumente leichter erzeugen kann.
- Bevorzuge eng gefasste Tools mit klaren Eingaben gegenüber einem breiten Tool mit vielen optionalen Verhaltensweisen.
- Gib knappe Tool-Ergebnisse zurück, damit die finale Antwort genügend Kontext hat, ohne Tokens zu verschwenden.
Verwandte Ressourcen