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