1*cfd76f4aSAndreas Gohr<?php 2*cfd76f4aSAndreas Gohr 3*cfd76f4aSAndreas Gohrnamespace dokuwiki\plugin\aichat\Model\Mistral; 4*cfd76f4aSAndreas Gohr 5*cfd76f4aSAndreas Gohruse dokuwiki\plugin\aichat\Model\AbstractModel; 6*cfd76f4aSAndreas Gohr 7*cfd76f4aSAndreas Gohr/** 8*cfd76f4aSAndreas Gohr * Abstract OpenAI Model 9*cfd76f4aSAndreas Gohr * 10*cfd76f4aSAndreas Gohr * This class provides a basic interface to the OpenAI API 11*cfd76f4aSAndreas Gohr */ 12*cfd76f4aSAndreas Gohrabstract class AbstractMistralModel extends AbstractModel 13*cfd76f4aSAndreas Gohr{ 14*cfd76f4aSAndreas Gohr /** @inheritdoc */ 15*cfd76f4aSAndreas Gohr public function __construct(string $name, array $config) 16*cfd76f4aSAndreas Gohr { 17*cfd76f4aSAndreas Gohr parent::__construct($name, $config); 18*cfd76f4aSAndreas Gohr $this->http->headers['Authorization'] = 'Bearer ' . $config['mistral_apikey'] ?? ''; 19*cfd76f4aSAndreas Gohr } 20*cfd76f4aSAndreas Gohr 21*cfd76f4aSAndreas Gohr /** 22*cfd76f4aSAndreas Gohr * Send a request to the OpenAI API 23*cfd76f4aSAndreas Gohr * 24*cfd76f4aSAndreas Gohr * @param string $endpoint 25*cfd76f4aSAndreas Gohr * @param array $data Payload to send 26*cfd76f4aSAndreas Gohr * @return array API response 27*cfd76f4aSAndreas Gohr * @throws \Exception 28*cfd76f4aSAndreas Gohr */ 29*cfd76f4aSAndreas Gohr protected function request($endpoint, $data) 30*cfd76f4aSAndreas Gohr { 31*cfd76f4aSAndreas Gohr $url = 'https://api.mistral.ai/v1/' . $endpoint; 32*cfd76f4aSAndreas Gohr return $this->sendAPIRequest('POST', $url, $data); 33*cfd76f4aSAndreas Gohr } 34*cfd76f4aSAndreas Gohr 35*cfd76f4aSAndreas Gohr /** @inheritdoc */ 36*cfd76f4aSAndreas Gohr protected function parseAPIResponse($response) 37*cfd76f4aSAndreas Gohr { 38*cfd76f4aSAndreas Gohr if (isset($response['usage'])) { 39*cfd76f4aSAndreas Gohr $this->inputTokensUsed += $response['usage']['prompt_tokens']; 40*cfd76f4aSAndreas Gohr $this->outputTokensUsed += $response['usage']['completion_tokens'] ?? 0; 41*cfd76f4aSAndreas Gohr } 42*cfd76f4aSAndreas Gohr 43*cfd76f4aSAndreas Gohr if (isset($response['error'])) { 44*cfd76f4aSAndreas Gohr throw new \Exception('Mistral API error: ' . $response['error']['message']); 45*cfd76f4aSAndreas Gohr } 46*cfd76f4aSAndreas Gohr 47*cfd76f4aSAndreas Gohr return $response; 48*cfd76f4aSAndreas Gohr } 49*cfd76f4aSAndreas Gohr 50*cfd76f4aSAndreas Gohr /** 51*cfd76f4aSAndreas Gohr * @internal for checking available models 52*cfd76f4aSAndreas Gohr */ 53*cfd76f4aSAndreas Gohr public function listUpstreamModels() 54*cfd76f4aSAndreas Gohr { 55*cfd76f4aSAndreas Gohr $url = 'https://api.openai.com/v1/models'; 56*cfd76f4aSAndreas Gohr return $this->http->get($url); 57*cfd76f4aSAndreas Gohr } 58*cfd76f4aSAndreas Gohr} 59