xref: /plugin/aichat/cli.php (revision e8451b21b9ca52de6bf419c0f1a2020e9bcd5276)
1<?php
2
3use dokuwiki\Extension\CLIPlugin;
4use dokuwiki\plugin\aichat\Chunk;
5use dokuwiki\Search\Indexer;
6use splitbrain\phpcli\Colors;
7use splitbrain\phpcli\Options;
8use splitbrain\phpcli\TableFormatter;
9
10/**
11 * DokuWiki Plugin aichat (CLI Component)
12 *
13 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
14 * @author  Andreas Gohr <gohr@cosmocode.de>
15 */
16class cli_plugin_aichat extends CLIPlugin
17{
18    /** @var helper_plugin_aichat */
19    protected $helper;
20
21    public function __construct($autocatch = true)
22    {
23        parent::__construct($autocatch);
24        $this->helper = plugin_load('helper', 'aichat');
25        $this->helper->setLogger($this);
26    }
27
28    /** @inheritDoc */
29    protected function setup(Options $options)
30    {
31        $options->useCompactHelp();
32
33        $options->setHelp(
34            'Manage and query the AI chatbot data. Please note that calls to your LLM provider will be made. ' .
35            'This may incur costs.'
36        );
37
38        $options->registerCommand(
39            'embed',
40            'Create embeddings for all pages. This skips pages that already have embeddings'
41        );
42        $options->registerOption(
43            'clear',
44            'Clear all existing embeddings before creating new ones',
45            'c',
46            false,
47            'embed'
48        );
49
50        $options->registerCommand('maintenance', 'Run storage maintenance. Refer to the documentation for details.');
51
52        $options->registerCommand('similar', 'Search for similar pages');
53        $options->registerArgument('query', 'Look up chunks similar to this query', true, 'similar');
54
55        $options->registerCommand('ask', 'Ask a question');
56        $options->registerArgument('question', 'The question to ask', true, 'ask');
57
58        $options->registerCommand('chat', 'Start an interactive chat session');
59
60        $options->registerCommand('models', 'List available models');
61
62        $options->registerCommand('info', 'Get Info about the vector storage and other stats');
63
64        $options->registerCommand('split', 'Split a page into chunks (for debugging)');
65        $options->registerArgument('page', 'The page to split', true, 'split');
66
67        $options->registerCommand('page', 'Check if chunks for a given page are available (for debugging)');
68        $options->registerArgument('page', 'The page to check', true, 'page');
69        $options->registerOption('dump', 'Dump the chunks', 'd', false, 'page');
70
71        $options->registerCommand('tsv', 'Create TSV files for visualizing at http://projector.tensorflow.org/' .
72            ' Not supported on all storages.');
73        $options->registerArgument('vector.tsv', 'The vector file', false, 'tsv');
74        $options->registerArgument('meta.tsv', 'The meta file', false, 'tsv');
75    }
76
77    /** @inheritDoc */
78    protected function main(Options $options)
79    {
80        $this->loadConfig();
81        ini_set('memory_limit', -1);
82        switch ($options->getCmd()) {
83            case 'embed':
84                $this->createEmbeddings($options->getOpt('clear'));
85                break;
86            case 'maintenance':
87                $this->runMaintenance();
88                break;
89            case 'similar':
90                $this->similar($options->getArgs()[0]);
91                break;
92            case 'ask':
93                $this->ask($options->getArgs()[0]);
94                break;
95            case 'chat':
96                $this->chat();
97                break;
98            case 'models':
99                $this->models();
100                break;
101            case 'split':
102                $this->split($options->getArgs()[0]);
103                break;
104            case 'page':
105                $this->page($options->getArgs()[0], $options->getOpt('dump'));
106                break;
107            case 'info':
108                $this->showinfo();
109                break;
110            case 'tsv':
111                $args = $options->getArgs();
112                $vector = $args[0] ?? 'vector.tsv';
113                $meta = $args[1] ?? 'meta.tsv';
114                $this->tsv($vector, $meta);
115                break;
116            default:
117                echo $options->help();
118        }
119    }
120
121    /**
122     * @return void
123     */
124    protected function showinfo()
125    {
126        $stats = [
127            'model' => $this->getConf('model'),
128        ];
129        $stats = array_merge(
130            $stats,
131            array_map('dformat', $this->helper->getRunData()),
132            $this->helper->getStorage()->statistics()
133        );
134        $this->printTable($stats);
135    }
136
137    /**
138     * Print key value data as tabular data
139     *
140     * @param array $data
141     * @param int $level
142     * @return void
143     */
144    protected function printTable($data, $level = 0)
145    {
146        $tf = new TableFormatter($this->colors);
147        foreach ($data as $key => $value) {
148            if (is_array($value)) {
149                echo $tf->format(
150                    [$level * 2, 20, '*'],
151                    ['', $key, ''],
152                    [Colors::C_LIGHTBLUE, Colors::C_LIGHTBLUE, Colors::C_LIGHTBLUE]
153                );
154                $this->printTable($value, $level + 1);
155            } else {
156                echo $tf->format(
157                    [$level * 2, 20, '*'],
158                    ['', $key, $value],
159                    [Colors::C_LIGHTBLUE, Colors::C_LIGHTBLUE, Colors::C_LIGHTGRAY]
160                );
161            }
162        }
163    }
164
165    /**
166     * Check chunk availability for a given page
167     *
168     * @param string $page
169     * @return void
170     */
171    protected function page($page, $dump = false)
172    {
173        $indexer = new Indexer();
174        $pages = $indexer->getPages();
175        $pos = array_search(cleanID($page), $pages);
176
177        if ($pos === false) {
178            $this->error('Page not found');
179            return;
180        }
181
182        $storage = $this->helper->getStorage();
183        $chunks = $storage->getPageChunks($page, $pos * 100);
184        if ($chunks) {
185            $this->success('Found ' . count($chunks) . ' chunks');
186            if ($dump) {
187                echo json_encode($chunks, JSON_PRETTY_PRINT);
188            }
189        } else {
190            $this->error('No chunks found');
191        }
192    }
193
194    /**
195     * Split the given page into chunks and print them
196     *
197     * @param string $page
198     * @return void
199     * @throws Exception
200     */
201    protected function split($page)
202    {
203        $text = rawWiki($page);
204        $chunks = $this->helper->getEmbeddings()->splitIntoChunks($text);
205        foreach ($chunks as $chunk) {
206            echo $chunk;
207            echo "\n";
208            $this->colors->ptln('--------------------------------', Colors::C_LIGHTPURPLE);
209        }
210        $this->success('Split into ' . count($chunks) . ' chunks');
211    }
212
213    /**
214     * Interactive Chat Session
215     *
216     * @return void
217     * @throws Exception
218     */
219    protected function chat()
220    {
221        if ($this->loglevel['debug']['enabled']) {
222            $this->helper->getChatModel()->setDebug(true);
223        }
224
225        $history = [];
226        while ($q = $this->readLine('Your Question')) {
227            $this->helper->getChatModel()->resetUsageStats();
228            $result = $this->helper->askChatQuestion($q, $history);
229            $this->colors->ptln("Interpretation: {$result['question']}", Colors::C_LIGHTPURPLE);
230            $history[] = [$result['question'], $result['answer']];
231            $this->printAnswer($result);
232        }
233    }
234
235    protected function models()
236    {
237        $result = [
238            'chat' => [],
239            'embedding' => [],
240        ];
241
242
243        $jsons = glob(__DIR__ . '/Model/*/models.json');
244        foreach ($jsons as $json) {
245            $models = json_decode(file_get_contents($json), true);
246            foreach ($models as $type => $model) {
247                $namespace = basename(dirname($json));
248                foreach ($model as $name => $info) {
249
250
251                    $class = '\\dokuwiki\\plugin\\aichat\\Model\\' . $namespace . '\\' . ucfirst($type) . 'Model';
252                    try {
253                        new $class($name, $this->conf);
254                        $info['confok'] = true;
255                    } catch (Exception $e) {
256                        $info['confok'] = false;
257                    }
258
259                    $result[$type]["$namespace $name"] = $info;
260                }
261            }
262        }
263
264        $td = new TableFormatter($this->colors);
265        $cols = [30, 20, 20, '*'];
266        echo "==== Chat Models ====\n\n";
267        echo $td->format(
268            $cols,
269            ['Model', 'Token Limits', 'Price USD/M', 'Description'],
270            [Colors::C_LIGHTBLUE, Colors::C_LIGHTBLUE, Colors::C_LIGHTBLUE, Colors::C_LIGHTBLUE]
271        );
272        foreach ($result['chat'] as $name => $info) {
273            echo $td->format(
274                $cols,
275                [
276                    $name,
277                    sprintf(" In: %7d\nOut: %7d", $info['inputTokens'], $info['outputTokens']),
278                    sprintf(" In: %.2f\nOut: %.2f", $info['inputTokenPrice'], $info['inputTokenPrice']),
279                    $info['description']."\n"
280                ],
281                [
282                    $info['confok'] ? Colors::C_LIGHTGREEN : Colors::C_LIGHTRED,
283                ]
284            );
285        }
286
287        echo "==== Embedding Models ====\n\n";
288        echo $td->format(
289            $cols,
290            ['Model', 'Token Limits', 'Price USD/M', 'Description'],
291            [Colors::C_LIGHTBLUE, Colors::C_LIGHTBLUE, Colors::C_LIGHTBLUE, Colors::C_LIGHTBLUE]
292        );
293        foreach ($result['embedding'] as $name => $info) {
294            echo $td->format(
295                $cols,
296                [
297                    $name,
298                    sprintf("%7d", $info['inputTokens']),
299                    sprintf("%.2f", $info['inputTokenPrice']),
300                    $info['description']."\n"
301                ],
302                [
303                    $info['confok'] ? Colors::C_LIGHTGREEN : Colors::C_LIGHTRED,
304                ]
305            );
306        }
307
308        $this->colors->ptln('Current prices may differ', Colors::C_RED);
309    }
310
311    /**
312     * Handle a single, standalone question
313     *
314     * @param string $query
315     * @return void
316     * @throws Exception
317     */
318    protected function ask($query)
319    {
320        if ($this->loglevel['debug']['enabled']) {
321            $this->helper->getChatModel()->setDebug(true);
322        }
323
324        $result = $this->helper->askQuestion($query);
325        $this->printAnswer($result);
326    }
327
328    /**
329     * Get the pages that are similar to the query
330     *
331     * @param string $query
332     * @return void
333     */
334    protected function similar($query)
335    {
336        $langlimit = $this->helper->getLanguageLimit();
337        if ($langlimit) {
338            $this->info('Limiting results to {lang}', ['lang' => $langlimit]);
339        }
340
341        $sources = $this->helper->getEmbeddings()->getSimilarChunks($query, $langlimit);
342        $this->printSources($sources);
343    }
344
345    /**
346     * Run the maintenance tasks
347     *
348     * @return void
349     */
350    protected function runMaintenance()
351    {
352        $start = time();
353        $this->helper->getStorage()->runMaintenance();
354        $this->notice('Peak memory used: {memory}', ['memory' => filesize_h(memory_get_peak_usage(true))]);
355        $this->notice('Spent time: {time}min', ['time' => round((time() - $start) / 60, 2)]);
356
357        $data = $this->helper->getRunData();
358        $data['maintenance ran at'] = time();
359        $this->helper->setRunData($data);
360    }
361
362    /**
363     * Recreate chunks and embeddings for all pages
364     *
365     * @return void
366     */
367    protected function createEmbeddings($clear)
368    {
369        [$skipRE, $matchRE] = $this->getRegexps();
370
371        $start = time();
372        $this->helper->getEmbeddings()->createNewIndex($skipRE, $matchRE, $clear);
373        $this->notice('Peak memory used: {memory}', ['memory' => filesize_h(memory_get_peak_usage(true))]);
374        $this->notice('Spent time: {time}min', ['time' => round((time() - $start) / 60, 2)]);
375
376        $data = $this->helper->getRunData();
377        $data['embed ran at'] = time();
378        $this->helper->setRunData($data);
379    }
380
381    /**
382     * Dump TSV files for debugging
383     *
384     * @return void
385     */
386    protected function tsv($vector, $meta)
387    {
388
389        $storage = $this->helper->getStorage();
390        $storage->dumpTSV($vector, $meta);
391        $this->success('written to ' . $vector . ' and ' . $meta);
392    }
393
394    /**
395     * Print the given detailed answer in a nice way
396     *
397     * @param array $answer
398     * @return void
399     */
400    protected function printAnswer($answer)
401    {
402        $this->colors->ptln($answer['answer'], Colors::C_LIGHTCYAN);
403        echo "\n";
404        $this->printSources($answer['sources']);
405        echo "\n";
406        $this->printUsage();
407    }
408
409    /**
410     * Print the given sources
411     *
412     * @param Chunk[] $sources
413     * @return void
414     */
415    protected function printSources($sources)
416    {
417        foreach ($sources as $source) {
418            /** @var Chunk $source */
419            $this->colors->ptln(
420                "\t" . $source->getPage() . ' ' . $source->getId() . ' (' . $source->getScore() . ')',
421                Colors::C_LIGHTBLUE
422            );
423        }
424    }
425
426    /**
427     * Print the usage statistics for OpenAI
428     *
429     * @return void
430     */
431    protected function printUsage()
432    {
433        $this->info(
434            'Made {requests} requests in {time}s to Model. Used {tokens} tokens for about ${cost}.',
435            $this->helper->getChatModel()->getUsageStats()
436        );
437    }
438
439    /**
440     * Interactively ask for a value from the user
441     *
442     * @param string $prompt
443     * @return string
444     */
445    protected function readLine($prompt)
446    {
447        $value = '';
448
449        while ($value === '') {
450            echo $prompt;
451            echo ': ';
452
453            $fh = fopen('php://stdin', 'r');
454            $value = trim(fgets($fh));
455            fclose($fh);
456        }
457
458        return $value;
459    }
460
461    /**
462     * Read the skip and match regex from the config
463     *
464     * Ensures the regular expressions are valid
465     *
466     * @return string[] [$skipRE, $matchRE]
467     */
468    protected function getRegexps()
469    {
470        $skip = $this->getConf('skipRegex');
471        $skipRE = '';
472        $match = $this->getConf('matchRegex');
473        $matchRE = '';
474
475        if ($skip) {
476            $skipRE = '/' . $skip . '/';
477            if (@preg_match($skipRE, '') === false) {
478                $this->error(preg_last_error_msg());
479                $this->error('Invalid regular expression in $conf[\'skipRegex\']. Ignored.');
480                $skipRE = '';
481            } else {
482                $this->success('Skipping pages matching ' . $skipRE);
483            }
484        }
485
486        if ($match) {
487            $matchRE = '/' . $match . '/';
488            if (@preg_match($matchRE, '') === false) {
489                $this->error(preg_last_error_msg());
490                $this->error('Invalid regular expression in $conf[\'matchRegex\']. Ignored.');
491                $matchRE = '';
492            } else {
493                $this->success('Only indexing pages matching ' . $matchRE);
494            }
495        }
496        return [$skipRE, $matchRE];
497    }
498}
499