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; 768b6fa79SAndreas Gohruse dokuwiki\plugin\aichat\Storage\AbstractStorage; 8*13dbfc23SAndreas Gohruse dokuwiki\plugin\aichat\Storage\PineconeStorage; 9f6ef2e50SAndreas Gohruse dokuwiki\plugin\aichat\Storage\SQLiteStorage; 100337f47fSAndreas Gohr 110337f47fSAndreas Gohrrequire_once __DIR__ . '/vendor/autoload.php'; 120337f47fSAndreas Gohr 130337f47fSAndreas Gohr/** 140337f47fSAndreas Gohr * DokuWiki Plugin aichat (Helper Component) 150337f47fSAndreas Gohr * 160337f47fSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 170337f47fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 180337f47fSAndreas Gohr */ 190337f47fSAndreas Gohrclass helper_plugin_aichat extends \dokuwiki\Extension\Plugin 200337f47fSAndreas Gohr{ 21f6ef2e50SAndreas Gohr /** @var AbstractModel */ 22f6ef2e50SAndreas Gohr protected $model; 230337f47fSAndreas Gohr /** @var Embeddings */ 240337f47fSAndreas Gohr protected $embeddings; 2568b6fa79SAndreas Gohr /** @var AbstractStorage */ 2668b6fa79SAndreas Gohr protected $storage; 270337f47fSAndreas Gohr 280337f47fSAndreas Gohr /** 29c4127b8eSAndreas Gohr * Check if the current user is allowed to use the plugin (if it has been restricted) 30c4127b8eSAndreas Gohr * 31c4127b8eSAndreas Gohr * @return bool 32c4127b8eSAndreas Gohr */ 33c4127b8eSAndreas Gohr public function userMayAccess() 34c4127b8eSAndreas Gohr { 35c4127b8eSAndreas Gohr global $auth; 36c4127b8eSAndreas Gohr global $USERINFO; 37c4127b8eSAndreas Gohr global $INPUT; 38c4127b8eSAndreas Gohr 39c4127b8eSAndreas Gohr if (!$auth) return true; 40c4127b8eSAndreas Gohr if (!$this->getConf('restrict')) return true; 41c4127b8eSAndreas Gohr if (!isset($USERINFO)) return false; 42c4127b8eSAndreas Gohr 43c4127b8eSAndreas Gohr return auth_isMember($this->getConf('restrict'), $INPUT->server->str('REMOTE_USER'), $USERINFO['grps']); 44c4127b8eSAndreas Gohr } 45c4127b8eSAndreas Gohr 46c4127b8eSAndreas Gohr /** 470337f47fSAndreas Gohr * Access the OpenAI client 480337f47fSAndreas Gohr * 49f6ef2e50SAndreas Gohr * @return GPT35Turbo 500337f47fSAndreas Gohr */ 51f6ef2e50SAndreas Gohr public function getModel() 520337f47fSAndreas Gohr { 539f6b34c4SAndreas Gohr if ($this->model === null) { 549f6b34c4SAndreas Gohr $class = '\\dokuwiki\\plugin\\aichat\\Model\\' . $this->getConf('model'); 559f6b34c4SAndreas Gohr 569f6b34c4SAndreas Gohr if (!class_exists($class)) { 579f6b34c4SAndreas Gohr throw new \RuntimeException('Configured model not found: ' . $class); 589f6b34c4SAndreas Gohr } 599f6b34c4SAndreas Gohr // FIXME for now we only have OpenAI models, so we can hardcode the auth setup 609f6b34c4SAndreas Gohr $this->model = new $class([ 619f6b34c4SAndreas Gohr 'key' => $this->getConf('openaikey'), 629f6b34c4SAndreas Gohr 'org' => $this->getConf('openaiorg') 639f6b34c4SAndreas Gohr ]); 649f6b34c4SAndreas Gohr } 659f6b34c4SAndreas Gohr 66f6ef2e50SAndreas Gohr return $this->model; 670337f47fSAndreas Gohr } 680337f47fSAndreas Gohr 690337f47fSAndreas Gohr /** 700337f47fSAndreas Gohr * Access the Embeddings interface 710337f47fSAndreas Gohr * 720337f47fSAndreas Gohr * @return Embeddings 730337f47fSAndreas Gohr */ 740337f47fSAndreas Gohr public function getEmbeddings() 750337f47fSAndreas Gohr { 769f6b34c4SAndreas Gohr if ($this->embeddings === null) { 779f6b34c4SAndreas Gohr // FIXME we currently have only one storage backend, so we can hardcode it 7868b6fa79SAndreas Gohr $this->embeddings = new Embeddings($this->getModel(), $this->getStorage()); 799f6b34c4SAndreas Gohr } 809f6b34c4SAndreas Gohr 810337f47fSAndreas Gohr return $this->embeddings; 820337f47fSAndreas Gohr } 830337f47fSAndreas Gohr 840337f47fSAndreas Gohr /** 8568b6fa79SAndreas Gohr * Access the Storage interface 8668b6fa79SAndreas Gohr * 8768b6fa79SAndreas Gohr * @return AbstractStorage 8868b6fa79SAndreas Gohr */ 8968b6fa79SAndreas Gohr public function getStorage() 9068b6fa79SAndreas Gohr { 9168b6fa79SAndreas Gohr if ($this->storage === null) { 92*13dbfc23SAndreas Gohr if($this->getConf('pinecone_apikey')) { 93*13dbfc23SAndreas Gohr $this->storage = new PineconeStorage(); 94*13dbfc23SAndreas Gohr } else { 9568b6fa79SAndreas Gohr $this->storage = new SQLiteStorage(); 9668b6fa79SAndreas Gohr } 97*13dbfc23SAndreas Gohr } 9868b6fa79SAndreas Gohr 9968b6fa79SAndreas Gohr return $this->storage; 10068b6fa79SAndreas Gohr } 10168b6fa79SAndreas Gohr 10268b6fa79SAndreas Gohr /** 1030337f47fSAndreas Gohr * Ask a question with a chat history 1040337f47fSAndreas Gohr * 1050337f47fSAndreas Gohr * @param string $question 1060337f47fSAndreas Gohr * @param array[] $history The chat history [[user, ai], [user, ai], ...] 1070337f47fSAndreas Gohr * @return array ['question' => $question, 'answer' => $answer, 'sources' => $sources] 1080337f47fSAndreas Gohr * @throws Exception 1090337f47fSAndreas Gohr */ 1100337f47fSAndreas Gohr public function askChatQuestion($question, $history = []) 1110337f47fSAndreas Gohr { 1120337f47fSAndreas Gohr if ($history) { 1130337f47fSAndreas Gohr $standaloneQuestion = $this->rephraseChatQuestion($question, $history); 1140337f47fSAndreas Gohr } else { 1150337f47fSAndreas Gohr $standaloneQuestion = $question; 1160337f47fSAndreas Gohr } 1170337f47fSAndreas Gohr return $this->askQuestion($standaloneQuestion); 1180337f47fSAndreas Gohr } 1190337f47fSAndreas Gohr 1200337f47fSAndreas Gohr /** 1210337f47fSAndreas Gohr * Ask a single standalone question 1220337f47fSAndreas Gohr * 1230337f47fSAndreas Gohr * @param string $question 1240337f47fSAndreas Gohr * @return array ['question' => $question, 'answer' => $answer, 'sources' => $sources] 1250337f47fSAndreas Gohr * @throws Exception 1260337f47fSAndreas Gohr */ 1270337f47fSAndreas Gohr public function askQuestion($question) 1280337f47fSAndreas Gohr { 1299f6b34c4SAndreas Gohr $similar = $this->getEmbeddings()->getSimilarChunks($question); 1309e81bea7SAndreas Gohr if ($similar) { 13155392016SAndreas Gohr $context = implode("\n", array_map(function (Chunk $chunk) { 13268908844SAndreas Gohr return "\n```\n" . $chunk->getText() . "\n```\n"; 13355392016SAndreas Gohr }, $similar)); 1340337f47fSAndreas Gohr $prompt = $this->getPrompt('question', ['context' => $context]); 1359e81bea7SAndreas Gohr } else { 1369e81bea7SAndreas Gohr $prompt = $this->getPrompt('noanswer'); 1379e81bea7SAndreas Gohr } 13868908844SAndreas Gohr 1390337f47fSAndreas Gohr $messages = [ 1400337f47fSAndreas Gohr [ 1410337f47fSAndreas Gohr 'role' => 'system', 1420337f47fSAndreas Gohr 'content' => $prompt 1430337f47fSAndreas Gohr ], 1440337f47fSAndreas Gohr [ 1450337f47fSAndreas Gohr 'role' => 'user', 1460337f47fSAndreas Gohr 'content' => $question 1470337f47fSAndreas Gohr ] 1480337f47fSAndreas Gohr ]; 1490337f47fSAndreas Gohr 1509f6b34c4SAndreas Gohr $answer = $this->getModel()->getAnswer($messages); 1510337f47fSAndreas Gohr 1520337f47fSAndreas Gohr return [ 1530337f47fSAndreas Gohr 'question' => $question, 1540337f47fSAndreas Gohr 'answer' => $answer, 1550337f47fSAndreas Gohr 'sources' => $similar, 1560337f47fSAndreas Gohr ]; 1570337f47fSAndreas Gohr } 1580337f47fSAndreas Gohr 1590337f47fSAndreas Gohr /** 1600337f47fSAndreas Gohr * Rephrase a question into a standalone question based on the chat history 1610337f47fSAndreas Gohr * 1620337f47fSAndreas Gohr * @param string $question The original user question 1630337f47fSAndreas Gohr * @param array[] $history The chat history [[user, ai], [user, ai], ...] 1640337f47fSAndreas Gohr * @return string The rephrased question 1650337f47fSAndreas Gohr * @throws Exception 1660337f47fSAndreas Gohr */ 1670337f47fSAndreas Gohr public function rephraseChatQuestion($question, $history) 1680337f47fSAndreas Gohr { 1690337f47fSAndreas Gohr // go back in history as far as possible without hitting the token limit 1700337f47fSAndreas Gohr $chatHistory = ''; 1710337f47fSAndreas Gohr $history = array_reverse($history); 1720337f47fSAndreas Gohr foreach ($history as $row) { 173f6ef2e50SAndreas Gohr if ( 1749f6b34c4SAndreas Gohr count($this->getEmbeddings()->getTokenEncoder()->encode($chatHistory)) > 1759f6b34c4SAndreas Gohr $this->getModel()->getMaxRephrasingTokenLength() 176f6ef2e50SAndreas Gohr ) { 1770337f47fSAndreas Gohr break; 1780337f47fSAndreas Gohr } 1790337f47fSAndreas Gohr 1800337f47fSAndreas Gohr $chatHistory = 1810337f47fSAndreas Gohr "Human: " . $row[0] . "\n" . 1820337f47fSAndreas Gohr "Assistant: " . $row[1] . "\n" . 1830337f47fSAndreas Gohr $chatHistory; 1840337f47fSAndreas Gohr } 1850337f47fSAndreas Gohr 1860337f47fSAndreas Gohr // ask openAI to rephrase the question 1870337f47fSAndreas Gohr $prompt = $this->getPrompt('rephrase', ['history' => $chatHistory, 'question' => $question]); 1880337f47fSAndreas Gohr $messages = [['role' => 'user', 'content' => $prompt]]; 1899f6b34c4SAndreas Gohr return $this->getModel()->getRephrasedQuestion($messages); 1900337f47fSAndreas Gohr } 1910337f47fSAndreas Gohr 1920337f47fSAndreas Gohr /** 1930337f47fSAndreas Gohr * Load the given prompt template and fill in the variables 1940337f47fSAndreas Gohr * 1950337f47fSAndreas Gohr * @param string $type 1960337f47fSAndreas Gohr * @param string[] $vars 1970337f47fSAndreas Gohr * @return string 1980337f47fSAndreas Gohr */ 1990337f47fSAndreas Gohr protected function getPrompt($type, $vars = []) 2000337f47fSAndreas Gohr { 2010337f47fSAndreas Gohr $template = file_get_contents($this->localFN('prompt_' . $type)); 2020337f47fSAndreas Gohr 2030337f47fSAndreas Gohr $replace = array(); 2040337f47fSAndreas Gohr foreach ($vars as $key => $val) { 2050337f47fSAndreas Gohr $replace['{{' . strtoupper($key) . '}}'] = $val; 2060337f47fSAndreas Gohr } 2070337f47fSAndreas Gohr 2080337f47fSAndreas Gohr return strtr($template, $replace); 2090337f47fSAndreas Gohr } 2100337f47fSAndreas Gohr} 2110337f47fSAndreas Gohr 212