Mit Funktionsaufrufen können Sie benutzerdefinierte Funktionen definieren und LLMs die Möglichkeit geben, sie aufzurufen, um Echtzeitinformationen abzurufen oder mit externen Systemen wie SQL-Datenbanken oder Kundenservicetools zu interagieren.
Weitere konzeptionelle Informationen zu Funktionsaufrufen finden Sie unter Einführung in Funktionsaufrufe.
Funktionsaufrufe verwenden
Die folgenden Beispiele zeigen, wie Sie Funktionsaufrufe verwenden.
Python
Folgen Sie der Python Einrichtungsanleitung in der Kurzanleitung zur Agent Platform mit Clientbibliotheken, bevor Sie dieses Beispiel anwenden. Weitere Informationen finden Sie in der API-Referenzdokumentation für die Agent PlatformPython.
Richten Sie die Standardanmeldedaten für Anwendungen ein, um sich bei der Agent Platform zu authentifizieren. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.
Legen Sie vor dem Ausführen dieses Beispiels die Umgebungsvariable OPENAI_BASE_URL fest.
Weitere Informationen finden Sie unter Authentifizierung und Anmeldedaten.
from openai import OpenAI client = OpenAI() response = client.chat.completions.create( model="MODEL", messages=[ {"role": "user", "content": "CONTENT"} ], tools=[ { "type": "function", "function": { "name": "FUNCTION_NAME", "description": "FUNCTION_DESCRIPTION", "parameters": PARAMETERS_OBJECT, } } ], tool_choice="auto", )
- MODEL: Der Modellname, den Sie verwenden möchten,
z. B.
qwen/qwen3-next-80b-a3b-instruct-maas. - CONTENT: Der Nutzer-Prompt, der an das Modell gesendet werden soll.
- FUNCTION_NAME: Der Name der aufzurufenden Funktion.
- FUNCTION_DESCRIPTION: Eine Beschreibung der Funktion.
- PARAMETERS_OBJECT: Ein Dictionary, das die Funktionsparameter definiert, z. B.:
{"type": "object", "properties": {"location": {"type": "string", "description": "The city and state"}}, "required": ["location"]}
REST
Nachdem Sie Ihre Umgebung eingerichtet haben, können Sie mit REST einen Text-Prompt testen. Im folgenden Beispiel wird eine Anfrage an den Publisher gesendet Modellendpunkt zu erstellen.
Ersetzen Sie diese Werte in den folgenden Anfragedaten:
- PROJECT_ID: Ihre Google Cloud-Projekt-ID.
- LOCATION: Eine Region, die offene Modelle unterstützt.
- MODEL: Der Modellname, den Sie verwenden möchten,
z. B.
qwen/qwen3-next-80b-a3b-instruct-maas. - CONTENT: Der Nutzer-Prompt, der an das Modell gesendet werden soll.
- FUNCTION_NAME: Der Name der aufzurufenden Funktion.
- FUNCTION_DESCRIPTION: Eine Beschreibung der Funktion.
- PARAMETERS_OBJECT: Ein JSON-Schemaobjekt, das die Funktionsparameter definiert, z. B.:
{"type": "object", "properties": {"location": {"type": "string", "description": "The city and state"}}, "required": ["location"]}
HTTP-Methode und URL:
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/endpoints/openapi/chat/completions
JSON-Text der Anfrage:
{
"model": "MODEL",
"messages": [
{
"role": "user",
"content": "CONTENT"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "FUNCTION_NAME",
"description": "FUNCTION_DESCRIPTION",
"parameters": PARAMETERS_OBJECT
}
}
],
"tool_choice": "auto"
}
Wenn Sie die Anfrage senden möchten, wählen Sie eine der folgenden Optionen aus:
curl
Speichern Sie den Anfragetext in einer Datei mit dem Namen request.json und führen Sie den folgenden Befehl aus:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/endpoints/openapi/chat/completions"
PowerShell
Speichern Sie den Anfragetext in einer Datei mit dem Namen request.json und führen Sie den folgenden Befehl aus:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/endpoints/openapi/chat/completions" | Select-Object -Expand Content
Sie sollten einen erfolgreichen Statuscode (2xx) und eine leere Antwort als Ausgabe erhalten.
Beispiel
Im Folgenden sehen Sie die vollständige Ausgabe, die Sie nach Verwendung der Funktion get_current_weather zum Abrufen meteorologischer Informationen erwarten können.
Python
from openai import OpenAI client = OpenAI() response = client.chat.completions.create( model="qwen/qwen3-next-80b-a3b-instruct-maas", messages=[ { "role": "user", "content": "Which city has a higher temperature, Boston or new Delhi and by how much in F?" }, { "role": "assistant", "content": "I'll check the current temperatures for Boston and New Delhi in Fahrenheit and compare them. I'll call the weather function for both cities.", "tool_calls": [{"function":{"arguments":"{\"location\":\"Boston, MA\",\"unit\":\"fahrenheit\"}","name":"get_current_weather"},"id":"get_current_weather","type":"function"},{"function":{"arguments":"{\"location\":\"New Delhi, India\",\"unit\":\"fahrenheit\"}","name":"get_current_weather"},"id":"get_current_weather","type":"function"}] }, { "role": "tool", "content": "The temperature in Boston is 75 degrees Fahrenheit.", "tool_call_id": "get_current_weather" }, { "role": "tool", "content": "The temperature in New Delhi is 50 degrees Fahrenheit.", "tool_call_id": "get_current_weather" } ], tools=[ { "type": "function", "function": { "name": "get_current_weather", "description": "Get the current weather in a given location", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA" }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"] } }, "required": ["location"] } } } ], tool_choice="auto" )
curl
curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://us-central1-aiplatform.googleapis.com/v1/projects/sample-project/locations/us-central1/endpoints/openapi/chat/completions -d \ '{ "model": "qwen/qwen3-next-80b-a3b-instruct-maas", "messages": [ { "role": "user", "content": "Which city has a higher temperature, Boston or new Delhi and by how much in F?" }, { "role": "assistant", "content": "I'll check the current temperatures for Boston and New Delhi in Fahrenheit and compare them. I'll call the weather function for both cities.", "tool_calls": [{"function":{"arguments":"{\"location\":\"Boston, MA\",\"unit\":\"fahrenheit\"}","name":"get_current_weather"},"id":"get_current_weather","type":"function"},{"function":{"arguments":"{\"location\":\"New Delhi, India\",\"unit\":\"fahrenheit\"}","name":"get_current_weather"},"id":"get_current_weather","type":"function"}] }, { "role": "tool", "content": "The temperature in Boston is 75 degrees Fahrenheit.", "tool_call_id": "get_current_weather" }, { "role": "tool", "content": "The temperature in New Delhi is 50 degrees Fahrenheit.", "tool_call_id": "get_current_weather" } ], "tools": [ { "type": "function", "function": { "name": "get_current_weather", "description": "Get the current weather in a given location", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA" }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"] } }, "required": ["location"] } } } ], "tool_choice": "auto" }'
{ "choices": [ { "finish_reason": "stop", "index": 0, "logprobs": null, "message": { "content": "Based on the current weather data:\n\n- **Boston, MA**: 75°F \n- **New Delhi, India**: 50°F \n\n**Comparison**: \nBoston is **25°F warmer** than New Delhi. \n\n**Answer**: \nBoston has a higher temperature than New Delhi by 25 degrees Fahrenheit.", "role": "assistant" } } ], "created": 1750450289, "id": "2025-06-20|13:11:29.240295-07|6.230.75.101|-987540014", "model": "qwen/qwen3-next-80b-a3b-instruct-maas", "object": "chat.completion", "system_fingerprint": "", "usage": { "completion_tokens": 66, "prompt_tokens": 217, "total_tokens": 283 } }
Modellspezifische Anleitung
Die folgenden Abschnitte enthalten modellspezifische Anleitungen für Funktionsaufrufe.
DeepSeek
DeepSeek-Modelle funktionieren bei Funktionsaufrufen nicht so gut, wenn Sie einen System-Prompt verwenden. Für eine optimale Leistung sollten Sie den System-Prompt weglassen, wenn Sie Funktionsaufrufe mit DeepSeek-Modellen verwenden.
Llama
meta/llama3-405b-instruct-maas unterstützt tool_choice = 'required' nicht.
OpenAI
Wenn Sie openai/gpt-oss-120b-instruct-maas und openai/gpt-oss-20b-instruct-maas verwenden, platzieren Sie Ihre Tool-Definitionen im System-Prompt, um eine optimale Leistung zu erzielen. Beispiel:
{"messages": [ {"role": "system", "content": "You are a helpful assistant with access to the following functions. Use them if required:\n..."}, {"role": "user", "content": "What's the weather like in Boston?"}, ... ]}
Diese Modelle unterstützen tool_choice = 'required' oder benannte Tool-Aufrufe nicht.
Qwen
Qwen-Modelle funktionieren am besten, wenn tool_choice explizit auf auto oder none gesetzt ist. Wenn tool_choice nicht festgelegt ist, funktioniert das Modell möglicherweise nicht so gut.
Nächste Schritte
- Weitere Informationen zur strukturierten Ausgabe.
- Weitere Informationen zu Thinking.
- Weitere Informationen zu Batchvorhersagen.