xref: /plugin/aichat/remote.php (revision 42b2c6e864def16df42600c2885e21d4d148fd0c)
1*42b2c6e8SAndreas Gohr<?php
2*42b2c6e8SAndreas Gohr
3*42b2c6e8SAndreas Gohruse dokuwiki\Extension\RemotePlugin;
4*42b2c6e8SAndreas Gohruse dokuwiki\plugin\aichat\RemoteResponse\LlmReply;
5*42b2c6e8SAndreas Gohruse dokuwiki\Remote\AccessDeniedException;
6*42b2c6e8SAndreas Gohr
7*42b2c6e8SAndreas Gohr/**
8*42b2c6e8SAndreas Gohr * DokuWiki Plugin aichat (Action Component)
9*42b2c6e8SAndreas Gohr *
10*42b2c6e8SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
11*42b2c6e8SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
12*42b2c6e8SAndreas Gohr */
13*42b2c6e8SAndreas Gohrclass remote_plugin_aichat extends RemotePlugin
14*42b2c6e8SAndreas Gohr{
15*42b2c6e8SAndreas Gohr    /**
16*42b2c6e8SAndreas Gohr     *
17*42b2c6e8SAndreas Gohr     * @param string $query The question to ask the LLM
18*42b2c6e8SAndreas Gohr     * @param string $model The model to use, if empty the default model is used
19*42b2c6e8SAndreas Gohr     * @param string $lang Language code to override preferUIlanguage setting. "auto" to force autodetection.
20*42b2c6e8SAndreas Gohr     * @return LlmReply
21*42b2c6e8SAndreas Gohr     */
22*42b2c6e8SAndreas Gohr    public function ask($query, $model = '', $lang = '')
23*42b2c6e8SAndreas Gohr    {
24*42b2c6e8SAndreas Gohr        /** @var helper_plugin_aichat $helper */
25*42b2c6e8SAndreas Gohr        $helper = plugin_load('helper', 'aichat');
26*42b2c6e8SAndreas Gohr        if ($model) {
27*42b2c6e8SAndreas Gohr            $helper->updateConfig(
28*42b2c6e8SAndreas Gohr                ['chatmodel' => $model, 'rephasemodel' => $model]
29*42b2c6e8SAndreas Gohr            );
30*42b2c6e8SAndreas Gohr        }
31*42b2c6e8SAndreas Gohr
32*42b2c6e8SAndreas Gohr        if (!$helper->userMayAccess()) {
33*42b2c6e8SAndreas Gohr            throw new AccessDeniedException('You are not allowed to use this plugin', 111);
34*42b2c6e8SAndreas Gohr        }
35*42b2c6e8SAndreas Gohr
36*42b2c6e8SAndreas Gohr        if ($lang === 'auto') {
37*42b2c6e8SAndreas Gohr            $helper->updateConfig(['preferUIlanguage' => 0]);
38*42b2c6e8SAndreas Gohr        } elseif ($lang) {
39*42b2c6e8SAndreas Gohr            $helper->updateConfig(['preferUIlanguage' => 1]);
40*42b2c6e8SAndreas Gohr            global $conf;
41*42b2c6e8SAndreas Gohr            $conf['lang'] = $lang;
42*42b2c6e8SAndreas Gohr        }
43*42b2c6e8SAndreas Gohr
44*42b2c6e8SAndreas Gohr        $result = $helper->askQuestion($query);
45*42b2c6e8SAndreas Gohr
46*42b2c6e8SAndreas Gohr        return new LlmReply($result);
47*42b2c6e8SAndreas Gohr    }
48*42b2c6e8SAndreas Gohr}
49