10337f47fSAndreas Gohr<?php 20337f47fSAndreas Gohr 3*55392016SAndreas 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 /** 300337f47fSAndreas Gohr * Access the OpenAI client 310337f47fSAndreas Gohr * 320337f47fSAndreas Gohr * @return OpenAI 330337f47fSAndreas Gohr */ 340337f47fSAndreas Gohr public function getOpenAI() 350337f47fSAndreas Gohr { 360337f47fSAndreas Gohr return $this->openAI; 370337f47fSAndreas Gohr } 380337f47fSAndreas Gohr 390337f47fSAndreas Gohr /** 400337f47fSAndreas Gohr * Access the Embeddings interface 410337f47fSAndreas Gohr * 420337f47fSAndreas Gohr * @return Embeddings 430337f47fSAndreas Gohr */ 440337f47fSAndreas Gohr public function getEmbeddings() 450337f47fSAndreas Gohr { 460337f47fSAndreas Gohr return $this->embeddings; 470337f47fSAndreas Gohr } 480337f47fSAndreas Gohr 490337f47fSAndreas Gohr /** 500337f47fSAndreas Gohr * Ask a question with a chat history 510337f47fSAndreas Gohr * 520337f47fSAndreas Gohr * @param string $question 530337f47fSAndreas Gohr * @param array[] $history The chat history [[user, ai], [user, ai], ...] 540337f47fSAndreas Gohr * @return array ['question' => $question, 'answer' => $answer, 'sources' => $sources] 550337f47fSAndreas Gohr * @throws Exception 560337f47fSAndreas Gohr */ 570337f47fSAndreas Gohr public function askChatQuestion($question, $history = []) 580337f47fSAndreas Gohr { 590337f47fSAndreas Gohr if ($history) { 600337f47fSAndreas Gohr $standaloneQuestion = $this->rephraseChatQuestion($question, $history); 610337f47fSAndreas Gohr } else { 620337f47fSAndreas Gohr $standaloneQuestion = $question; 630337f47fSAndreas Gohr } 640337f47fSAndreas Gohr return $this->askQuestion($standaloneQuestion); 650337f47fSAndreas Gohr } 660337f47fSAndreas Gohr 670337f47fSAndreas Gohr /** 680337f47fSAndreas Gohr * Ask a single standalone question 690337f47fSAndreas Gohr * 700337f47fSAndreas Gohr * @param string $question 710337f47fSAndreas Gohr * @return array ['question' => $question, 'answer' => $answer, 'sources' => $sources] 720337f47fSAndreas Gohr * @throws Exception 730337f47fSAndreas Gohr */ 740337f47fSAndreas Gohr public function askQuestion($question) 750337f47fSAndreas Gohr { 760337f47fSAndreas Gohr $similar = $this->embeddings->getSimilarChunks($question); 770337f47fSAndreas Gohr 789e81bea7SAndreas Gohr if ($similar) { 79*55392016SAndreas Gohr $context = implode("\n", array_map(function (Chunk $chunk) { 80*55392016SAndreas Gohr return $chunk->getText(); 81*55392016SAndreas Gohr }, $similar)); 820337f47fSAndreas Gohr $prompt = $this->getPrompt('question', ['context' => $context]); 839e81bea7SAndreas Gohr } else { 849e81bea7SAndreas Gohr $prompt = $this->getPrompt('noanswer'); 859e81bea7SAndreas Gohr } 860337f47fSAndreas Gohr $messages = [ 870337f47fSAndreas Gohr [ 880337f47fSAndreas Gohr 'role' => 'system', 890337f47fSAndreas Gohr 'content' => $prompt 900337f47fSAndreas Gohr ], 910337f47fSAndreas Gohr [ 920337f47fSAndreas Gohr 'role' => 'user', 930337f47fSAndreas Gohr 'content' => $question 940337f47fSAndreas Gohr ] 950337f47fSAndreas Gohr ]; 960337f47fSAndreas Gohr 970337f47fSAndreas Gohr $answer = $this->openAI->getChatAnswer($messages); 980337f47fSAndreas Gohr 990337f47fSAndreas Gohr return [ 1000337f47fSAndreas Gohr 'question' => $question, 1010337f47fSAndreas Gohr 'answer' => $answer, 1020337f47fSAndreas Gohr 'sources' => $similar, 1030337f47fSAndreas Gohr ]; 1040337f47fSAndreas Gohr } 1050337f47fSAndreas Gohr 1060337f47fSAndreas Gohr /** 1070337f47fSAndreas Gohr * Rephrase a question into a standalone question based on the chat history 1080337f47fSAndreas Gohr * 1090337f47fSAndreas Gohr * @param string $question The original user question 1100337f47fSAndreas Gohr * @param array[] $history The chat history [[user, ai], [user, ai], ...] 1110337f47fSAndreas Gohr * @return string The rephrased question 1120337f47fSAndreas Gohr * @throws Exception 1130337f47fSAndreas Gohr */ 1140337f47fSAndreas Gohr public function rephraseChatQuestion($question, $history) 1150337f47fSAndreas Gohr { 1160337f47fSAndreas Gohr // go back in history as far as possible without hitting the token limit 1170337f47fSAndreas Gohr $tiktok = new Encoder(); 1180337f47fSAndreas Gohr $chatHistory = ''; 1190337f47fSAndreas Gohr $history = array_reverse($history); 1200337f47fSAndreas Gohr foreach ($history as $row) { 1210337f47fSAndreas Gohr if (count($tiktok->encode($chatHistory)) > 3000) { 1220337f47fSAndreas Gohr break; 1230337f47fSAndreas Gohr } 1240337f47fSAndreas Gohr 1250337f47fSAndreas Gohr $chatHistory = 1260337f47fSAndreas Gohr "Human: " . $row[0] . "\n" . 1270337f47fSAndreas Gohr "Assistant: " . $row[1] . "\n" . 1280337f47fSAndreas Gohr $chatHistory; 1290337f47fSAndreas Gohr } 1300337f47fSAndreas Gohr 1310337f47fSAndreas Gohr // ask openAI to rephrase the question 1320337f47fSAndreas Gohr $prompt = $this->getPrompt('rephrase', ['history' => $chatHistory, 'question' => $question]); 1330337f47fSAndreas Gohr $messages = [['role' => 'user', 'content' => $prompt]]; 1340337f47fSAndreas Gohr return $this->openAI->getChatAnswer($messages); 1350337f47fSAndreas Gohr } 1360337f47fSAndreas Gohr 1370337f47fSAndreas Gohr /** 1380337f47fSAndreas Gohr * Load the given prompt template and fill in the variables 1390337f47fSAndreas Gohr * 1400337f47fSAndreas Gohr * @param string $type 1410337f47fSAndreas Gohr * @param string[] $vars 1420337f47fSAndreas Gohr * @return string 1430337f47fSAndreas Gohr */ 1440337f47fSAndreas Gohr protected function getPrompt($type, $vars = []) 1450337f47fSAndreas Gohr { 1460337f47fSAndreas Gohr $template = file_get_contents($this->localFN('prompt_' . $type)); 1470337f47fSAndreas Gohr 1480337f47fSAndreas Gohr $replace = array(); 1490337f47fSAndreas Gohr foreach ($vars as $key => $val) { 1500337f47fSAndreas Gohr $replace['{{' . strtoupper($key) . '}}'] = $val; 1510337f47fSAndreas Gohr } 1520337f47fSAndreas Gohr 1530337f47fSAndreas Gohr return strtr($template, $replace); 1540337f47fSAndreas Gohr } 1550337f47fSAndreas Gohr} 1560337f47fSAndreas Gohr 157