1*074b7701SAndreas Gohr<?php 2*074b7701SAndreas Gohr 3*074b7701SAndreas Gohrnamespace dokuwiki\plugin\aichat\Model\Groq; 4*074b7701SAndreas Gohr 5*074b7701SAndreas Gohruse dokuwiki\plugin\aichat\Model\AbstractModel; 6*074b7701SAndreas Gohruse dokuwiki\plugin\aichat\Model\ChatInterface; 7*074b7701SAndreas Gohr 8*074b7701SAndreas Gohrclass ChatModel extends AbstractModel implements ChatInterface 9*074b7701SAndreas Gohr{ 10*074b7701SAndreas Gohr /** @inheritdoc */ 11*074b7701SAndreas Gohr public function __construct(string $name, array $config) 12*074b7701SAndreas Gohr { 13*074b7701SAndreas Gohr parent::__construct($name, $config); 14*074b7701SAndreas Gohr 15*074b7701SAndreas Gohr if (empty($config['groq_apikey'])) { 16*074b7701SAndreas Gohr throw new \Exception('Groq API key not configured'); 17*074b7701SAndreas Gohr } 18*074b7701SAndreas Gohr 19*074b7701SAndreas Gohr $this->http->headers['Authorization'] = 'Bearer '.$config['groq_apikey']; 20*074b7701SAndreas Gohr } 21*074b7701SAndreas Gohr 22*074b7701SAndreas Gohr /** @inheritdoc */ 23*074b7701SAndreas Gohr public function getAnswer(array $messages): string 24*074b7701SAndreas Gohr { 25*074b7701SAndreas Gohr $data = [ 26*074b7701SAndreas Gohr 'messages' => $messages, 27*074b7701SAndreas Gohr 'model' => $this->getModelName(), 28*074b7701SAndreas Gohr 'max_tokens' => null, 29*074b7701SAndreas Gohr 'stream' => false, 30*074b7701SAndreas Gohr 'n' => 1, // number of completions 31*074b7701SAndreas Gohr 'temperature' => 0.0, 32*074b7701SAndreas Gohr ]; 33*074b7701SAndreas Gohr $response = $this->request('chat/completions', $data); 34*074b7701SAndreas Gohr return $response['choices'][0]['message']['content']; 35*074b7701SAndreas Gohr } 36*074b7701SAndreas Gohr 37*074b7701SAndreas Gohr /** 38*074b7701SAndreas Gohr * Send a request to the API 39*074b7701SAndreas Gohr * 40*074b7701SAndreas Gohr * @param string $endpoint 41*074b7701SAndreas Gohr * @param array $data Payload to send 42*074b7701SAndreas Gohr * @return array API response 43*074b7701SAndreas Gohr * @throws \Exception 44*074b7701SAndreas Gohr */ 45*074b7701SAndreas Gohr protected function request($endpoint, $data) 46*074b7701SAndreas Gohr { 47*074b7701SAndreas Gohr $url = 'https://api.groq.com/openai/v1/' . $endpoint; 48*074b7701SAndreas Gohr return $this->sendAPIRequest('POST', $url, $data); 49*074b7701SAndreas Gohr } 50*074b7701SAndreas Gohr 51*074b7701SAndreas Gohr /** @inheritdoc */ 52*074b7701SAndreas Gohr protected function parseAPIResponse($response) 53*074b7701SAndreas Gohr { 54*074b7701SAndreas Gohr if (isset($response['usage'])) { 55*074b7701SAndreas Gohr $this->inputTokensUsed += $response['usage']['prompt_tokens']; 56*074b7701SAndreas Gohr $this->outputTokensUsed += $response['usage']['completion_tokens'] ?? 0; 57*074b7701SAndreas Gohr } 58*074b7701SAndreas Gohr 59*074b7701SAndreas Gohr if (isset($response['error'])) { 60*074b7701SAndreas Gohr throw new \Exception('Groq API error: ' . $response['error']['message']); 61*074b7701SAndreas Gohr } 62*074b7701SAndreas Gohr 63*074b7701SAndreas Gohr return $response; 64*074b7701SAndreas Gohr } 65*074b7701SAndreas Gohr} 66