config['model'] ?? 'gpt-4o-mini'; try { $response = Http::withToken($provider->api_key) ->post('https://api.openai.com/v1/chat/completions', [ 'model' => $model, 'messages' => [ ['role' => 'user', 'content' => $prompt], ], ]); } catch (ConnectionException $e) { throw new AiGenerationException("could not connect to OpenAI: {$e->getMessage()}"); } if ($response->failed()) { throw new AiGenerationException($response->json('error.message') ?? 'unexpected response from OpenAI'); } $content = $response->json('choices.0.message.content'); if (! $content) { throw new AiGenerationException('unexpected response from OpenAI: missing message content'); } return new AiGenerationResult($content, 'text/plain', $response->json()); } }