1<?php
2
3namespace dokuwiki\plugin\aichat\Model\Mistral;
4
5use dokuwiki\plugin\aichat\Model\ChatInterface;
6
7class ChatModel extends AbstractMistralModel implements ChatInterface
8{
9    /** @inheritdoc */
10    public function getAnswer(array $messages): string
11    {
12        // Mistral allows only for a system message at the beginning of the chat
13        // https://discord.com/channels/1144547040454508606/1220314306844037150
14        $system = '';
15        $chat = [];
16        foreach ($messages as $message) {
17            if ($message['role'] === 'system') {
18                $system .= $message['content'] . "\n";
19            } else {
20                $chat[] = $message;
21            }
22        }
23        $system = trim($system);
24        if ($system) {
25            array_unshift($chat, ['role' => 'system', 'content' => $system]);
26        }
27
28
29        $data = [
30            'messages' => $chat,
31            'model' => $this->getModelName(),
32            'max_tokens' => null,
33            'stream' => false,
34            'temperature' => 0.0,
35        ];
36        $response = $this->request('chat/completions', $data);
37        return $response['choices'][0]['message']['content'];
38    }
39}
40