xref: /plugin/aichat/Model/Mistral/AbstractMistralModel.php (revision 2e22aefbcc83da0bc9ab21a2175b8ada4ba79826)
1cfd76f4aSAndreas Gohr<?php
2cfd76f4aSAndreas Gohr
3cfd76f4aSAndreas Gohrnamespace dokuwiki\plugin\aichat\Model\Mistral;
4cfd76f4aSAndreas Gohr
5cfd76f4aSAndreas Gohruse dokuwiki\plugin\aichat\Model\AbstractModel;
6*2e22aefbSAndreas Gohruse dokuwiki\plugin\aichat\Model\Generic\AbstractGenericModel;
7cfd76f4aSAndreas Gohr
8cfd76f4aSAndreas Gohr/**
9cfd76f4aSAndreas Gohr * Abstract OpenAI Model
10cfd76f4aSAndreas Gohr *
11cfd76f4aSAndreas Gohr * This class provides a basic interface to the OpenAI API
12cfd76f4aSAndreas Gohr */
13*2e22aefbSAndreas Gohrabstract class AbstractMistralModel extends AbstractGenericModel
14cfd76f4aSAndreas Gohr{
15*2e22aefbSAndreas Gohr    protected $apiurl = 'https://api.mistral.ai/v1/';
16cfd76f4aSAndreas Gohr
17cfd76f4aSAndreas Gohr    /** @inheritdoc */
18cfd76f4aSAndreas Gohr    protected function parseAPIResponse($response)
19cfd76f4aSAndreas Gohr    {
20cfd76f4aSAndreas Gohr        if (isset($response['usage'])) {
21*2e22aefbSAndreas Gohr            $this->inputTokensUsed += $response['usage']['prompt_tokens'] ?? 0;
22cfd76f4aSAndreas Gohr            $this->outputTokensUsed += $response['usage']['completion_tokens'] ?? 0;
23cfd76f4aSAndreas Gohr        }
24cfd76f4aSAndreas Gohr
25c2b7a1f7SAndreas Gohr        if (isset($response['object']) && $response['object'] === 'error') {
2642b2c6e8SAndreas Gohr            throw new \Exception('Mistral API error: ' . $response['message'], 3002);
27cfd76f4aSAndreas Gohr        }
28cfd76f4aSAndreas Gohr
29cfd76f4aSAndreas Gohr        return $response;
30cfd76f4aSAndreas Gohr    }
31cfd76f4aSAndreas Gohr
32cfd76f4aSAndreas Gohr}
33