xref: /plugin/aichat/helper.php (revision f6ef2e505783ac17f756e44bf15c66238362377a)
10337f47fSAndreas Gohr<?php
20337f47fSAndreas Gohr
3*f6ef2e50SAndreas Gohruse dokuwiki\plugin\aichat\Model\AbstractModel;
4*f6ef2e50SAndreas Gohruse dokuwiki\plugin\aichat\Chunk;
50337f47fSAndreas Gohruse dokuwiki\plugin\aichat\Embeddings;
6*f6ef2e50SAndreas Gohruse dokuwiki\plugin\aichat\Model\OpenAI\GPT35Turbo;
7*f6ef2e50SAndreas Gohruse dokuwiki\plugin\aichat\Storage\SQLiteStorage;
80337f47fSAndreas Gohr
90337f47fSAndreas Gohrrequire_once __DIR__ . '/vendor/autoload.php';
100337f47fSAndreas Gohr
110337f47fSAndreas Gohr/**
120337f47fSAndreas Gohr * DokuWiki Plugin aichat (Helper Component)
130337f47fSAndreas Gohr *
140337f47fSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
150337f47fSAndreas Gohr * @author  Andreas Gohr <gohr@cosmocode.de>
160337f47fSAndreas Gohr */
170337f47fSAndreas Gohrclass helper_plugin_aichat extends \dokuwiki\Extension\Plugin
180337f47fSAndreas Gohr{
19*f6ef2e50SAndreas Gohr    /** @var AbstractModel */
20*f6ef2e50SAndreas Gohr    protected $model;
210337f47fSAndreas Gohr    /** @var Embeddings */
220337f47fSAndreas Gohr    protected $embeddings;
230337f47fSAndreas Gohr
240337f47fSAndreas Gohr    public function __construct()
250337f47fSAndreas Gohr    {
26*f6ef2e50SAndreas Gohr        $class = '\\dokuwiki\\plugin\\aichat\\Model\\' . $this->getConf('model');
27*f6ef2e50SAndreas Gohr
28*f6ef2e50SAndreas Gohr        if (class_exists($class)) {
29*f6ef2e50SAndreas Gohr            // FIXME for now we only have OpenAI models, so we can hardcode the auth setup
30*f6ef2e50SAndreas Gohr            $this->model = new $class([
31*f6ef2e50SAndreas Gohr                'key' => $this->getConf('openaikey'),
32*f6ef2e50SAndreas Gohr                'org' => $this->getConf('openaiorg')
33*f6ef2e50SAndreas Gohr            ]);
34*f6ef2e50SAndreas Gohr        } else {
35*f6ef2e50SAndreas Gohr            throw new \Exception('Configured model not found: ' . $class);
36*f6ef2e50SAndreas Gohr        }
37*f6ef2e50SAndreas Gohr
38*f6ef2e50SAndreas Gohr        // FIXME we currently have only one storage backend, so we can hardcode it
39*f6ef2e50SAndreas Gohr        $this->embeddings = new Embeddings($this->model, new SQLiteStorage());
400337f47fSAndreas Gohr    }
410337f47fSAndreas Gohr
420337f47fSAndreas Gohr    /**
43c4127b8eSAndreas Gohr     * Check if the current user is allowed to use the plugin (if it has been restricted)
44c4127b8eSAndreas Gohr     *
45c4127b8eSAndreas Gohr     * @return bool
46c4127b8eSAndreas Gohr     */
47c4127b8eSAndreas Gohr    public function userMayAccess()
48c4127b8eSAndreas Gohr    {
49c4127b8eSAndreas Gohr        global $auth;
50c4127b8eSAndreas Gohr        global $USERINFO;
51c4127b8eSAndreas Gohr        global $INPUT;
52c4127b8eSAndreas Gohr
53c4127b8eSAndreas Gohr        if (!$auth) return true;
54c4127b8eSAndreas Gohr        if (!$this->getConf('restrict')) return true;
55c4127b8eSAndreas Gohr        if (!isset($USERINFO)) return false;
56c4127b8eSAndreas Gohr
57c4127b8eSAndreas Gohr        return auth_isMember($this->getConf('restrict'), $INPUT->server->str('REMOTE_USER'), $USERINFO['grps']);
58c4127b8eSAndreas Gohr    }
59c4127b8eSAndreas Gohr
60c4127b8eSAndreas Gohr    /**
610337f47fSAndreas Gohr     * Access the OpenAI client
620337f47fSAndreas Gohr     *
63*f6ef2e50SAndreas Gohr     * @return GPT35Turbo
640337f47fSAndreas Gohr     */
65*f6ef2e50SAndreas Gohr    public function getModel()
660337f47fSAndreas Gohr    {
67*f6ef2e50SAndreas Gohr        return $this->model;
680337f47fSAndreas Gohr    }
690337f47fSAndreas Gohr
700337f47fSAndreas Gohr    /**
710337f47fSAndreas Gohr     * Access the Embeddings interface
720337f47fSAndreas Gohr     *
730337f47fSAndreas Gohr     * @return Embeddings
740337f47fSAndreas Gohr     */
750337f47fSAndreas Gohr    public function getEmbeddings()
760337f47fSAndreas Gohr    {
770337f47fSAndreas Gohr        return $this->embeddings;
780337f47fSAndreas Gohr    }
790337f47fSAndreas Gohr
800337f47fSAndreas Gohr    /**
810337f47fSAndreas Gohr     * Ask a question with a chat history
820337f47fSAndreas Gohr     *
830337f47fSAndreas Gohr     * @param string $question
840337f47fSAndreas Gohr     * @param array[] $history The chat history [[user, ai], [user, ai], ...]
850337f47fSAndreas Gohr     * @return array ['question' => $question, 'answer' => $answer, 'sources' => $sources]
860337f47fSAndreas Gohr     * @throws Exception
870337f47fSAndreas Gohr     */
880337f47fSAndreas Gohr    public function askChatQuestion($question, $history = [])
890337f47fSAndreas Gohr    {
900337f47fSAndreas Gohr        if ($history) {
910337f47fSAndreas Gohr            $standaloneQuestion = $this->rephraseChatQuestion($question, $history);
920337f47fSAndreas Gohr        } else {
930337f47fSAndreas Gohr            $standaloneQuestion = $question;
940337f47fSAndreas Gohr        }
950337f47fSAndreas Gohr        return $this->askQuestion($standaloneQuestion);
960337f47fSAndreas Gohr    }
970337f47fSAndreas Gohr
980337f47fSAndreas Gohr    /**
990337f47fSAndreas Gohr     * Ask a single standalone question
1000337f47fSAndreas Gohr     *
1010337f47fSAndreas Gohr     * @param string $question
1020337f47fSAndreas Gohr     * @return array ['question' => $question, 'answer' => $answer, 'sources' => $sources]
1030337f47fSAndreas Gohr     * @throws Exception
1040337f47fSAndreas Gohr     */
1050337f47fSAndreas Gohr    public function askQuestion($question)
1060337f47fSAndreas Gohr    {
1070337f47fSAndreas Gohr        $similar = $this->embeddings->getSimilarChunks($question);
1089e81bea7SAndreas Gohr        if ($similar) {
10955392016SAndreas Gohr            $context = implode("\n", array_map(function (Chunk $chunk) {
11068908844SAndreas Gohr                return "\n```\n" . $chunk->getText() . "\n```\n";
11155392016SAndreas Gohr            }, $similar));
1120337f47fSAndreas Gohr            $prompt = $this->getPrompt('question', ['context' => $context]);
1139e81bea7SAndreas Gohr        } else {
1149e81bea7SAndreas Gohr            $prompt = $this->getPrompt('noanswer');
1159e81bea7SAndreas Gohr        }
11668908844SAndreas Gohr
1170337f47fSAndreas Gohr        $messages = [
1180337f47fSAndreas Gohr            [
1190337f47fSAndreas Gohr                'role' => 'system',
1200337f47fSAndreas Gohr                'content' => $prompt
1210337f47fSAndreas Gohr            ],
1220337f47fSAndreas Gohr            [
1230337f47fSAndreas Gohr                'role' => 'user',
1240337f47fSAndreas Gohr                'content' => $question
1250337f47fSAndreas Gohr            ]
1260337f47fSAndreas Gohr        ];
1270337f47fSAndreas Gohr
128*f6ef2e50SAndreas Gohr        $answer = $this->model->getAnswer($messages);
1290337f47fSAndreas Gohr
1300337f47fSAndreas Gohr        return [
1310337f47fSAndreas Gohr            'question' => $question,
1320337f47fSAndreas Gohr            'answer' => $answer,
1330337f47fSAndreas Gohr            'sources' => $similar,
1340337f47fSAndreas Gohr        ];
1350337f47fSAndreas Gohr    }
1360337f47fSAndreas Gohr
1370337f47fSAndreas Gohr    /**
1380337f47fSAndreas Gohr     * Rephrase a question into a standalone question based on the chat history
1390337f47fSAndreas Gohr     *
1400337f47fSAndreas Gohr     * @param string $question The original user question
1410337f47fSAndreas Gohr     * @param array[] $history The chat history [[user, ai], [user, ai], ...]
1420337f47fSAndreas Gohr     * @return string The rephrased question
1430337f47fSAndreas Gohr     * @throws Exception
1440337f47fSAndreas Gohr     */
1450337f47fSAndreas Gohr    public function rephraseChatQuestion($question, $history)
1460337f47fSAndreas Gohr    {
1470337f47fSAndreas Gohr        // go back in history as far as possible without hitting the token limit
1480337f47fSAndreas Gohr        $chatHistory = '';
1490337f47fSAndreas Gohr        $history = array_reverse($history);
1500337f47fSAndreas Gohr        foreach ($history as $row) {
151*f6ef2e50SAndreas Gohr            if (
152*f6ef2e50SAndreas Gohr                count($this->embeddings->getTokenEncoder()->encode($chatHistory)) >
153*f6ef2e50SAndreas Gohr                $this->model->getMaxRephrasingTokenLength()
154*f6ef2e50SAndreas Gohr            ) {
1550337f47fSAndreas Gohr                break;
1560337f47fSAndreas Gohr            }
1570337f47fSAndreas Gohr
1580337f47fSAndreas Gohr            $chatHistory =
1590337f47fSAndreas Gohr                "Human: " . $row[0] . "\n" .
1600337f47fSAndreas Gohr                "Assistant: " . $row[1] . "\n" .
1610337f47fSAndreas Gohr                $chatHistory;
1620337f47fSAndreas Gohr        }
1630337f47fSAndreas Gohr
1640337f47fSAndreas Gohr        // ask openAI to rephrase the question
1650337f47fSAndreas Gohr        $prompt = $this->getPrompt('rephrase', ['history' => $chatHistory, 'question' => $question]);
1660337f47fSAndreas Gohr        $messages = [['role' => 'user', 'content' => $prompt]];
167*f6ef2e50SAndreas Gohr        return $this->model->getRephrasedQuestion($messages);
1680337f47fSAndreas Gohr    }
1690337f47fSAndreas Gohr
1700337f47fSAndreas Gohr    /**
1710337f47fSAndreas Gohr     * Load the given prompt template and fill in the variables
1720337f47fSAndreas Gohr     *
1730337f47fSAndreas Gohr     * @param string $type
1740337f47fSAndreas Gohr     * @param string[] $vars
1750337f47fSAndreas Gohr     * @return string
1760337f47fSAndreas Gohr     */
1770337f47fSAndreas Gohr    protected function getPrompt($type, $vars = [])
1780337f47fSAndreas Gohr    {
1790337f47fSAndreas Gohr        $template = file_get_contents($this->localFN('prompt_' . $type));
1800337f47fSAndreas Gohr
1810337f47fSAndreas Gohr        $replace = array();
1820337f47fSAndreas Gohr        foreach ($vars as $key => $val) {
1830337f47fSAndreas Gohr            $replace['{{' . strtoupper($key) . '}}'] = $val;
1840337f47fSAndreas Gohr        }
1850337f47fSAndreas Gohr
1860337f47fSAndreas Gohr        return strtr($template, $replace);
1870337f47fSAndreas Gohr    }
1880337f47fSAndreas Gohr}
1890337f47fSAndreas Gohr
190