getFromConf($config, 'apikey'); $this->http->headers['Authorization'] = 'Bearer ' . $apiKey; if($this->apiurl === '') { $this->apiurl = $this->getFromConf($config, 'apiurl'); } $this->apiurl = rtrim($this->apiurl, '/'); } /** * 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 = $this->apiurl . '/' . $endpoint; return $this->sendAPIRequest('POST', $url, $data); } /** @inheritdoc */ protected function parseAPIResponse($response) { if (isset($response['usage'])) { if(isset($response['usage']['prompt_tokens'])) { $this->inputTokensUsed += $response['usage']['prompt_tokens']; } elseif ($response['usage']['total_tokens']) { // on embedding models, prompt_tokens is not available $this->inputTokensUsed += $response['usage']['total_tokens']; } $this->outputTokensUsed += $response['usage']['completion_tokens'] ?? 0; } if (isset($response['error'])) { throw new ModelException('API error: ' . $response['error']['message'], 3002); } return $response; } /** @inheritdoc */ public function getAnswer(array $messages): string { $data = [ 'messages' => $messages, 'model' => $this->getModelName(), 'max_completion_tokens' => null, 'stream' => false, 'n' => 1, // number of completions $data['temperature'] = 0.0 ]; $response = $this->request('chat/completions', $data); return $response['choices'][0]['message']['content']; } /** @inheritdoc */ public function getEmbedding($text): array { $data = [ 'model' => $this->getModelName(), 'input' => [$text], ]; $response = $this->request('embeddings', $data); return $response['data'][0]['embedding']; } /** * @internal for checking available models */ public function listUpstreamModels() { $url = $this->apiurl . '/models'; return $this->http->get($url); } }