You can define custom functions as tools in a response request. When the model decides to use a tool, the response will contain a function_call item. You must then execute your local code with the provided arguments.
$response = $client->responses()->create([
'model' => 'gpt-4o-mini',
'tools' => [
[
'type' => 'function',
'name' => 'get_temperature',
'description' => 'Get the current temperature 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'],
],
]
],
'input' => "What is the temperature in Rio Grande do Norte, Brazil?",
]);
foreach ($response->output as $item) {
if ($item->type === 'function_call') {
$name = $item->name ?? null;
$args = json_decode($item->arguments ?? '{}', true) ?: [];
if ($name === 'get_temperature') {
// ✅ Call your custom function here with $args
}
}
}