xref: /plugin/aichat/Model/Reka/ChatModel.php (revision 7c3b69cbd638c7d1775b012bfb9b48d8136f76b9)
1eaa2c59dSAndreas Gohr<?php
2eaa2c59dSAndreas Gohr
3eaa2c59dSAndreas Gohrnamespace dokuwiki\plugin\aichat\Model\Reka;
4eaa2c59dSAndreas Gohr
5eaa2c59dSAndreas Gohruse dokuwiki\plugin\aichat\Model\AbstractModel;
6eaa2c59dSAndreas Gohruse dokuwiki\plugin\aichat\Model\ChatInterface;
72e22aefbSAndreas Gohruse dokuwiki\plugin\aichat\Model\ModelException;
8eaa2c59dSAndreas Gohr
9eaa2c59dSAndreas Gohrclass ChatModel extends AbstractModel implements ChatInterface
10eaa2c59dSAndreas Gohr{
11eaa2c59dSAndreas Gohr    /** @inheritdoc */
12*7c3b69cbSAndreas Gohr    protected function getHttpClient()
13eaa2c59dSAndreas Gohr    {
14*7c3b69cbSAndreas Gohr        $http = parent::getHttpClient();
15*7c3b69cbSAndreas Gohr        $http->headers['x-api-key'] = $this->getFromConf('apikey');
16*7c3b69cbSAndreas Gohr        return $http;
17eaa2c59dSAndreas Gohr    }
18eaa2c59dSAndreas Gohr
19eaa2c59dSAndreas Gohr    /** @inheritdoc */
20eaa2c59dSAndreas Gohr    public function getAnswer(array $messages): string
21eaa2c59dSAndreas Gohr    {
22eaa2c59dSAndreas Gohr        $chat = [];
23eaa2c59dSAndreas Gohr        foreach ($messages as $message) {
24eaa2c59dSAndreas Gohr            if ($message['role'] === 'user') {
25eaa2c59dSAndreas Gohr                $chat[] = [
26eaa2c59dSAndreas Gohr                    'type' => 'human',
27eaa2c59dSAndreas Gohr                    'text' => $message['content'],
28eaa2c59dSAndreas Gohr                ];
29eaa2c59dSAndreas Gohr            } elseif ($message['role'] === 'assistant') {
30eaa2c59dSAndreas Gohr                $chat[] = [
31eaa2c59dSAndreas Gohr                    'type' => 'model',
32eaa2c59dSAndreas Gohr                    'text' => $message['content'],
33eaa2c59dSAndreas Gohr                ];
34eaa2c59dSAndreas Gohr            }
35eaa2c59dSAndreas Gohr            // system messages are not supported
36eaa2c59dSAndreas Gohr        }
37eaa2c59dSAndreas Gohr
38eaa2c59dSAndreas Gohr        $data = [
39eaa2c59dSAndreas Gohr            'conversation_history' => $chat,
40eaa2c59dSAndreas Gohr            'model_name' => $this->getModelName(),
41eaa2c59dSAndreas Gohr            'temperature' => 0.0,
42eaa2c59dSAndreas Gohr        ];
43eaa2c59dSAndreas Gohr
44eaa2c59dSAndreas Gohr        $response = $this->request('chat', $data);
45eaa2c59dSAndreas Gohr        return $response['text'];
46eaa2c59dSAndreas Gohr    }
47eaa2c59dSAndreas Gohr
48eaa2c59dSAndreas Gohr    /**
49eaa2c59dSAndreas Gohr     * Send a request to the API
50eaa2c59dSAndreas Gohr     *
51eaa2c59dSAndreas Gohr     * @param string $endpoint
52eaa2c59dSAndreas Gohr     * @param array $data Payload to send
53eaa2c59dSAndreas Gohr     * @return array API response
54eaa2c59dSAndreas Gohr     * @throws \Exception
55eaa2c59dSAndreas Gohr     */
56eaa2c59dSAndreas Gohr    protected function request($endpoint, $data)
57eaa2c59dSAndreas Gohr    {
58eaa2c59dSAndreas Gohr        $url = 'https://api.reka.ai/' . $endpoint;
59eaa2c59dSAndreas Gohr        return $this->sendAPIRequest('POST', $url, $data);
60eaa2c59dSAndreas Gohr    }
61eaa2c59dSAndreas Gohr
62eaa2c59dSAndreas Gohr    /** @inheritdoc */
63eaa2c59dSAndreas Gohr    protected function parseAPIResponse($response)
64eaa2c59dSAndreas Gohr    {
65*7c3b69cbSAndreas Gohr        $http = $this->getHttpClient();
66*7c3b69cbSAndreas Gohr
67*7c3b69cbSAndreas Gohr        if (((int) $http->status) !== 200) {
68eaa2c59dSAndreas Gohr            if (isset($response['detail'])) {
692e22aefbSAndreas Gohr                throw new ModelException('Reka API error: ' . $response['detail'], 3002);
70eaa2c59dSAndreas Gohr            } else {
71*7c3b69cbSAndreas Gohr                throw new ModelException('Reka API error: ' . $http->status . ' ' . $http->error, 3002);
72eaa2c59dSAndreas Gohr            }
73eaa2c59dSAndreas Gohr        }
74eaa2c59dSAndreas Gohr
75eaa2c59dSAndreas Gohr        if (isset($response['metadata'])) {
76eaa2c59dSAndreas Gohr            $this->inputTokensUsed += $response['metadata']['input_tokens'];
77eaa2c59dSAndreas Gohr            $this->outputTokensUsed += $response['metadata']['generated_tokens'];
78eaa2c59dSAndreas Gohr        }
79eaa2c59dSAndreas Gohr
80eaa2c59dSAndreas Gohr        return $response;
81eaa2c59dSAndreas Gohr    }
82eaa2c59dSAndreas Gohr}
83