10337f47fSAndreas Gohr<?php 20337f47fSAndreas Gohr 33379af09SAndreas Gohruse dokuwiki\Extension\CLIPlugin; 4f6ef2e50SAndreas Gohruse dokuwiki\plugin\aichat\Model\AbstractModel; 5f6ef2e50SAndreas Gohruse dokuwiki\plugin\aichat\Chunk; 60337f47fSAndreas Gohruse dokuwiki\plugin\aichat\Embeddings; 7f6ef2e50SAndreas Gohruse dokuwiki\plugin\aichat\Model\OpenAI\GPT35Turbo; 801f06932SAndreas Gohruse dokuwiki\plugin\aichat\Storage\AbstractStorage; 913dbfc23SAndreas Gohruse dokuwiki\plugin\aichat\Storage\PineconeStorage; 10f6ef2e50SAndreas Gohruse dokuwiki\plugin\aichat\Storage\SQLiteStorage; 110337f47fSAndreas Gohr 120337f47fSAndreas Gohrrequire_once __DIR__ . '/vendor/autoload.php'; 130337f47fSAndreas Gohr 140337f47fSAndreas Gohr/** 150337f47fSAndreas Gohr * DokuWiki Plugin aichat (Helper Component) 160337f47fSAndreas Gohr * 170337f47fSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 180337f47fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 190337f47fSAndreas Gohr */ 200337f47fSAndreas Gohrclass helper_plugin_aichat extends \dokuwiki\Extension\Plugin 210337f47fSAndreas Gohr{ 223379af09SAndreas Gohr /** @var CLIPlugin $logger */ 233379af09SAndreas Gohr protected $logger; 24f6ef2e50SAndreas Gohr /** @var AbstractModel */ 25f6ef2e50SAndreas Gohr protected $model; 260337f47fSAndreas Gohr /** @var Embeddings */ 270337f47fSAndreas Gohr protected $embeddings; 2801f06932SAndreas Gohr /** @var AbstractStorage */ 2901f06932SAndreas Gohr protected $storage; 300337f47fSAndreas Gohr 310337f47fSAndreas Gohr /** 323379af09SAndreas Gohr * Use the given CLI plugin for logging 333379af09SAndreas Gohr * 343379af09SAndreas Gohr * @param CLIPlugin $logger 353379af09SAndreas Gohr * @return void 363379af09SAndreas Gohr */ 378285fff9SAndreas Gohr public function setLogger($logger) 388285fff9SAndreas Gohr { 393379af09SAndreas Gohr $this->logger = $logger; 403379af09SAndreas Gohr } 413379af09SAndreas Gohr 423379af09SAndreas 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 * 63f6ef2e50SAndreas Gohr * @return GPT35Turbo 640337f47fSAndreas Gohr */ 65f6ef2e50SAndreas Gohr public function getModel() 660337f47fSAndreas Gohr { 679f6b34c4SAndreas Gohr if ($this->model === null) { 689f6b34c4SAndreas Gohr $class = '\\dokuwiki\\plugin\\aichat\\Model\\' . $this->getConf('model'); 699f6b34c4SAndreas Gohr 709f6b34c4SAndreas Gohr if (!class_exists($class)) { 719f6b34c4SAndreas Gohr throw new \RuntimeException('Configured model not found: ' . $class); 729f6b34c4SAndreas Gohr } 739f6b34c4SAndreas Gohr // FIXME for now we only have OpenAI models, so we can hardcode the auth setup 749f6b34c4SAndreas Gohr $this->model = new $class([ 759f6b34c4SAndreas Gohr 'key' => $this->getConf('openaikey'), 769f6b34c4SAndreas Gohr 'org' => $this->getConf('openaiorg') 779f6b34c4SAndreas Gohr ]); 789f6b34c4SAndreas Gohr } 799f6b34c4SAndreas Gohr 80f6ef2e50SAndreas Gohr return $this->model; 810337f47fSAndreas Gohr } 820337f47fSAndreas Gohr 830337f47fSAndreas Gohr /** 840337f47fSAndreas Gohr * Access the Embeddings interface 850337f47fSAndreas Gohr * 860337f47fSAndreas Gohr * @return Embeddings 870337f47fSAndreas Gohr */ 880337f47fSAndreas Gohr public function getEmbeddings() 890337f47fSAndreas Gohr { 909f6b34c4SAndreas Gohr if ($this->embeddings === null) { 919f6b34c4SAndreas Gohr // FIXME we currently have only one storage backend, so we can hardcode it 9201f06932SAndreas Gohr $this->embeddings = new Embeddings($this->getModel(), $this->getStorage()); 933379af09SAndreas Gohr if ($this->logger) { 943379af09SAndreas Gohr $this->embeddings->setLogger($this->logger); 953379af09SAndreas Gohr } 969f6b34c4SAndreas Gohr } 979f6b34c4SAndreas Gohr 980337f47fSAndreas Gohr return $this->embeddings; 990337f47fSAndreas Gohr } 1000337f47fSAndreas Gohr 1010337f47fSAndreas Gohr /** 10201f06932SAndreas Gohr * Access the Storage interface 10301f06932SAndreas Gohr * 10401f06932SAndreas Gohr * @return AbstractStorage 10501f06932SAndreas Gohr */ 10601f06932SAndreas Gohr public function getStorage() 10701f06932SAndreas Gohr { 10801f06932SAndreas Gohr if ($this->storage === null) { 10913dbfc23SAndreas Gohr if ($this->getConf('pinecone_apikey')) { 11013dbfc23SAndreas Gohr $this->storage = new PineconeStorage(); 11113dbfc23SAndreas Gohr } else { 11201f06932SAndreas Gohr $this->storage = new SQLiteStorage(); 11368b6fa79SAndreas Gohr } 1148285fff9SAndreas Gohr 1153379af09SAndreas Gohr if ($this->logger) { 1163379af09SAndreas Gohr $this->storage->setLogger($this->logger); 1173379af09SAndreas Gohr } 11801f06932SAndreas Gohr } 11901f06932SAndreas Gohr 12001f06932SAndreas Gohr return $this->storage; 12101f06932SAndreas Gohr } 12201f06932SAndreas Gohr 12301f06932SAndreas Gohr /** 1240337f47fSAndreas Gohr * Ask a question with a chat history 1250337f47fSAndreas Gohr * 1260337f47fSAndreas Gohr * @param string $question 1270337f47fSAndreas Gohr * @param array[] $history The chat history [[user, ai], [user, ai], ...] 1280337f47fSAndreas Gohr * @return array ['question' => $question, 'answer' => $answer, 'sources' => $sources] 1290337f47fSAndreas Gohr * @throws Exception 1300337f47fSAndreas Gohr */ 1310337f47fSAndreas Gohr public function askChatQuestion($question, $history = []) 1320337f47fSAndreas Gohr { 1330337f47fSAndreas Gohr if ($history) { 1340337f47fSAndreas Gohr $standaloneQuestion = $this->rephraseChatQuestion($question, $history); 1350337f47fSAndreas Gohr } else { 1360337f47fSAndreas Gohr $standaloneQuestion = $question; 1370337f47fSAndreas Gohr } 1380337f47fSAndreas Gohr return $this->askQuestion($standaloneQuestion); 1390337f47fSAndreas Gohr } 1400337f47fSAndreas Gohr 1410337f47fSAndreas Gohr /** 1420337f47fSAndreas Gohr * Ask a single standalone question 1430337f47fSAndreas Gohr * 1440337f47fSAndreas Gohr * @param string $question 1450337f47fSAndreas Gohr * @return array ['question' => $question, 'answer' => $answer, 'sources' => $sources] 1460337f47fSAndreas Gohr * @throws Exception 1470337f47fSAndreas Gohr */ 1480337f47fSAndreas Gohr public function askQuestion($question) 1490337f47fSAndreas Gohr { 1509f6b34c4SAndreas Gohr $similar = $this->getEmbeddings()->getSimilarChunks($question); 1519e81bea7SAndreas Gohr if ($similar) { 15255392016SAndreas Gohr $context = implode("\n", array_map(function (Chunk $chunk) { 15368908844SAndreas Gohr return "\n```\n" . $chunk->getText() . "\n```\n"; 15455392016SAndreas Gohr }, $similar)); 155*219268b1SAndreas Gohr $prompt = $this->getPrompt('question', [ 156*219268b1SAndreas Gohr 'context' => $context, 157*219268b1SAndreas Gohr 'language' => $this->getLanguagePrompt() 158*219268b1SAndreas Gohr ]); 1599e81bea7SAndreas Gohr } else { 1609e81bea7SAndreas Gohr $prompt = $this->getPrompt('noanswer'); 1619e81bea7SAndreas Gohr } 16268908844SAndreas Gohr 1630337f47fSAndreas Gohr $messages = [ 1640337f47fSAndreas Gohr [ 1650337f47fSAndreas Gohr 'role' => 'system', 1660337f47fSAndreas Gohr 'content' => $prompt 1670337f47fSAndreas Gohr ], 1680337f47fSAndreas Gohr [ 1690337f47fSAndreas Gohr 'role' => 'user', 1700337f47fSAndreas Gohr 'content' => $question 1710337f47fSAndreas Gohr ] 1720337f47fSAndreas Gohr ]; 1730337f47fSAndreas Gohr 1749f6b34c4SAndreas Gohr $answer = $this->getModel()->getAnswer($messages); 1750337f47fSAndreas Gohr 1760337f47fSAndreas Gohr return [ 1770337f47fSAndreas Gohr 'question' => $question, 1780337f47fSAndreas Gohr 'answer' => $answer, 1790337f47fSAndreas Gohr 'sources' => $similar, 1800337f47fSAndreas Gohr ]; 1810337f47fSAndreas Gohr } 1820337f47fSAndreas Gohr 1830337f47fSAndreas Gohr /** 1840337f47fSAndreas Gohr * Rephrase a question into a standalone question based on the chat history 1850337f47fSAndreas Gohr * 1860337f47fSAndreas Gohr * @param string $question The original user question 1870337f47fSAndreas Gohr * @param array[] $history The chat history [[user, ai], [user, ai], ...] 1880337f47fSAndreas Gohr * @return string The rephrased question 1890337f47fSAndreas Gohr * @throws Exception 1900337f47fSAndreas Gohr */ 1910337f47fSAndreas Gohr public function rephraseChatQuestion($question, $history) 1920337f47fSAndreas Gohr { 1930337f47fSAndreas Gohr // go back in history as far as possible without hitting the token limit 1940337f47fSAndreas Gohr $chatHistory = ''; 1950337f47fSAndreas Gohr $history = array_reverse($history); 1960337f47fSAndreas Gohr foreach ($history as $row) { 197f6ef2e50SAndreas Gohr if ( 1989f6b34c4SAndreas Gohr count($this->getEmbeddings()->getTokenEncoder()->encode($chatHistory)) > 1999f6b34c4SAndreas Gohr $this->getModel()->getMaxRephrasingTokenLength() 200f6ef2e50SAndreas Gohr ) { 2010337f47fSAndreas Gohr break; 2020337f47fSAndreas Gohr } 2030337f47fSAndreas Gohr 2040337f47fSAndreas Gohr $chatHistory = 2050337f47fSAndreas Gohr "Human: " . $row[0] . "\n" . 2060337f47fSAndreas Gohr "Assistant: " . $row[1] . "\n" . 2070337f47fSAndreas Gohr $chatHistory; 2080337f47fSAndreas Gohr } 2090337f47fSAndreas Gohr 2100337f47fSAndreas Gohr // ask openAI to rephrase the question 2110337f47fSAndreas Gohr $prompt = $this->getPrompt('rephrase', ['history' => $chatHistory, 'question' => $question]); 2120337f47fSAndreas Gohr $messages = [['role' => 'user', 'content' => $prompt]]; 2139f6b34c4SAndreas Gohr return $this->getModel()->getRephrasedQuestion($messages); 2140337f47fSAndreas Gohr } 2150337f47fSAndreas Gohr 2160337f47fSAndreas Gohr /** 2170337f47fSAndreas Gohr * Load the given prompt template and fill in the variables 2180337f47fSAndreas Gohr * 2190337f47fSAndreas Gohr * @param string $type 2200337f47fSAndreas Gohr * @param string[] $vars 2210337f47fSAndreas Gohr * @return string 2220337f47fSAndreas Gohr */ 2230337f47fSAndreas Gohr protected function getPrompt($type, $vars = []) 2240337f47fSAndreas Gohr { 2250337f47fSAndreas Gohr $template = file_get_contents($this->localFN('prompt_' . $type)); 2260337f47fSAndreas Gohr 2270337f47fSAndreas Gohr $replace = array(); 2280337f47fSAndreas Gohr foreach ($vars as $key => $val) { 2290337f47fSAndreas Gohr $replace['{{' . strtoupper($key) . '}}'] = $val; 2300337f47fSAndreas Gohr } 2310337f47fSAndreas Gohr 2320337f47fSAndreas Gohr return strtr($template, $replace); 2330337f47fSAndreas Gohr } 234*219268b1SAndreas Gohr 235*219268b1SAndreas Gohr /** 236*219268b1SAndreas Gohr * Construct the prompt to define the answer language 237*219268b1SAndreas Gohr * 238*219268b1SAndreas Gohr * @return string 239*219268b1SAndreas Gohr */ 240*219268b1SAndreas Gohr protected function getLanguagePrompt() 241*219268b1SAndreas Gohr { 242*219268b1SAndreas Gohr global $conf; 243*219268b1SAndreas Gohr 244*219268b1SAndreas Gohr if ($this->getConf('preferUIlanguage')) { 245*219268b1SAndreas Gohr $isoLangnames = include(__DIR__ . '/lang/languages.php'); 246*219268b1SAndreas Gohr if (isset($isoLangnames[$conf['lang']])) { 247*219268b1SAndreas Gohr $languagePrompt = 'Always answer in ' . $isoLangnames[$conf['lang']] . '.'; 248*219268b1SAndreas Gohr return $languagePrompt; 249*219268b1SAndreas Gohr } 250*219268b1SAndreas Gohr } 251*219268b1SAndreas Gohr 252*219268b1SAndreas Gohr $languagePrompt = 'Always answer in the user\'s language.'; 253*219268b1SAndreas Gohr return $languagePrompt; 254*219268b1SAndreas Gohr } 2550337f47fSAndreas Gohr} 2560337f47fSAndreas Gohr 257