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