$reasoning ? 'o4-mini' : 'gpt-4o-mini', 'messages' => array_map(function (Message $message) { return $message->toArray(); }, $messages->toArray()), ]; if ($tools->count() > 0) { $payload['tool_choice'] = $forceToolCalls ? 'required' : 'auto'; $payload['tools'] = []; /** @var ToolInterface $tool */ foreach ($tools->toArray() as $tool) { $payload['tools'][] = [ 'type' => 'function', 'function' => [ 'name' => $tool->getName(), 'description' => $tool->getDescription(), 'parameters' => [ 'type' => 'object', 'properties' => $tool->getArguments(), 'required' => $tool->getRequiredArguments(), ], ], ]; } } try { /** @var ResponseInterface $response */ $response = $this->httpClient->request('POST', "https://api.openai.com/v1/chat/completions", [ 'headers' => [ 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $this->openAiApiKey, ], 'json' => $payload, 'timeout' => 60 * 10, 'max_duration' => 60 * 10, ]); $responseArray = $response->toArray(false); if (isset($responseArray['choices']) && is_array($responseArray['choices']) && count($responseArray['choices']) > 0) { $choices = []; foreach ($responseArray['choices'] as $choice) { $choices[] = Choice::fromArray($choice); } return new ChatResult( choices: $choices, messageHistory: $messages->toArray(), ); } throw new Exception('Error: ' . $response->getContent(false)); } catch (ClientExceptionInterface | DecodingExceptionInterface | RedirectionExceptionInterface | ServerExceptionInterface | TransportExceptionInterface $e) { throw new Exception('API Error: ' . $e->getMessage(), 0, $e); } } }