1c2b7a1f7SAndreas Gohr<?php 2c2b7a1f7SAndreas Gohr 32071dcedSAndreas Gohruse dokuwiki\Extension\CLIPlugin; 4*0de7e020SAndreas Gohruse dokuwiki\plugin\aichat\AbstractCLI; 5c2b7a1f7SAndreas Gohruse dokuwiki\plugin\aichat\ModelFactory; 6c2b7a1f7SAndreas Gohruse splitbrain\phpcli\Colors; 7c2b7a1f7SAndreas Gohruse splitbrain\phpcli\Options; 8c2b7a1f7SAndreas Gohr 9c2b7a1f7SAndreas Gohr/** 10c2b7a1f7SAndreas Gohr * DokuWiki Plugin aichat (CLI Component) 11c2b7a1f7SAndreas Gohr * 12c2b7a1f7SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 13c2b7a1f7SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 14c2b7a1f7SAndreas Gohr */ 15*0de7e020SAndreas Gohrclass cli_plugin_aichat_simulate extends AbstractCLI 16c2b7a1f7SAndreas Gohr{ 17c2b7a1f7SAndreas Gohr 18c2b7a1f7SAndreas Gohr 19c2b7a1f7SAndreas Gohr /** @inheritDoc */ 20c2b7a1f7SAndreas Gohr protected function setup(Options $options) 21c2b7a1f7SAndreas Gohr { 22*0de7e020SAndreas Gohr parent::setup($options); 23*0de7e020SAndreas Gohr 24*0de7e020SAndreas Gohr $options->setHelp('Run a prepared chat session against multiple models'); 25c2b7a1f7SAndreas Gohr $options->registerArgument('input', 'A file with the chat questions. Each question separated by two newlines'); 26c2b7a1f7SAndreas Gohr $options->registerArgument('output', 'Where to write the result CSV to'); 27c2b7a1f7SAndreas Gohr 28c2b7a1f7SAndreas Gohr $options->registerOption( 29c2b7a1f7SAndreas Gohr 'filter', 30c2b7a1f7SAndreas Gohr 'Use only models matching this case-insensitive regex (no delimiters)', 31c2b7a1f7SAndreas Gohr 'f', 32c2b7a1f7SAndreas Gohr 'regex' 33c2b7a1f7SAndreas Gohr ); 34c2b7a1f7SAndreas Gohr } 35c2b7a1f7SAndreas Gohr 36c2b7a1f7SAndreas Gohr /** @inheritDoc */ 37c2b7a1f7SAndreas Gohr protected function main(Options $options) 38c2b7a1f7SAndreas Gohr { 39*0de7e020SAndreas Gohr parent::main($options); 40c2b7a1f7SAndreas Gohr 41c2b7a1f7SAndreas Gohr [$input, $output] = $options->getArgs(); 42c2b7a1f7SAndreas Gohr $questions = $this->readInputFile($input); 43*0de7e020SAndreas Gohr $outFH = @fopen($output, 'w'); 44*0de7e020SAndreas Gohr if (!$outFH) throw new \Exception("Could not open $output for writing"); 45c2b7a1f7SAndreas Gohr 46c2b7a1f7SAndreas Gohr $models = $this->helper->factory->getModels(true, 'chat'); 47c2b7a1f7SAndreas Gohr 48c2b7a1f7SAndreas Gohr $results = []; 49c2b7a1f7SAndreas Gohr foreach ($models as $name => $info) { 50c2b7a1f7SAndreas Gohr if ($options->getOpt('filter') && !preg_match('/' . $options->getOpt('filter') . '/i', $name)) { 51c2b7a1f7SAndreas Gohr continue; 52c2b7a1f7SAndreas Gohr } 53c2b7a1f7SAndreas Gohr $this->success("Running on $name..."); 54c2b7a1f7SAndreas Gohr $results[$name] = $this->simulate($questions, $info); 55c2b7a1f7SAndreas Gohr } 56c2b7a1f7SAndreas Gohr 57c2b7a1f7SAndreas Gohr foreach ($this->records2rows($results) as $row) { 58*0de7e020SAndreas Gohr fputcsv($outFH, $row); 59c2b7a1f7SAndreas Gohr } 60*0de7e020SAndreas Gohr fclose($outFH); 61c2b7a1f7SAndreas Gohr $this->success("Results written to $output"); 62c2b7a1f7SAndreas Gohr } 63c2b7a1f7SAndreas Gohr 64c2b7a1f7SAndreas Gohr protected function simulate($questions, $model) 65c2b7a1f7SAndreas Gohr { 66c2b7a1f7SAndreas Gohr // override models 67c2b7a1f7SAndreas Gohr $this->helper->factory->chatModel = $model['instance']; 68c2b7a1f7SAndreas Gohr $this->helper->factory->rephraseModel = clone $model['instance']; 69c2b7a1f7SAndreas Gohr 70c2b7a1f7SAndreas Gohr $records = []; 71c2b7a1f7SAndreas Gohr 72c2b7a1f7SAndreas Gohr $history = []; 73c2b7a1f7SAndreas Gohr foreach ($questions as $q) { 74c2b7a1f7SAndreas Gohr $this->helper->getChatModel()->resetUsageStats(); 75c2b7a1f7SAndreas Gohr $this->helper->getRephraseModel()->resetUsageStats(); 76c2b7a1f7SAndreas Gohr $this->helper->getEmbeddingModel()->resetUsageStats(); 77c2b7a1f7SAndreas Gohr 78c2b7a1f7SAndreas Gohr $this->colors->ptln($q, Colors::C_LIGHTPURPLE); 79c2b7a1f7SAndreas Gohr $result = $this->helper->askChatQuestion($q, $history); 80c2b7a1f7SAndreas Gohr $history[] = [$result['question'], $result['answer']]; 81*0de7e020SAndreas Gohr $this->colors->ptln($result['question'], Colors::C_LIGHTBLUE); 82c2b7a1f7SAndreas Gohr 83c2b7a1f7SAndreas Gohr $record = [ 84c2b7a1f7SAndreas Gohr 'question' => $q, 85c2b7a1f7SAndreas Gohr 'rephrased' => $result['question'], 86c2b7a1f7SAndreas Gohr 'answer' => $result['answer'], 872071dcedSAndreas Gohr 'source.list' => implode("\n", $result['sources']), 88c2b7a1f7SAndreas Gohr 'source.time' => $this->helper->getEmbeddings()->timeSpent, 89c2b7a1f7SAndreas Gohr ...$this->flattenStats('stats.embedding', $this->helper->getEmbeddingModel()->getUsageStats()), 90c2b7a1f7SAndreas Gohr ...$this->flattenStats('stats.rephrase', $this->helper->getRephraseModel()->getUsageStats()), 91c2b7a1f7SAndreas Gohr ...$this->flattenStats('stats.chat', $this->helper->getChatModel()->getUsageStats()), 92c2b7a1f7SAndreas Gohr ]; 93c2b7a1f7SAndreas Gohr $records[] = $record; 94c2b7a1f7SAndreas Gohr $this->colors->ptln($result['answer'], Colors::C_LIGHTCYAN); 95c2b7a1f7SAndreas Gohr } 96c2b7a1f7SAndreas Gohr 97c2b7a1f7SAndreas Gohr return $records; 98c2b7a1f7SAndreas Gohr } 99c2b7a1f7SAndreas Gohr 100c2b7a1f7SAndreas Gohr /** 101c2b7a1f7SAndreas Gohr * Reformat the result array into a CSV friendly array 102c2b7a1f7SAndreas Gohr */ 103c2b7a1f7SAndreas Gohr protected function records2rows(array $result): array 104c2b7a1f7SAndreas Gohr { 105c2b7a1f7SAndreas Gohr $rowkeys = [ 106c2b7a1f7SAndreas Gohr 'question' => ['question', 'stats.embedding.cost', 'stats.embedding.time'], 107c2b7a1f7SAndreas Gohr 'rephrased' => ['rephrased', 'stats.rephrase.cost', 'stats.rephrase.time'], 108c2b7a1f7SAndreas Gohr 'sources' => ['source.list', '', 'source.time'], 109c2b7a1f7SAndreas Gohr 'answer' => ['answer', 'stats.chat.cost', 'stats.chat.time'], 110c2b7a1f7SAndreas Gohr ]; 111c2b7a1f7SAndreas Gohr 112c2b7a1f7SAndreas Gohr $models = array_keys($result); 113c2b7a1f7SAndreas Gohr $numberOfRecords = count($result[$models[0]]); 114c2b7a1f7SAndreas Gohr $rows = []; 115c2b7a1f7SAndreas Gohr 116c2b7a1f7SAndreas Gohr // write headers 117c2b7a1f7SAndreas Gohr $row = []; 118c2b7a1f7SAndreas Gohr $row[] = 'type'; 119c2b7a1f7SAndreas Gohr foreach ($models as $model) { 120c2b7a1f7SAndreas Gohr $row[] = $model; 121c2b7a1f7SAndreas Gohr $row[] = 'Cost USD'; 122c2b7a1f7SAndreas Gohr $row[] = 'Time s'; 123c2b7a1f7SAndreas Gohr } 124c2b7a1f7SAndreas Gohr $rows[] = $row; 125c2b7a1f7SAndreas Gohr 126c2b7a1f7SAndreas Gohr // write rows 127c2b7a1f7SAndreas Gohr for ($i = 0; $i < $numberOfRecords; $i++) { 128c2b7a1f7SAndreas Gohr foreach ($rowkeys as $type => $keys) { 129c2b7a1f7SAndreas Gohr $row = []; 130c2b7a1f7SAndreas Gohr $row[] = $type; 131c2b7a1f7SAndreas Gohr foreach ($models as $model) { 132c2b7a1f7SAndreas Gohr foreach ($keys as $key) { 133c2b7a1f7SAndreas Gohr if ($key) { 134c2b7a1f7SAndreas Gohr $row[] = $result[$model][$i][$key]; 135c2b7a1f7SAndreas Gohr } else { 136c2b7a1f7SAndreas Gohr $row[] = ''; 137c2b7a1f7SAndreas Gohr } 138c2b7a1f7SAndreas Gohr } 139c2b7a1f7SAndreas Gohr } 140c2b7a1f7SAndreas Gohr $rows[] = $row; 141c2b7a1f7SAndreas Gohr } 142c2b7a1f7SAndreas Gohr } 143c2b7a1f7SAndreas Gohr 144c2b7a1f7SAndreas Gohr 145c2b7a1f7SAndreas Gohr return $rows; 146c2b7a1f7SAndreas Gohr } 147c2b7a1f7SAndreas Gohr 148c2b7a1f7SAndreas Gohr 149c2b7a1f7SAndreas Gohr /** 150c2b7a1f7SAndreas Gohr * Prefix each key in the given stats array to be merged with a larger array 151c2b7a1f7SAndreas Gohr * 152c2b7a1f7SAndreas Gohr * @param string $prefix 153c2b7a1f7SAndreas Gohr * @param array $stats 154c2b7a1f7SAndreas Gohr * @return array 155c2b7a1f7SAndreas Gohr */ 1562071dcedSAndreas Gohr protected function flattenStats(string $prefix, array $stats) 1572071dcedSAndreas Gohr { 158c2b7a1f7SAndreas Gohr $result = []; 159c2b7a1f7SAndreas Gohr foreach ($stats as $key => $value) { 160c2b7a1f7SAndreas Gohr $result["$prefix.$key"] = $value; 161c2b7a1f7SAndreas Gohr } 162c2b7a1f7SAndreas Gohr return $result; 163c2b7a1f7SAndreas Gohr } 164c2b7a1f7SAndreas Gohr 165c2b7a1f7SAndreas Gohr /** 166c2b7a1f7SAndreas Gohr * @param string $file 167c2b7a1f7SAndreas Gohr * @return array 168c2b7a1f7SAndreas Gohr * @throws Exception 169c2b7a1f7SAndreas Gohr */ 170c2b7a1f7SAndreas Gohr protected function readInputFile(string $file): array 171c2b7a1f7SAndreas Gohr { 172c2b7a1f7SAndreas Gohr if (!file_exists($file)) throw new \Exception("File not found: $file"); 173c2b7a1f7SAndreas Gohr $lines = file_get_contents($file); 174c2b7a1f7SAndreas Gohr $questions = explode("\n\n", $lines); 175c2b7a1f7SAndreas Gohr $questions = array_map('trim', $questions); 176c2b7a1f7SAndreas Gohr return $questions; 177c2b7a1f7SAndreas Gohr } 178c2b7a1f7SAndreas Gohr} 179