10337f47fSAndreas Gohr<?php 20337f47fSAndreas Gohr 3*3379af09SAndreas 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; 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{ 21*3379af09SAndreas Gohr /** @var CLIPlugin $logger */ 22*3379af09SAndreas Gohr protected $logger; 23f6ef2e50SAndreas Gohr /** @var AbstractModel */ 24f6ef2e50SAndreas Gohr protected $model; 250337f47fSAndreas Gohr /** @var Embeddings */ 260337f47fSAndreas Gohr protected $embeddings; 2701f06932SAndreas Gohr /** @var AbstractStorage */ 2801f06932SAndreas Gohr protected $storage; 290337f47fSAndreas Gohr 300337f47fSAndreas Gohr /** 31*3379af09SAndreas Gohr * Use the given CLI plugin for logging 32*3379af09SAndreas Gohr * 33*3379af09SAndreas Gohr * @param CLIPlugin $logger 34*3379af09SAndreas Gohr * @return void 35*3379af09SAndreas Gohr */ 36*3379af09SAndreas Gohr public function setLogger($logger) { 37*3379af09SAndreas Gohr $this->logger = $logger; 38*3379af09SAndreas Gohr } 39*3379af09SAndreas Gohr 40*3379af09SAndreas Gohr /** 41c4127b8eSAndreas Gohr * Check if the current user is allowed to use the plugin (if it has been restricted) 42c4127b8eSAndreas Gohr * 43c4127b8eSAndreas Gohr * @return bool 44c4127b8eSAndreas Gohr */ 45c4127b8eSAndreas Gohr public function userMayAccess() 46c4127b8eSAndreas Gohr { 47c4127b8eSAndreas Gohr global $auth; 48c4127b8eSAndreas Gohr global $USERINFO; 49c4127b8eSAndreas Gohr global $INPUT; 50c4127b8eSAndreas Gohr 51c4127b8eSAndreas Gohr if (!$auth) return true; 52c4127b8eSAndreas Gohr if (!$this->getConf('restrict')) return true; 53c4127b8eSAndreas Gohr if (!isset($USERINFO)) return false; 54c4127b8eSAndreas Gohr 55c4127b8eSAndreas Gohr return auth_isMember($this->getConf('restrict'), $INPUT->server->str('REMOTE_USER'), $USERINFO['grps']); 56c4127b8eSAndreas Gohr } 57c4127b8eSAndreas Gohr 58c4127b8eSAndreas Gohr /** 590337f47fSAndreas Gohr * Access the OpenAI client 600337f47fSAndreas Gohr * 61f6ef2e50SAndreas Gohr * @return GPT35Turbo 620337f47fSAndreas Gohr */ 63f6ef2e50SAndreas Gohr public function getModel() 640337f47fSAndreas Gohr { 659f6b34c4SAndreas Gohr if ($this->model === null) { 669f6b34c4SAndreas Gohr $class = '\\dokuwiki\\plugin\\aichat\\Model\\' . $this->getConf('model'); 679f6b34c4SAndreas Gohr 689f6b34c4SAndreas Gohr if (!class_exists($class)) { 699f6b34c4SAndreas Gohr throw new \RuntimeException('Configured model not found: ' . $class); 709f6b34c4SAndreas Gohr } 719f6b34c4SAndreas Gohr // FIXME for now we only have OpenAI models, so we can hardcode the auth setup 729f6b34c4SAndreas Gohr $this->model = new $class([ 739f6b34c4SAndreas Gohr 'key' => $this->getConf('openaikey'), 749f6b34c4SAndreas Gohr 'org' => $this->getConf('openaiorg') 759f6b34c4SAndreas Gohr ]); 769f6b34c4SAndreas Gohr } 779f6b34c4SAndreas Gohr 78f6ef2e50SAndreas Gohr return $this->model; 790337f47fSAndreas Gohr } 800337f47fSAndreas Gohr 810337f47fSAndreas Gohr /** 820337f47fSAndreas Gohr * Access the Embeddings interface 830337f47fSAndreas Gohr * 840337f47fSAndreas Gohr * @return Embeddings 850337f47fSAndreas Gohr */ 860337f47fSAndreas Gohr public function getEmbeddings() 870337f47fSAndreas Gohr { 889f6b34c4SAndreas Gohr if ($this->embeddings === null) { 899f6b34c4SAndreas Gohr // FIXME we currently have only one storage backend, so we can hardcode it 9001f06932SAndreas Gohr $this->embeddings = new Embeddings($this->getModel(), $this->getStorage()); 91*3379af09SAndreas Gohr if($this->logger) { 92*3379af09SAndreas Gohr $this->embeddings->setLogger($this->logger); 93*3379af09SAndreas Gohr } 949f6b34c4SAndreas Gohr } 959f6b34c4SAndreas Gohr 960337f47fSAndreas Gohr return $this->embeddings; 970337f47fSAndreas Gohr } 980337f47fSAndreas Gohr 990337f47fSAndreas Gohr /** 10001f06932SAndreas Gohr * Access the Storage interface 10101f06932SAndreas Gohr * 10201f06932SAndreas Gohr * @return AbstractStorage 10301f06932SAndreas Gohr */ 10401f06932SAndreas Gohr public function getStorage() 10501f06932SAndreas Gohr { 10601f06932SAndreas Gohr if ($this->storage === null) { 10701f06932SAndreas Gohr $this->storage = new SQLiteStorage(); 108*3379af09SAndreas Gohr if($this->logger) { 109*3379af09SAndreas Gohr $this->storage->setLogger($this->logger); 110*3379af09SAndreas Gohr } 11101f06932SAndreas Gohr } 11201f06932SAndreas Gohr 11301f06932SAndreas Gohr return $this->storage; 11401f06932SAndreas Gohr } 11501f06932SAndreas Gohr 11601f06932SAndreas Gohr /** 1170337f47fSAndreas Gohr * Ask a question with a chat history 1180337f47fSAndreas Gohr * 1190337f47fSAndreas Gohr * @param string $question 1200337f47fSAndreas Gohr * @param array[] $history The chat history [[user, ai], [user, ai], ...] 1210337f47fSAndreas Gohr * @return array ['question' => $question, 'answer' => $answer, 'sources' => $sources] 1220337f47fSAndreas Gohr * @throws Exception 1230337f47fSAndreas Gohr */ 1240337f47fSAndreas Gohr public function askChatQuestion($question, $history = []) 1250337f47fSAndreas Gohr { 1260337f47fSAndreas Gohr if ($history) { 1270337f47fSAndreas Gohr $standaloneQuestion = $this->rephraseChatQuestion($question, $history); 1280337f47fSAndreas Gohr } else { 1290337f47fSAndreas Gohr $standaloneQuestion = $question; 1300337f47fSAndreas Gohr } 1310337f47fSAndreas Gohr return $this->askQuestion($standaloneQuestion); 1320337f47fSAndreas Gohr } 1330337f47fSAndreas Gohr 1340337f47fSAndreas Gohr /** 1350337f47fSAndreas Gohr * Ask a single standalone question 1360337f47fSAndreas Gohr * 1370337f47fSAndreas Gohr * @param string $question 1380337f47fSAndreas Gohr * @return array ['question' => $question, 'answer' => $answer, 'sources' => $sources] 1390337f47fSAndreas Gohr * @throws Exception 1400337f47fSAndreas Gohr */ 1410337f47fSAndreas Gohr public function askQuestion($question) 1420337f47fSAndreas Gohr { 1439f6b34c4SAndreas Gohr $similar = $this->getEmbeddings()->getSimilarChunks($question); 1449e81bea7SAndreas Gohr if ($similar) { 14555392016SAndreas Gohr $context = implode("\n", array_map(function (Chunk $chunk) { 14668908844SAndreas Gohr return "\n```\n" . $chunk->getText() . "\n```\n"; 14755392016SAndreas Gohr }, $similar)); 1480337f47fSAndreas Gohr $prompt = $this->getPrompt('question', ['context' => $context]); 1499e81bea7SAndreas Gohr } else { 1509e81bea7SAndreas Gohr $prompt = $this->getPrompt('noanswer'); 1519e81bea7SAndreas Gohr } 15268908844SAndreas Gohr 1530337f47fSAndreas Gohr $messages = [ 1540337f47fSAndreas Gohr [ 1550337f47fSAndreas Gohr 'role' => 'system', 1560337f47fSAndreas Gohr 'content' => $prompt 1570337f47fSAndreas Gohr ], 1580337f47fSAndreas Gohr [ 1590337f47fSAndreas Gohr 'role' => 'user', 1600337f47fSAndreas Gohr 'content' => $question 1610337f47fSAndreas Gohr ] 1620337f47fSAndreas Gohr ]; 1630337f47fSAndreas Gohr 1649f6b34c4SAndreas Gohr $answer = $this->getModel()->getAnswer($messages); 1650337f47fSAndreas Gohr 1660337f47fSAndreas Gohr return [ 1670337f47fSAndreas Gohr 'question' => $question, 1680337f47fSAndreas Gohr 'answer' => $answer, 1690337f47fSAndreas Gohr 'sources' => $similar, 1700337f47fSAndreas Gohr ]; 1710337f47fSAndreas Gohr } 1720337f47fSAndreas Gohr 1730337f47fSAndreas Gohr /** 1740337f47fSAndreas Gohr * Rephrase a question into a standalone question based on the chat history 1750337f47fSAndreas Gohr * 1760337f47fSAndreas Gohr * @param string $question The original user question 1770337f47fSAndreas Gohr * @param array[] $history The chat history [[user, ai], [user, ai], ...] 1780337f47fSAndreas Gohr * @return string The rephrased question 1790337f47fSAndreas Gohr * @throws Exception 1800337f47fSAndreas Gohr */ 1810337f47fSAndreas Gohr public function rephraseChatQuestion($question, $history) 1820337f47fSAndreas Gohr { 1830337f47fSAndreas Gohr // go back in history as far as possible without hitting the token limit 1840337f47fSAndreas Gohr $chatHistory = ''; 1850337f47fSAndreas Gohr $history = array_reverse($history); 1860337f47fSAndreas Gohr foreach ($history as $row) { 187f6ef2e50SAndreas Gohr if ( 1889f6b34c4SAndreas Gohr count($this->getEmbeddings()->getTokenEncoder()->encode($chatHistory)) > 1899f6b34c4SAndreas Gohr $this->getModel()->getMaxRephrasingTokenLength() 190f6ef2e50SAndreas Gohr ) { 1910337f47fSAndreas Gohr break; 1920337f47fSAndreas Gohr } 1930337f47fSAndreas Gohr 1940337f47fSAndreas Gohr $chatHistory = 1950337f47fSAndreas Gohr "Human: " . $row[0] . "\n" . 1960337f47fSAndreas Gohr "Assistant: " . $row[1] . "\n" . 1970337f47fSAndreas Gohr $chatHistory; 1980337f47fSAndreas Gohr } 1990337f47fSAndreas Gohr 2000337f47fSAndreas Gohr // ask openAI to rephrase the question 2010337f47fSAndreas Gohr $prompt = $this->getPrompt('rephrase', ['history' => $chatHistory, 'question' => $question]); 2020337f47fSAndreas Gohr $messages = [['role' => 'user', 'content' => $prompt]]; 2039f6b34c4SAndreas Gohr return $this->getModel()->getRephrasedQuestion($messages); 2040337f47fSAndreas Gohr } 2050337f47fSAndreas Gohr 2060337f47fSAndreas Gohr /** 2070337f47fSAndreas Gohr * Load the given prompt template and fill in the variables 2080337f47fSAndreas Gohr * 2090337f47fSAndreas Gohr * @param string $type 2100337f47fSAndreas Gohr * @param string[] $vars 2110337f47fSAndreas Gohr * @return string 2120337f47fSAndreas Gohr */ 2130337f47fSAndreas Gohr protected function getPrompt($type, $vars = []) 2140337f47fSAndreas Gohr { 2150337f47fSAndreas Gohr $template = file_get_contents($this->localFN('prompt_' . $type)); 2160337f47fSAndreas Gohr 2170337f47fSAndreas Gohr $replace = array(); 2180337f47fSAndreas Gohr foreach ($vars as $key => $val) { 2190337f47fSAndreas Gohr $replace['{{' . strtoupper($key) . '}}'] = $val; 2200337f47fSAndreas Gohr } 2210337f47fSAndreas Gohr 2220337f47fSAndreas Gohr return strtr($template, $replace); 2230337f47fSAndreas Gohr } 2240337f47fSAndreas Gohr} 2250337f47fSAndreas Gohr 226