xref: /plugin/aichat/Model/Gemini/AbstractGeminiModel.php (revision 4dd0657e5ac7e828b159f619391769caa2e5332f)
110789c17SAndreas Gohr<?php
210789c17SAndreas Gohr
310789c17SAndreas Gohrnamespace dokuwiki\plugin\aichat\Model\Gemini;
410789c17SAndreas Gohr
510789c17SAndreas Gohruse dokuwiki\plugin\aichat\Model\AbstractModel;
610789c17SAndreas Gohr
710789c17SAndreas Gohrabstract class AbstractGeminiModel extends AbstractModel
810789c17SAndreas Gohr{
910789c17SAndreas Gohr
1010789c17SAndreas Gohr    /** @var string Gemini API key */
1110789c17SAndreas Gohr    protected $apikey;
1210789c17SAndreas Gohr
1310789c17SAndreas Gohr    /** @inheritdoc */
1410789c17SAndreas Gohr    public function __construct(string $name, array $config)
1510789c17SAndreas Gohr    {
1610789c17SAndreas Gohr        if (empty($config['gemini_apikey'])) {
1710789c17SAndreas Gohr            throw new \Exception('Gemini API key not configured');
1810789c17SAndreas Gohr        }
1910789c17SAndreas Gohr
2010789c17SAndreas Gohr        $this->apikey = $config['gemini_apikey'];
21*4dd0657eSAndreas Gohr
22*4dd0657eSAndreas Gohr        parent::__construct($name, $config);
23*4dd0657eSAndreas Gohr    }
24*4dd0657eSAndreas Gohr
25*4dd0657eSAndreas Gohr    /** @inheritdoc */
26*4dd0657eSAndreas Gohr    function loadUnknownModelInfo(): array
27*4dd0657eSAndreas Gohr    {
28*4dd0657eSAndreas Gohr        $url = sprintf(
29*4dd0657eSAndreas Gohr            'https://generativelanguage.googleapis.com/v1beta/models/%s?key=%s',
30*4dd0657eSAndreas Gohr            $this->modelName,
31*4dd0657eSAndreas Gohr            $this->apikey
32*4dd0657eSAndreas Gohr        );
33*4dd0657eSAndreas Gohr        $result = $this->sendAPIRequest('GET', $url, '');
34*4dd0657eSAndreas Gohr        if(!$result) {
35*4dd0657eSAndreas Gohr            throw new \Exception('Failed to load model info for '.$this->modelFullName);
36*4dd0657eSAndreas Gohr        }
37*4dd0657eSAndreas Gohr
38*4dd0657eSAndreas Gohr        $info = parent::loadUnknownModelInfo();
39*4dd0657eSAndreas Gohr        $info['description'] = $result['description'];
40*4dd0657eSAndreas Gohr        $info['inputTokens'] = $result['inputTokenLimit'];
41*4dd0657eSAndreas Gohr        $info['outputTokens'] = $result['outputTokenLimit'];
42*4dd0657eSAndreas Gohr        return $info;
4310789c17SAndreas Gohr    }
4410789c17SAndreas Gohr
4510789c17SAndreas Gohr    /**
4610789c17SAndreas Gohr     * Send a request to the Gemini API
4710789c17SAndreas Gohr     *
4810789c17SAndreas Gohr     * @param string $endpoint
4910789c17SAndreas Gohr     * @param array $data Payload to send
5010789c17SAndreas Gohr     * @return array API response
5110789c17SAndreas Gohr     * @throws \Exception
5210789c17SAndreas Gohr     */
5310789c17SAndreas Gohr    protected function request($model, $endpoint, $data)
5410789c17SAndreas Gohr    {
5510789c17SAndreas Gohr        $url = sprintf(
5610789c17SAndreas Gohr            'https://generativelanguage.googleapis.com/v1beta/models/%s:%s?key=%s',
5710789c17SAndreas Gohr            $model,
5810789c17SAndreas Gohr            $endpoint,
5910789c17SAndreas Gohr            $this->apikey
6010789c17SAndreas Gohr        );
6110789c17SAndreas Gohr
6210789c17SAndreas Gohr        return $this->sendAPIRequest('POST', $url, $data);
6310789c17SAndreas Gohr    }
6410789c17SAndreas Gohr
6510789c17SAndreas Gohr    /** @inheritdoc */
6610789c17SAndreas Gohr    protected function parseAPIResponse($response)
6710789c17SAndreas Gohr    {
6810789c17SAndreas Gohr        if (isset($response['usageMetadata'])) {
6910789c17SAndreas Gohr            $this->inputTokensUsed += $response['usageMetadata']['promptTokenCount'];
7010789c17SAndreas Gohr            $this->outputTokensUsed += $response['usageMetadata']['candidatesTokenCount'] ?? 0;
7110789c17SAndreas Gohr        }
7210789c17SAndreas Gohr
7310789c17SAndreas Gohr        if (isset($response['error'])) {
7410789c17SAndreas Gohr            throw new \Exception('Gemini API error: ' . $response['error']['message']);
7510789c17SAndreas Gohr        }
7610789c17SAndreas Gohr
7710789c17SAndreas Gohr        return $response;
7810789c17SAndreas Gohr    }
7910789c17SAndreas Gohr
8010789c17SAndreas Gohr
8110789c17SAndreas Gohr}
82