1<?php
2
3namespace dokuwiki\plugin\aichat\Model\Gemini;
4
5use dokuwiki\plugin\aichat\Model\ChatInterface;
6
7class ChatModel extends AbstractGeminiModel implements ChatInterface
8{
9    /** @inheritdoc */
10    public function getAnswer(array $messages): string
11    {
12        // Gemini payload is weird, we convert OpenAI style here
13        $data = [
14            'contents' => [],
15        ];
16        foreach ($messages as $message) {
17            if ($message['role'] === 'system') {
18                // system messages go to extra array
19                if (!isset($data['system_instructions'])) {
20                    $data['system_instructions'] = [];
21                    $data['system_instructions']['parts'] = [];
22                }
23                $data['system_instructions']['parts'][] = ['text' => $message['content']];
24            } else {
25                $data['contents'][] = [
26                    'role' => $message['role'],
27                    'parts' => [
28                        ['text' => $message['content']]
29                    ]
30                ];
31            }
32        }
33
34        $response = $this->request($this->getModelName(), 'generateContent', $data);
35        return $response['candidates'][0]['content']['parts'][0]['text'];
36    }
37}
38