Zum Inhalt springen
API-Key API-Key

Function Calling

Function Calling ermöglicht es Modellen, strukturierte JSON-Argumente für von Ihnen definierte Funktionen zu generieren und so die Integration mit externen Tools und APIs zu ermöglichen.

Was Sie lernen werden:

  • Wie Sie Tools/Funktionen definieren, die das Modell aufrufen kann
  • Wie Sie Function Calling mit der Responses API verwenden
  • Wo Sie strukturierte Ausgaben mit JSON Schema finden (Strukturierte Ausgaben)
Python
from openai import OpenAI
client = OpenAI()
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"format": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The temperature unit to use.",
},
},
"required": ["location", "format"],
},
},
},
]
messages = [
{"role": "system", "content": "Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous."},
{"role": "user", "content": "What's the weather like today in Hamburg?"},
]
response = client.chat.completions.create(
model="GLM-5.3",
messages=messages,
tools=tools,
)
print(response.choices[0].message)

Beispielausgabe (illustrativ):

ChatCompletionMessage(
content=None,
role='assistant',
tool_calls=[
ChatCompletionMessageToolCall(
id='call_adnFRLazqswLI1ky6FU2O40u',
function=Function(
arguments='{"location":"Hamburg","format":"celsius"}',
name='get_current_weather'
),
type='function'
)
]
)
Python
from openai import OpenAI
client = OpenAI()
tools = [
{
"type": "function",
"name": "get_weather",
"description": "Get current temperature for a given location.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City and country e.g. Bogota, Colombia",
}
},
"required": ["location"],
"additionalProperties": False,
},
}
]
response = client.responses.create(
model="gpt-oss-120b",
input=[{"role": "user", "content": "What is the weather like in Paris today?"}],
tools=tools,
)
print(response.output)

JSON Schema auf der Responses API steht unter Strukturierte Ausgaben.