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