xref: /plugin/aichat/Model/Gemini/AbstractGeminiModel.php (revision 10789c175b44bee75b54261fbdf3bb1e6d176759)
1*10789c17SAndreas Gohr<?php
2*10789c17SAndreas Gohr
3*10789c17SAndreas Gohrnamespace dokuwiki\plugin\aichat\Model\Gemini;
4*10789c17SAndreas Gohr
5*10789c17SAndreas Gohruse dokuwiki\plugin\aichat\Model\AbstractModel;
6*10789c17SAndreas Gohr
7*10789c17SAndreas Gohrabstract class AbstractGeminiModel extends AbstractModel
8*10789c17SAndreas Gohr{
9*10789c17SAndreas Gohr
10*10789c17SAndreas Gohr    /** @var string Gemini API key */
11*10789c17SAndreas Gohr    protected $apikey;
12*10789c17SAndreas Gohr
13*10789c17SAndreas Gohr    /** @inheritdoc */
14*10789c17SAndreas Gohr    public function __construct(string $name, array $config)
15*10789c17SAndreas Gohr    {
16*10789c17SAndreas Gohr        parent::__construct($name, $config);
17*10789c17SAndreas Gohr
18*10789c17SAndreas Gohr        if (empty($config['gemini_apikey'])) {
19*10789c17SAndreas Gohr            throw new \Exception('Gemini API key not configured');
20*10789c17SAndreas Gohr        }
21*10789c17SAndreas Gohr
22*10789c17SAndreas Gohr        $this->apikey = $config['gemini_apikey'];
23*10789c17SAndreas Gohr    }
24*10789c17SAndreas Gohr
25*10789c17SAndreas Gohr    /**
26*10789c17SAndreas Gohr     * Send a request to the Gemini API
27*10789c17SAndreas Gohr     *
28*10789c17SAndreas Gohr     * @param string $endpoint
29*10789c17SAndreas Gohr     * @param array $data Payload to send
30*10789c17SAndreas Gohr     * @return array API response
31*10789c17SAndreas Gohr     * @throws \Exception
32*10789c17SAndreas Gohr     */
33*10789c17SAndreas Gohr    protected function request($model, $endpoint, $data)
34*10789c17SAndreas Gohr    {
35*10789c17SAndreas Gohr        $url = sprintf(
36*10789c17SAndreas Gohr            'https://generativelanguage.googleapis.com/v1beta/models/%s:%s?key=%s',
37*10789c17SAndreas Gohr            $model,
38*10789c17SAndreas Gohr            $endpoint,
39*10789c17SAndreas Gohr            $this->apikey
40*10789c17SAndreas Gohr        );
41*10789c17SAndreas Gohr
42*10789c17SAndreas Gohr        return $this->sendAPIRequest('POST', $url, $data);
43*10789c17SAndreas Gohr    }
44*10789c17SAndreas Gohr
45*10789c17SAndreas Gohr    /** @inheritdoc */
46*10789c17SAndreas Gohr    protected function parseAPIResponse($response)
47*10789c17SAndreas Gohr    {
48*10789c17SAndreas Gohr        if (isset($response['usageMetadata'])) {
49*10789c17SAndreas Gohr            $this->inputTokensUsed += $response['usageMetadata']['promptTokenCount'];
50*10789c17SAndreas Gohr            $this->outputTokensUsed += $response['usageMetadata']['candidatesTokenCount'] ?? 0;
51*10789c17SAndreas Gohr        }
52*10789c17SAndreas Gohr
53*10789c17SAndreas Gohr        if (isset($response['error'])) {
54*10789c17SAndreas Gohr            throw new \Exception('Gemini API error: ' . $response['error']['message']);
55*10789c17SAndreas Gohr        }
56*10789c17SAndreas Gohr
57*10789c17SAndreas Gohr        return $response;
58*10789c17SAndreas Gohr    }
59*10789c17SAndreas Gohr
60*10789c17SAndreas Gohr
61*10789c17SAndreas Gohr}
62