xref: /plugin/aichat/Model/Gemini/AbstractGeminiModel.php (revision 2e22aefbcc83da0bc9ab21a2175b8ada4ba79826)
110789c17SAndreas Gohr<?php
210789c17SAndreas Gohr
310789c17SAndreas Gohrnamespace dokuwiki\plugin\aichat\Model\Gemini;
410789c17SAndreas Gohr
510789c17SAndreas Gohruse dokuwiki\plugin\aichat\Model\AbstractModel;
6*2e22aefbSAndreas Gohruse dokuwiki\plugin\aichat\Model\ModelException;
710789c17SAndreas Gohr
810789c17SAndreas Gohrabstract class AbstractGeminiModel extends AbstractModel
910789c17SAndreas Gohr{
1010789c17SAndreas Gohr
1110789c17SAndreas Gohr    /** @var string Gemini API key */
1210789c17SAndreas Gohr    protected $apikey;
1310789c17SAndreas Gohr
1410789c17SAndreas Gohr    /** @inheritdoc */
1510789c17SAndreas Gohr    public function __construct(string $name, array $config)
1610789c17SAndreas Gohr    {
174dd0657eSAndreas Gohr        parent::__construct($name, $config);
18*2e22aefbSAndreas Gohr        $this->apikey = $this->getFromConf($config, 'apikey');
194dd0657eSAndreas Gohr    }
204dd0657eSAndreas Gohr
214dd0657eSAndreas Gohr    /** @inheritdoc */
224dd0657eSAndreas Gohr    function loadUnknownModelInfo(): array
234dd0657eSAndreas Gohr    {
244dd0657eSAndreas Gohr        $url = sprintf(
254dd0657eSAndreas Gohr            'https://generativelanguage.googleapis.com/v1beta/models/%s?key=%s',
264dd0657eSAndreas Gohr            $this->modelName,
274dd0657eSAndreas Gohr            $this->apikey
284dd0657eSAndreas Gohr        );
294dd0657eSAndreas Gohr        $result = $this->sendAPIRequest('GET', $url, '');
304dd0657eSAndreas Gohr        if(!$result) {
31*2e22aefbSAndreas Gohr            throw new ModelException('Failed to load model info for '.$this->modelFullName, 3003);
324dd0657eSAndreas Gohr        }
334dd0657eSAndreas Gohr
344dd0657eSAndreas Gohr        $info = parent::loadUnknownModelInfo();
354dd0657eSAndreas Gohr        $info['description'] = $result['description'];
364dd0657eSAndreas Gohr        $info['inputTokens'] = $result['inputTokenLimit'];
374dd0657eSAndreas Gohr        $info['outputTokens'] = $result['outputTokenLimit'];
384dd0657eSAndreas Gohr        return $info;
3910789c17SAndreas Gohr    }
4010789c17SAndreas Gohr
4110789c17SAndreas Gohr    /**
4210789c17SAndreas Gohr     * Send a request to the Gemini API
4310789c17SAndreas Gohr     *
4410789c17SAndreas Gohr     * @param string $endpoint
4510789c17SAndreas Gohr     * @param array $data Payload to send
4610789c17SAndreas Gohr     * @return array API response
4710789c17SAndreas Gohr     * @throws \Exception
4810789c17SAndreas Gohr     */
4910789c17SAndreas Gohr    protected function request($model, $endpoint, $data)
5010789c17SAndreas Gohr    {
5110789c17SAndreas Gohr        $url = sprintf(
5210789c17SAndreas Gohr            'https://generativelanguage.googleapis.com/v1beta/models/%s:%s?key=%s',
5310789c17SAndreas Gohr            $model,
5410789c17SAndreas Gohr            $endpoint,
5510789c17SAndreas Gohr            $this->apikey
5610789c17SAndreas Gohr        );
5710789c17SAndreas Gohr
5810789c17SAndreas Gohr        return $this->sendAPIRequest('POST', $url, $data);
5910789c17SAndreas Gohr    }
6010789c17SAndreas Gohr
6110789c17SAndreas Gohr    /** @inheritdoc */
6210789c17SAndreas Gohr    protected function parseAPIResponse($response)
6310789c17SAndreas Gohr    {
6410789c17SAndreas Gohr        if (isset($response['usageMetadata'])) {
6510789c17SAndreas Gohr            $this->inputTokensUsed += $response['usageMetadata']['promptTokenCount'];
6610789c17SAndreas Gohr            $this->outputTokensUsed += $response['usageMetadata']['candidatesTokenCount'] ?? 0;
6710789c17SAndreas Gohr        }
6810789c17SAndreas Gohr
6910789c17SAndreas Gohr        if (isset($response['error'])) {
70*2e22aefbSAndreas Gohr            throw new ModelException('Gemini API error: ' . $response['error']['message'], 3002);
7110789c17SAndreas Gohr        }
7210789c17SAndreas Gohr
7310789c17SAndreas Gohr        return $response;
7410789c17SAndreas Gohr    }
7510789c17SAndreas Gohr
7610789c17SAndreas Gohr
7710789c17SAndreas Gohr}
78