xref: /plugin/aichat/helper.php (revision 689088446f64ff8d9dfdae9ae0666b45de449da7)
10337f47fSAndreas Gohr<?php
20337f47fSAndreas Gohr
355392016SAndreas Gohruse dokuwiki\plugin\aichat\backend\Chunk;
40337f47fSAndreas Gohruse dokuwiki\plugin\aichat\Embeddings;
50337f47fSAndreas Gohruse dokuwiki\plugin\aichat\OpenAI;
60337f47fSAndreas Gohruse TikToken\Encoder;
70337f47fSAndreas Gohr
80337f47fSAndreas Gohrrequire_once __DIR__ . '/vendor/autoload.php';
90337f47fSAndreas Gohr
100337f47fSAndreas Gohr/**
110337f47fSAndreas Gohr * DokuWiki Plugin aichat (Helper Component)
120337f47fSAndreas Gohr *
130337f47fSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
140337f47fSAndreas Gohr * @author  Andreas Gohr <gohr@cosmocode.de>
150337f47fSAndreas Gohr */
160337f47fSAndreas Gohrclass helper_plugin_aichat extends \dokuwiki\Extension\Plugin
170337f47fSAndreas Gohr{
180337f47fSAndreas Gohr    /** @var OpenAI */
190337f47fSAndreas Gohr    protected $openAI;
200337f47fSAndreas Gohr    /** @var Embeddings */
210337f47fSAndreas Gohr    protected $embeddings;
220337f47fSAndreas Gohr
230337f47fSAndreas Gohr    public function __construct()
240337f47fSAndreas Gohr    {
250337f47fSAndreas Gohr        $this->openAI = new OpenAI($this->getConf('openaikey'), $this->getConf('openaiorg'));
260337f47fSAndreas Gohr        $this->embeddings = new Embeddings($this->openAI);
270337f47fSAndreas Gohr    }
280337f47fSAndreas Gohr
290337f47fSAndreas Gohr    /**
30c4127b8eSAndreas Gohr     * Check if the current user is allowed to use the plugin (if it has been restricted)
31c4127b8eSAndreas Gohr     *
32c4127b8eSAndreas Gohr     * @return bool
33c4127b8eSAndreas Gohr     */
34c4127b8eSAndreas Gohr    public function userMayAccess()
35c4127b8eSAndreas Gohr    {
36c4127b8eSAndreas Gohr        global $auth;
37c4127b8eSAndreas Gohr        global $USERINFO;
38c4127b8eSAndreas Gohr        global $INPUT;
39c4127b8eSAndreas Gohr
40c4127b8eSAndreas Gohr        if (!$auth) return true;
41c4127b8eSAndreas Gohr        if (!$this->getConf('restrict')) return true;
42c4127b8eSAndreas Gohr        if (!isset($USERINFO)) return false;
43c4127b8eSAndreas Gohr
44c4127b8eSAndreas Gohr        return auth_isMember($this->getConf('restrict'), $INPUT->server->str('REMOTE_USER'), $USERINFO['grps']);
45c4127b8eSAndreas Gohr    }
46c4127b8eSAndreas Gohr
47c4127b8eSAndreas Gohr    /**
480337f47fSAndreas Gohr     * Access the OpenAI client
490337f47fSAndreas Gohr     *
500337f47fSAndreas Gohr     * @return OpenAI
510337f47fSAndreas Gohr     */
520337f47fSAndreas Gohr    public function getOpenAI()
530337f47fSAndreas Gohr    {
540337f47fSAndreas Gohr        return $this->openAI;
550337f47fSAndreas Gohr    }
560337f47fSAndreas Gohr
570337f47fSAndreas Gohr    /**
580337f47fSAndreas Gohr     * Access the Embeddings interface
590337f47fSAndreas Gohr     *
600337f47fSAndreas Gohr     * @return Embeddings
610337f47fSAndreas Gohr     */
620337f47fSAndreas Gohr    public function getEmbeddings()
630337f47fSAndreas Gohr    {
640337f47fSAndreas Gohr        return $this->embeddings;
650337f47fSAndreas Gohr    }
660337f47fSAndreas Gohr
670337f47fSAndreas Gohr    /**
680337f47fSAndreas Gohr     * Ask a question with a chat history
690337f47fSAndreas Gohr     *
700337f47fSAndreas Gohr     * @param string $question
710337f47fSAndreas Gohr     * @param array[] $history The chat history [[user, ai], [user, ai], ...]
720337f47fSAndreas Gohr     * @return array ['question' => $question, 'answer' => $answer, 'sources' => $sources]
730337f47fSAndreas Gohr     * @throws Exception
740337f47fSAndreas Gohr     */
750337f47fSAndreas Gohr    public function askChatQuestion($question, $history = [])
760337f47fSAndreas Gohr    {
770337f47fSAndreas Gohr        if ($history) {
780337f47fSAndreas Gohr            $standaloneQuestion = $this->rephraseChatQuestion($question, $history);
790337f47fSAndreas Gohr        } else {
800337f47fSAndreas Gohr            $standaloneQuestion = $question;
810337f47fSAndreas Gohr        }
820337f47fSAndreas Gohr        return $this->askQuestion($standaloneQuestion);
830337f47fSAndreas Gohr    }
840337f47fSAndreas Gohr
850337f47fSAndreas Gohr    /**
860337f47fSAndreas Gohr     * Ask a single standalone question
870337f47fSAndreas Gohr     *
880337f47fSAndreas Gohr     * @param string $question
890337f47fSAndreas Gohr     * @return array ['question' => $question, 'answer' => $answer, 'sources' => $sources]
900337f47fSAndreas Gohr     * @throws Exception
9174d69006SAndreas Gohr     * @todo add context until token limit is hit
920337f47fSAndreas Gohr     */
930337f47fSAndreas Gohr    public function askQuestion($question)
940337f47fSAndreas Gohr    {
950337f47fSAndreas Gohr        $similar = $this->embeddings->getSimilarChunks($question);
969e81bea7SAndreas Gohr        if ($similar) {
9755392016SAndreas Gohr            $context = implode("\n", array_map(function (Chunk $chunk) {
98*68908844SAndreas Gohr                return "\n```\n" . $chunk->getText() . "\n```\n";
9955392016SAndreas Gohr            }, $similar));
1000337f47fSAndreas Gohr            $prompt = $this->getPrompt('question', ['context' => $context]);
1019e81bea7SAndreas Gohr        } else {
1029e81bea7SAndreas Gohr            $prompt = $this->getPrompt('noanswer');
1039e81bea7SAndreas Gohr        }
104*68908844SAndreas Gohr
1050337f47fSAndreas Gohr        $messages = [
1060337f47fSAndreas Gohr            [
1070337f47fSAndreas Gohr                'role' => 'system',
1080337f47fSAndreas Gohr                'content' => $prompt
1090337f47fSAndreas Gohr            ],
1100337f47fSAndreas Gohr            [
1110337f47fSAndreas Gohr                'role' => 'user',
1120337f47fSAndreas Gohr                'content' => $question
1130337f47fSAndreas Gohr            ]
1140337f47fSAndreas Gohr        ];
1150337f47fSAndreas Gohr
1160337f47fSAndreas Gohr        $answer = $this->openAI->getChatAnswer($messages);
1170337f47fSAndreas Gohr
1180337f47fSAndreas Gohr        return [
1190337f47fSAndreas Gohr            'question' => $question,
1200337f47fSAndreas Gohr            'answer' => $answer,
1210337f47fSAndreas Gohr            'sources' => $similar,
1220337f47fSAndreas Gohr        ];
1230337f47fSAndreas Gohr    }
1240337f47fSAndreas Gohr
1250337f47fSAndreas Gohr    /**
1260337f47fSAndreas Gohr     * Rephrase a question into a standalone question based on the chat history
1270337f47fSAndreas Gohr     *
1280337f47fSAndreas Gohr     * @param string $question The original user question
1290337f47fSAndreas Gohr     * @param array[] $history The chat history [[user, ai], [user, ai], ...]
1300337f47fSAndreas Gohr     * @return string The rephrased question
1310337f47fSAndreas Gohr     * @throws Exception
1320337f47fSAndreas Gohr     */
1330337f47fSAndreas Gohr    public function rephraseChatQuestion($question, $history)
1340337f47fSAndreas Gohr    {
1350337f47fSAndreas Gohr        // go back in history as far as possible without hitting the token limit
1360337f47fSAndreas Gohr        $tiktok = new Encoder();
1370337f47fSAndreas Gohr        $chatHistory = '';
1380337f47fSAndreas Gohr        $history = array_reverse($history);
1390337f47fSAndreas Gohr        foreach ($history as $row) {
1400337f47fSAndreas Gohr            if (count($tiktok->encode($chatHistory)) > 3000) {
1410337f47fSAndreas Gohr                break;
1420337f47fSAndreas Gohr            }
1430337f47fSAndreas Gohr
1440337f47fSAndreas Gohr            $chatHistory =
1450337f47fSAndreas Gohr                "Human: " . $row[0] . "\n" .
1460337f47fSAndreas Gohr                "Assistant: " . $row[1] . "\n" .
1470337f47fSAndreas Gohr                $chatHistory;
1480337f47fSAndreas Gohr        }
1490337f47fSAndreas Gohr
1500337f47fSAndreas Gohr        // ask openAI to rephrase the question
1510337f47fSAndreas Gohr        $prompt = $this->getPrompt('rephrase', ['history' => $chatHistory, 'question' => $question]);
1520337f47fSAndreas Gohr        $messages = [['role' => 'user', 'content' => $prompt]];
153*68908844SAndreas Gohr        return $this->openAI->getChatAnswer($messages, OpenAI::REPHRASE_MODEL);
1540337f47fSAndreas Gohr    }
1550337f47fSAndreas Gohr
1560337f47fSAndreas Gohr    /**
1570337f47fSAndreas Gohr     * Load the given prompt template and fill in the variables
1580337f47fSAndreas Gohr     *
1590337f47fSAndreas Gohr     * @param string $type
1600337f47fSAndreas Gohr     * @param string[] $vars
1610337f47fSAndreas Gohr     * @return string
1620337f47fSAndreas Gohr     */
1630337f47fSAndreas Gohr    protected function getPrompt($type, $vars = [])
1640337f47fSAndreas Gohr    {
1650337f47fSAndreas Gohr        $template = file_get_contents($this->localFN('prompt_' . $type));
1660337f47fSAndreas Gohr
1670337f47fSAndreas Gohr        $replace = array();
1680337f47fSAndreas Gohr        foreach ($vars as $key => $val) {
1690337f47fSAndreas Gohr            $replace['{{' . strtoupper($key) . '}}'] = $val;
1700337f47fSAndreas Gohr        }
1710337f47fSAndreas Gohr
1720337f47fSAndreas Gohr        return strtr($template, $replace);
1730337f47fSAndreas Gohr    }
1740337f47fSAndreas Gohr}
1750337f47fSAndreas Gohr
176