http->headers['Authorization'] = 'Bearer ' . $openAIKey; if ($openAIOrg) { $this->http->headers['OpenAI-Organization'] = $openAIOrg; } } /** * Send a request to the OpenAI API * * @param string $endpoint * @param array $data Payload to send * @return array API response * @throws \Exception */ protected function request($endpoint, $data) { $url = 'https://api.openai.com/v1/' . $endpoint; return $this->sendAPIRequest('POST', $url, $data); } /** @inheritdoc */ protected function parseAPIResponse($response) { if (isset($response['usage'])) { $this->inputTokensUsed += $response['usage']['prompt_tokens']; $this->outputTokensUsed += $response['usage']['completion_tokens'] ?? 0; } if (isset($response['error'])) { throw new \Exception('OpenAI API error: ' . $response['error']['message']); } return $response; } /** * @internal for checking available models */ public function listUpstreamModels() { $url = 'https://api.openai.com/v1/models'; return $this->http->get($url); } }