10337f47fSAndreas Gohr<?php 20337f47fSAndreas Gohr 30337f47fSAndreas Gohruse dokuwiki\plugin\aichat\Embeddings; 40337f47fSAndreas Gohruse dokuwiki\plugin\aichat\OpenAI; 50337f47fSAndreas Gohruse TikToken\Encoder; 60337f47fSAndreas Gohr 70337f47fSAndreas Gohrrequire_once __DIR__ . '/vendor/autoload.php'; 80337f47fSAndreas Gohr 90337f47fSAndreas Gohr/** 100337f47fSAndreas Gohr * DokuWiki Plugin aichat (Helper Component) 110337f47fSAndreas Gohr * 120337f47fSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 130337f47fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 140337f47fSAndreas Gohr */ 150337f47fSAndreas Gohrclass helper_plugin_aichat extends \dokuwiki\Extension\Plugin 160337f47fSAndreas Gohr{ 170337f47fSAndreas Gohr /** @var OpenAI */ 180337f47fSAndreas Gohr protected $openAI; 190337f47fSAndreas Gohr /** @var Embeddings */ 200337f47fSAndreas Gohr protected $embeddings; 210337f47fSAndreas Gohr 220337f47fSAndreas Gohr public function __construct() 230337f47fSAndreas Gohr { 240337f47fSAndreas Gohr $this->openAI = new OpenAI($this->getConf('openaikey'), $this->getConf('openaiorg')); 250337f47fSAndreas Gohr $this->embeddings = new Embeddings($this->openAI); 260337f47fSAndreas Gohr } 270337f47fSAndreas Gohr 280337f47fSAndreas Gohr /** 290337f47fSAndreas Gohr * Access the OpenAI client 300337f47fSAndreas Gohr * 310337f47fSAndreas Gohr * @return OpenAI 320337f47fSAndreas Gohr */ 330337f47fSAndreas Gohr public function getOpenAI() 340337f47fSAndreas Gohr { 350337f47fSAndreas Gohr return $this->openAI; 360337f47fSAndreas Gohr } 370337f47fSAndreas Gohr 380337f47fSAndreas Gohr /** 390337f47fSAndreas Gohr * Access the Embeddings interface 400337f47fSAndreas Gohr * 410337f47fSAndreas Gohr * @return Embeddings 420337f47fSAndreas Gohr */ 430337f47fSAndreas Gohr public function getEmbeddings() 440337f47fSAndreas Gohr { 450337f47fSAndreas Gohr return $this->embeddings; 460337f47fSAndreas Gohr } 470337f47fSAndreas Gohr 480337f47fSAndreas Gohr /** 490337f47fSAndreas Gohr * Ask a question with a chat history 500337f47fSAndreas Gohr * 510337f47fSAndreas Gohr * @param string $question 520337f47fSAndreas Gohr * @param array[] $history The chat history [[user, ai], [user, ai], ...] 530337f47fSAndreas Gohr * @return array ['question' => $question, 'answer' => $answer, 'sources' => $sources] 540337f47fSAndreas Gohr * @throws Exception 550337f47fSAndreas Gohr */ 560337f47fSAndreas Gohr public function askChatQuestion($question, $history = []) 570337f47fSAndreas Gohr { 580337f47fSAndreas Gohr if ($history) { 590337f47fSAndreas Gohr $standaloneQuestion = $this->rephraseChatQuestion($question, $history); 600337f47fSAndreas Gohr } else { 610337f47fSAndreas Gohr $standaloneQuestion = $question; 620337f47fSAndreas Gohr } 630337f47fSAndreas Gohr return $this->askQuestion($standaloneQuestion); 640337f47fSAndreas Gohr } 650337f47fSAndreas Gohr 660337f47fSAndreas Gohr /** 670337f47fSAndreas Gohr * Ask a single standalone question 680337f47fSAndreas Gohr * 690337f47fSAndreas Gohr * @param string $question 700337f47fSAndreas Gohr * @return array ['question' => $question, 'answer' => $answer, 'sources' => $sources] 710337f47fSAndreas Gohr * @throws Exception 720337f47fSAndreas Gohr */ 730337f47fSAndreas Gohr public function askQuestion($question) 740337f47fSAndreas Gohr { 750337f47fSAndreas Gohr $similar = $this->embeddings->getSimilarChunks($question); 760337f47fSAndreas Gohr 77*9e81bea7SAndreas Gohr if ($similar) { 78*9e81bea7SAndreas Gohr $context = implode("\n", array_column($similar, 'text')); 790337f47fSAndreas Gohr $prompt = $this->getPrompt('question', ['context' => $context]); 80*9e81bea7SAndreas Gohr } else { 81*9e81bea7SAndreas Gohr $prompt = $this->getPrompt('noanswer'); 82*9e81bea7SAndreas Gohr } 830337f47fSAndreas Gohr $messages = [ 840337f47fSAndreas Gohr [ 850337f47fSAndreas Gohr 'role' => 'system', 860337f47fSAndreas Gohr 'content' => $prompt 870337f47fSAndreas Gohr ], 880337f47fSAndreas Gohr [ 890337f47fSAndreas Gohr 'role' => 'user', 900337f47fSAndreas Gohr 'content' => $question 910337f47fSAndreas Gohr ] 920337f47fSAndreas Gohr ]; 930337f47fSAndreas Gohr 940337f47fSAndreas Gohr $answer = $this->openAI->getChatAnswer($messages); 950337f47fSAndreas Gohr 960337f47fSAndreas Gohr return [ 970337f47fSAndreas Gohr 'question' => $question, 980337f47fSAndreas Gohr 'answer' => $answer, 990337f47fSAndreas Gohr 'sources' => $similar, 1000337f47fSAndreas Gohr ]; 1010337f47fSAndreas Gohr } 1020337f47fSAndreas Gohr 1030337f47fSAndreas Gohr /** 1040337f47fSAndreas Gohr * Rephrase a question into a standalone question based on the chat history 1050337f47fSAndreas Gohr * 1060337f47fSAndreas Gohr * @param string $question The original user question 1070337f47fSAndreas Gohr * @param array[] $history The chat history [[user, ai], [user, ai], ...] 1080337f47fSAndreas Gohr * @return string The rephrased question 1090337f47fSAndreas Gohr * @throws Exception 1100337f47fSAndreas Gohr */ 1110337f47fSAndreas Gohr public function rephraseChatQuestion($question, $history) 1120337f47fSAndreas Gohr { 1130337f47fSAndreas Gohr // go back in history as far as possible without hitting the token limit 1140337f47fSAndreas Gohr $tiktok = new Encoder(); 1150337f47fSAndreas Gohr $chatHistory = ''; 1160337f47fSAndreas Gohr $history = array_reverse($history); 1170337f47fSAndreas Gohr foreach ($history as $row) { 1180337f47fSAndreas Gohr if (count($tiktok->encode($chatHistory)) > 3000) { 1190337f47fSAndreas Gohr break; 1200337f47fSAndreas Gohr } 1210337f47fSAndreas Gohr 1220337f47fSAndreas Gohr $chatHistory = 1230337f47fSAndreas Gohr "Human: " . $row[0] . "\n" . 1240337f47fSAndreas Gohr "Assistant: " . $row[1] . "\n" . 1250337f47fSAndreas Gohr $chatHistory; 1260337f47fSAndreas Gohr } 1270337f47fSAndreas Gohr 1280337f47fSAndreas Gohr // ask openAI to rephrase the question 1290337f47fSAndreas Gohr $prompt = $this->getPrompt('rephrase', ['history' => $chatHistory, 'question' => $question]); 1300337f47fSAndreas Gohr $messages = [['role' => 'user', 'content' => $prompt]]; 1310337f47fSAndreas Gohr return $this->openAI->getChatAnswer($messages); 1320337f47fSAndreas Gohr } 1330337f47fSAndreas Gohr 1340337f47fSAndreas Gohr /** 1350337f47fSAndreas Gohr * Load the given prompt template and fill in the variables 1360337f47fSAndreas Gohr * 1370337f47fSAndreas Gohr * @param string $type 1380337f47fSAndreas Gohr * @param string[] $vars 1390337f47fSAndreas Gohr * @return string 1400337f47fSAndreas Gohr */ 1410337f47fSAndreas Gohr protected function getPrompt($type, $vars = []) 1420337f47fSAndreas Gohr { 1430337f47fSAndreas Gohr $template = file_get_contents($this->localFN('prompt_' . $type)); 1440337f47fSAndreas Gohr 1450337f47fSAndreas Gohr $replace = array(); 1460337f47fSAndreas Gohr foreach ($vars as $key => $val) { 1470337f47fSAndreas Gohr $replace['{{' . strtoupper($key) . '}}'] = $val; 1480337f47fSAndreas Gohr } 1490337f47fSAndreas Gohr 1500337f47fSAndreas Gohr return strtr($template, $replace); 1510337f47fSAndreas Gohr } 1520337f47fSAndreas Gohr} 1530337f47fSAndreas Gohr 154