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