xref: /plugin/aichat/Model/OpenAI/ChatModel.php (revision 163a36ec0aeda42b0bcbfee0194b166ed4274aa0)
1dce0dee5SAndreas Gohr<?php
2dce0dee5SAndreas Gohr
3dce0dee5SAndreas Gohrnamespace dokuwiki\plugin\aichat\Model\OpenAI;
4dce0dee5SAndreas Gohr
5dce0dee5SAndreas Gohruse dokuwiki\plugin\aichat\Model\ChatInterface;
6dce0dee5SAndreas Gohr
7dce0dee5SAndreas Gohrclass ChatModel extends AbstractOpenAIModel implements ChatInterface
8dce0dee5SAndreas Gohr{
9dce0dee5SAndreas Gohr    /** @inheritdoc */
10dce0dee5SAndreas Gohr    public function getAnswer(array $messages): string
11dce0dee5SAndreas Gohr    {
12dce0dee5SAndreas Gohr        $data = [
13dce0dee5SAndreas Gohr            'messages' => $messages,
14dce0dee5SAndreas Gohr            'model' => $this->getModelName(),
15*163a36ecSAndreas Gohr            'max_completion_tokens' => null,
16dce0dee5SAndreas Gohr            'stream' => false,
17dce0dee5SAndreas Gohr            'n' => 1, // number of completions
18dce0dee5SAndreas Gohr        ];
19*163a36ecSAndreas Gohr
20*163a36ecSAndreas Gohr        // resoning models o1, o1-mini, o3-mini do not support setting temperature
21*163a36ecSAndreas Gohr        // for all others we want a low temperature to get more coherent answers
22*163a36ecSAndreas Gohr        if(!str_starts_with($this->getModelName(), 'o')) {
23*163a36ecSAndreas Gohr            $data['temperature'] = 0.0;
24*163a36ecSAndreas Gohr        }
25*163a36ecSAndreas Gohr
26dce0dee5SAndreas Gohr        $response = $this->request('chat/completions', $data);
27dce0dee5SAndreas Gohr        return $response['choices'][0]['message']['content'];
28dce0dee5SAndreas Gohr    }
29dce0dee5SAndreas Gohr}
30