10337f47fSAndreas Gohr<?php 20337f47fSAndreas Gohr 3f6ef2e50SAndreas Gohruse dokuwiki\plugin\aichat\Model\AbstractModel; 4f6ef2e50SAndreas Gohruse dokuwiki\plugin\aichat\Chunk; 50337f47fSAndreas Gohruse dokuwiki\plugin\aichat\Embeddings; 6f6ef2e50SAndreas Gohruse dokuwiki\plugin\aichat\Model\OpenAI\GPT35Turbo; 7*01f06932SAndreas Gohruse dokuwiki\plugin\aichat\Storage\AbstractStorage; 8f6ef2e50SAndreas Gohruse dokuwiki\plugin\aichat\Storage\SQLiteStorage; 90337f47fSAndreas Gohr 100337f47fSAndreas Gohrrequire_once __DIR__ . '/vendor/autoload.php'; 110337f47fSAndreas Gohr 120337f47fSAndreas Gohr/** 130337f47fSAndreas Gohr * DokuWiki Plugin aichat (Helper Component) 140337f47fSAndreas Gohr * 150337f47fSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 160337f47fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 170337f47fSAndreas Gohr */ 180337f47fSAndreas Gohrclass helper_plugin_aichat extends \dokuwiki\Extension\Plugin 190337f47fSAndreas Gohr{ 20f6ef2e50SAndreas Gohr /** @var AbstractModel */ 21f6ef2e50SAndreas Gohr protected $model; 220337f47fSAndreas Gohr /** @var Embeddings */ 230337f47fSAndreas Gohr protected $embeddings; 24*01f06932SAndreas Gohr /** @var AbstractStorage */ 25*01f06932SAndreas Gohr protected $storage; 260337f47fSAndreas Gohr 270337f47fSAndreas Gohr /** 28c4127b8eSAndreas Gohr * Check if the current user is allowed to use the plugin (if it has been restricted) 29c4127b8eSAndreas Gohr * 30c4127b8eSAndreas Gohr * @return bool 31c4127b8eSAndreas Gohr */ 32c4127b8eSAndreas Gohr public function userMayAccess() 33c4127b8eSAndreas Gohr { 34c4127b8eSAndreas Gohr global $auth; 35c4127b8eSAndreas Gohr global $USERINFO; 36c4127b8eSAndreas Gohr global $INPUT; 37c4127b8eSAndreas Gohr 38c4127b8eSAndreas Gohr if (!$auth) return true; 39c4127b8eSAndreas Gohr if (!$this->getConf('restrict')) return true; 40c4127b8eSAndreas Gohr if (!isset($USERINFO)) return false; 41c4127b8eSAndreas Gohr 42c4127b8eSAndreas Gohr return auth_isMember($this->getConf('restrict'), $INPUT->server->str('REMOTE_USER'), $USERINFO['grps']); 43c4127b8eSAndreas Gohr } 44c4127b8eSAndreas Gohr 45c4127b8eSAndreas Gohr /** 460337f47fSAndreas Gohr * Access the OpenAI client 470337f47fSAndreas Gohr * 48f6ef2e50SAndreas Gohr * @return GPT35Turbo 490337f47fSAndreas Gohr */ 50f6ef2e50SAndreas Gohr public function getModel() 510337f47fSAndreas Gohr { 529f6b34c4SAndreas Gohr if ($this->model === null) { 539f6b34c4SAndreas Gohr $class = '\\dokuwiki\\plugin\\aichat\\Model\\' . $this->getConf('model'); 549f6b34c4SAndreas Gohr 559f6b34c4SAndreas Gohr if (!class_exists($class)) { 569f6b34c4SAndreas Gohr throw new \RuntimeException('Configured model not found: ' . $class); 579f6b34c4SAndreas Gohr } 589f6b34c4SAndreas Gohr // FIXME for now we only have OpenAI models, so we can hardcode the auth setup 599f6b34c4SAndreas Gohr $this->model = new $class([ 609f6b34c4SAndreas Gohr 'key' => $this->getConf('openaikey'), 619f6b34c4SAndreas Gohr 'org' => $this->getConf('openaiorg') 629f6b34c4SAndreas Gohr ]); 639f6b34c4SAndreas Gohr } 649f6b34c4SAndreas Gohr 65f6ef2e50SAndreas Gohr return $this->model; 660337f47fSAndreas Gohr } 670337f47fSAndreas Gohr 680337f47fSAndreas Gohr /** 690337f47fSAndreas Gohr * Access the Embeddings interface 700337f47fSAndreas Gohr * 710337f47fSAndreas Gohr * @return Embeddings 720337f47fSAndreas Gohr */ 730337f47fSAndreas Gohr public function getEmbeddings() 740337f47fSAndreas Gohr { 759f6b34c4SAndreas Gohr if ($this->embeddings === null) { 769f6b34c4SAndreas Gohr // FIXME we currently have only one storage backend, so we can hardcode it 77*01f06932SAndreas Gohr $this->embeddings = new Embeddings($this->getModel(), $this->getStorage()); 789f6b34c4SAndreas Gohr } 799f6b34c4SAndreas Gohr 800337f47fSAndreas Gohr return $this->embeddings; 810337f47fSAndreas Gohr } 820337f47fSAndreas Gohr 830337f47fSAndreas Gohr /** 84*01f06932SAndreas Gohr * Access the Storage interface 85*01f06932SAndreas Gohr * 86*01f06932SAndreas Gohr * @return AbstractStorage 87*01f06932SAndreas Gohr */ 88*01f06932SAndreas Gohr public function getStorage() 89*01f06932SAndreas Gohr { 90*01f06932SAndreas Gohr if ($this->storage === null) { 91*01f06932SAndreas Gohr $this->storage = new SQLiteStorage(); 92*01f06932SAndreas Gohr } 93*01f06932SAndreas Gohr 94*01f06932SAndreas Gohr return $this->storage; 95*01f06932SAndreas Gohr } 96*01f06932SAndreas Gohr 97*01f06932SAndreas Gohr /** 980337f47fSAndreas Gohr * Ask a question with a chat history 990337f47fSAndreas Gohr * 1000337f47fSAndreas Gohr * @param string $question 1010337f47fSAndreas Gohr * @param array[] $history The chat history [[user, ai], [user, ai], ...] 1020337f47fSAndreas Gohr * @return array ['question' => $question, 'answer' => $answer, 'sources' => $sources] 1030337f47fSAndreas Gohr * @throws Exception 1040337f47fSAndreas Gohr */ 1050337f47fSAndreas Gohr public function askChatQuestion($question, $history = []) 1060337f47fSAndreas Gohr { 1070337f47fSAndreas Gohr if ($history) { 1080337f47fSAndreas Gohr $standaloneQuestion = $this->rephraseChatQuestion($question, $history); 1090337f47fSAndreas Gohr } else { 1100337f47fSAndreas Gohr $standaloneQuestion = $question; 1110337f47fSAndreas Gohr } 1120337f47fSAndreas Gohr return $this->askQuestion($standaloneQuestion); 1130337f47fSAndreas Gohr } 1140337f47fSAndreas Gohr 1150337f47fSAndreas Gohr /** 1160337f47fSAndreas Gohr * Ask a single standalone question 1170337f47fSAndreas Gohr * 1180337f47fSAndreas Gohr * @param string $question 1190337f47fSAndreas Gohr * @return array ['question' => $question, 'answer' => $answer, 'sources' => $sources] 1200337f47fSAndreas Gohr * @throws Exception 1210337f47fSAndreas Gohr */ 1220337f47fSAndreas Gohr public function askQuestion($question) 1230337f47fSAndreas Gohr { 1249f6b34c4SAndreas Gohr $similar = $this->getEmbeddings()->getSimilarChunks($question); 1259e81bea7SAndreas Gohr if ($similar) { 12655392016SAndreas Gohr $context = implode("\n", array_map(function (Chunk $chunk) { 12768908844SAndreas Gohr return "\n```\n" . $chunk->getText() . "\n```\n"; 12855392016SAndreas Gohr }, $similar)); 1290337f47fSAndreas Gohr $prompt = $this->getPrompt('question', ['context' => $context]); 1309e81bea7SAndreas Gohr } else { 1319e81bea7SAndreas Gohr $prompt = $this->getPrompt('noanswer'); 1329e81bea7SAndreas Gohr } 13368908844SAndreas Gohr 1340337f47fSAndreas Gohr $messages = [ 1350337f47fSAndreas Gohr [ 1360337f47fSAndreas Gohr 'role' => 'system', 1370337f47fSAndreas Gohr 'content' => $prompt 1380337f47fSAndreas Gohr ], 1390337f47fSAndreas Gohr [ 1400337f47fSAndreas Gohr 'role' => 'user', 1410337f47fSAndreas Gohr 'content' => $question 1420337f47fSAndreas Gohr ] 1430337f47fSAndreas Gohr ]; 1440337f47fSAndreas Gohr 1459f6b34c4SAndreas Gohr $answer = $this->getModel()->getAnswer($messages); 1460337f47fSAndreas Gohr 1470337f47fSAndreas Gohr return [ 1480337f47fSAndreas Gohr 'question' => $question, 1490337f47fSAndreas Gohr 'answer' => $answer, 1500337f47fSAndreas Gohr 'sources' => $similar, 1510337f47fSAndreas Gohr ]; 1520337f47fSAndreas Gohr } 1530337f47fSAndreas Gohr 1540337f47fSAndreas Gohr /** 1550337f47fSAndreas Gohr * Rephrase a question into a standalone question based on the chat history 1560337f47fSAndreas Gohr * 1570337f47fSAndreas Gohr * @param string $question The original user question 1580337f47fSAndreas Gohr * @param array[] $history The chat history [[user, ai], [user, ai], ...] 1590337f47fSAndreas Gohr * @return string The rephrased question 1600337f47fSAndreas Gohr * @throws Exception 1610337f47fSAndreas Gohr */ 1620337f47fSAndreas Gohr public function rephraseChatQuestion($question, $history) 1630337f47fSAndreas Gohr { 1640337f47fSAndreas Gohr // go back in history as far as possible without hitting the token limit 1650337f47fSAndreas Gohr $chatHistory = ''; 1660337f47fSAndreas Gohr $history = array_reverse($history); 1670337f47fSAndreas Gohr foreach ($history as $row) { 168f6ef2e50SAndreas Gohr if ( 1699f6b34c4SAndreas Gohr count($this->getEmbeddings()->getTokenEncoder()->encode($chatHistory)) > 1709f6b34c4SAndreas Gohr $this->getModel()->getMaxRephrasingTokenLength() 171f6ef2e50SAndreas Gohr ) { 1720337f47fSAndreas Gohr break; 1730337f47fSAndreas Gohr } 1740337f47fSAndreas Gohr 1750337f47fSAndreas Gohr $chatHistory = 1760337f47fSAndreas Gohr "Human: " . $row[0] . "\n" . 1770337f47fSAndreas Gohr "Assistant: " . $row[1] . "\n" . 1780337f47fSAndreas Gohr $chatHistory; 1790337f47fSAndreas Gohr } 1800337f47fSAndreas Gohr 1810337f47fSAndreas Gohr // ask openAI to rephrase the question 1820337f47fSAndreas Gohr $prompt = $this->getPrompt('rephrase', ['history' => $chatHistory, 'question' => $question]); 1830337f47fSAndreas Gohr $messages = [['role' => 'user', 'content' => $prompt]]; 1849f6b34c4SAndreas Gohr return $this->getModel()->getRephrasedQuestion($messages); 1850337f47fSAndreas Gohr } 1860337f47fSAndreas Gohr 1870337f47fSAndreas Gohr /** 1880337f47fSAndreas Gohr * Load the given prompt template and fill in the variables 1890337f47fSAndreas Gohr * 1900337f47fSAndreas Gohr * @param string $type 1910337f47fSAndreas Gohr * @param string[] $vars 1920337f47fSAndreas Gohr * @return string 1930337f47fSAndreas Gohr */ 1940337f47fSAndreas Gohr protected function getPrompt($type, $vars = []) 1950337f47fSAndreas Gohr { 1960337f47fSAndreas Gohr $template = file_get_contents($this->localFN('prompt_' . $type)); 1970337f47fSAndreas Gohr 1980337f47fSAndreas Gohr $replace = array(); 1990337f47fSAndreas Gohr foreach ($vars as $key => $val) { 2000337f47fSAndreas Gohr $replace['{{' . strtoupper($key) . '}}'] = $val; 2010337f47fSAndreas Gohr } 2020337f47fSAndreas Gohr 2030337f47fSAndreas Gohr return strtr($template, $replace); 2040337f47fSAndreas Gohr } 2050337f47fSAndreas Gohr} 2060337f47fSAndreas Gohr 207