Skip to content

Commit 0de7e02

Browse files
committed
mechanisms to override things on command line
This should help with debugging/evaluating
1 parent 3d64482 commit 0de7e02

File tree

7 files changed

+121
-45
lines changed

7 files changed

+121
-45
lines changed

AbstractCLI.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace dokuwiki\plugin\aichat;
4+
5+
use splitbrain\phpcli\Options;
6+
7+
abstract class AbstractCLI extends \dokuwiki\Extension\CLIPlugin
8+
{
9+
/** @var \helper_plugin_aichat */
10+
protected $helper;
11+
12+
/** @inheritdoc */
13+
public function __construct($autocatch = true)
14+
{
15+
parent::__construct($autocatch);
16+
$this->helper = plugin_load('helper', 'aichat');
17+
$this->helper->setLogger($this);
18+
$this->loadConfig();
19+
ini_set('memory_limit', -1);
20+
}
21+
22+
/** @inheritdoc */
23+
protected function setup(Options $options)
24+
{
25+
$options->useCompactHelp();
26+
27+
$options->registerOption(
28+
'lang',
29+
'When set to a language code, it overrides the the lang and preferUIlanguage settings and asks the ' .
30+
'bot to always use this language instead. ' .
31+
'When set to "auto" the bot is asked to detect the language of the input falling back to the wiki lang.',
32+
'',
33+
'lang'
34+
);
35+
}
36+
37+
/** @inheritDoc */
38+
protected function main(Options $options)
39+
{
40+
if ($this->loglevel['debug']['enabled']) {
41+
$this->helper->factory->setDebug(true);
42+
}
43+
44+
$lc = $options->getOpt('lang');
45+
if ($lc === 'auto') {
46+
$this->helper->updateConfig(['preferUIlanguage' => 0]);
47+
} else if ($lc) {
48+
$this->helper->updateConfig(['preferUIlanguage' => 1]);
49+
global $conf;
50+
$conf['lang'] = $lc;
51+
}
52+
53+
}
54+
}

ModelFactory.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ public function __construct(array $config)
2424
$this->config = $config;
2525
}
2626

27+
/**
28+
* Update the configuration and reset the cached models
29+
*
30+
* @param array $config The new (partial) configuration
31+
*/
32+
public function updateConfig(array $config)
33+
{
34+
$this->config = array_merge($this->config, $config);
35+
$this->chatModel = null;
36+
$this->rephraseModel = null;
37+
$this->embeddingModel = null;
38+
}
39+
2740
/**
2841
* Set the debug flag for all models
2942
*
@@ -49,6 +62,7 @@ public function getChatModel()
4962
return $this->chatModel;
5063
}
5164
$this->chatModel = $this->loadModel('chat', $this->config['chatmodel']);
65+
$this->chatModel->setDebug($this->debug);
5266
return $this->chatModel;
5367
}
5468

@@ -64,6 +78,7 @@ public function getRephraseModel()
6478
return $this->rephraseModel;
6579
}
6680
$this->rephraseModel = $this->loadModel('chat', $this->config['chatmodel']);
81+
$this->rephraseModel->setDebug($this->debug);
6782
return $this->rephraseModel;
6883
}
6984

@@ -78,6 +93,7 @@ public function getEmbeddingModel()
7893
return $this->embeddingModel;
7994
}
8095
$this->embeddingModel = $this->loadModel('embedding', $this->config['embedmodel']);
96+
$this->embeddingModel->setDebug($this->debug);
8197
return $this->embeddingModel;
8298
}
8399

cli.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use dokuwiki\Extension\CLIPlugin;
4+
use dokuwiki\plugin\aichat\AbstractCLI;
45
use dokuwiki\plugin\aichat\Chunk;
56
use dokuwiki\plugin\aichat\ModelFactory;
67
use dokuwiki\Search\Indexer;
@@ -14,30 +15,28 @@
1415
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
1516
* @author Andreas Gohr <gohr@cosmocode.de>
1617
*/
17-
class cli_plugin_aichat extends CLIPlugin
18+
class cli_plugin_aichat extends AbstractCLI
1819
{
1920
/** @var helper_plugin_aichat */
2021
protected $helper;
2122

22-
/** @inheritdoc */
23-
public function __construct($autocatch = true)
24-
{
25-
parent::__construct($autocatch);
26-
$this->helper = plugin_load('helper', 'aichat');
27-
$this->helper->setLogger($this);
28-
$this->loadConfig();
29-
}
30-
3123
/** @inheritDoc */
3224
protected function setup(Options $options)
3325
{
34-
$options->useCompactHelp();
26+
parent::setup($options);
3527

3628
$options->setHelp(
3729
'Manage and query the AI chatbot data. Please note that calls to your LLM provider will be made. ' .
3830
'This may incur costs.'
3931
);
4032

33+
$options->registerOption(
34+
'model',
35+
'Overrides the chat and rephrasing model settings and uses this model instead',
36+
'',
37+
'model'
38+
);
39+
4140
$options->registerCommand(
4241
'embed',
4342
'Create embeddings for all pages. This skips pages that already have embeddings'
@@ -80,11 +79,15 @@ protected function setup(Options $options)
8079
/** @inheritDoc */
8180
protected function main(Options $options)
8281
{
83-
if ($this->loglevel['debug']['enabled']) {
84-
$this->helper->factory->setDebug(true);
82+
parent::main($options);
83+
84+
$model = $options->getOpt('model');
85+
if($model) {
86+
$this->helper->updateConfig(
87+
['chatmodel' => $model, 'rephasemodel' => $model]
88+
);
8589
}
8690

87-
ini_set('memory_limit', -1);
8891
switch ($options->getCmd()) {
8992
case 'embed':
9093
$this->createEmbeddings($options->getOpt('clear'));

cli/simulate.php

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use dokuwiki\Extension\CLIPlugin;
4+
use dokuwiki\plugin\aichat\AbstractCLI;
45
use dokuwiki\plugin\aichat\ModelFactory;
56
use splitbrain\phpcli\Colors;
67
use splitbrain\phpcli\Options;
@@ -11,25 +12,16 @@
1112
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
1213
* @author Andreas Gohr <gohr@cosmocode.de>
1314
*/
14-
class cli_plugin_aichat_simulate extends CLIPlugin
15+
class cli_plugin_aichat_simulate extends AbstractCLI
1516
{
16-
/** @var helper_plugin_aichat */
17-
protected $helper;
18-
19-
/** @inheritdoc */
20-
public function __construct($autocatch = true)
21-
{
22-
parent::__construct($autocatch);
23-
$this->helper = plugin_load('helper', 'aichat');
24-
$this->helper->setLogger($this);
25-
$this->loadConfig();
26-
}
2717

2818

2919
/** @inheritDoc */
3020
protected function setup(Options $options)
3121
{
32-
$options->setHelp('Run a prpared chat session against multiple models');
22+
parent::setup($options);
23+
24+
$options->setHelp('Run a prepared chat session against multiple models');
3325
$options->registerArgument('input', 'A file with the chat questions. Each question separated by two newlines');
3426
$options->registerArgument('output', 'Where to write the result CSV to');
3527

@@ -44,14 +36,12 @@ protected function setup(Options $options)
4436
/** @inheritDoc */
4537
protected function main(Options $options)
4638
{
47-
if ($this->loglevel['debug']['enabled']) {
48-
$this->helper->factory->setDebug(true);
49-
}
39+
parent::main($options);
5040

5141
[$input, $output] = $options->getArgs();
5242
$questions = $this->readInputFile($input);
53-
$outfh = @fopen($output, 'w');
54-
if (!$outfh) throw new \Exception("Could not open $output for writing");
43+
$outFH = @fopen($output, 'w');
44+
if (!$outFH) throw new \Exception("Could not open $output for writing");
5545

5646
$models = $this->helper->factory->getModels(true, 'chat');
5747

@@ -65,9 +55,9 @@ protected function main(Options $options)
6555
}
6656

6757
foreach ($this->records2rows($results) as $row) {
68-
fputcsv($outfh, $row);
58+
fputcsv($outFH, $row);
6959
}
70-
fclose($outfh);
60+
fclose($outFH);
7161
$this->success("Results written to $output");
7262
}
7363

@@ -88,6 +78,7 @@ protected function simulate($questions, $model)
8878
$this->colors->ptln($q, Colors::C_LIGHTPURPLE);
8979
$result = $this->helper->askChatQuestion($q, $history);
9080
$history[] = [$result['question'], $result['answer']];
81+
$this->colors->ptln($result['question'], Colors::C_LIGHTBLUE);
9182

9283
$record = [
9384
'question' => $q,

helper.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ public function setLogger($logger)
5656
$this->logger = $logger;
5757
}
5858

59+
/**
60+
* Update the configuration
61+
*
62+
* @param array $config
63+
* @return void
64+
*/
65+
public function updateConfig(array $config)
66+
{
67+
$this->conf = array_merge($this->conf, $config);
68+
$this->factory->updateConfig($config);
69+
}
70+
5971
/**
6072
* Check if the current user is allowed to use the plugin (if it has been restricted)
6173
*
@@ -240,10 +252,11 @@ public function rephraseChatQuestion($question, $history)
240252
*/
241253
protected function prepareMessages(
242254
ChatInterface $model,
243-
string $promptedQuestion,
244-
array $history,
245-
int $historySize
246-
): array {
255+
string $promptedQuestion,
256+
array $history,
257+
int $historySize
258+
): array
259+
{
247260
// calculate the space for context
248261
$remainingContext = $model->getMaxInputTokenLength();
249262
$remainingContext -= $this->countTokens($promptedQuestion);

lang/en/question.prompt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
Use the following documents as context to answer the users question.
2-
If you don't know the answer, just say that you don't know, don't try to make up an answer.
3-
{{LANGUAGE}}
1+
Your Task: Use the following documents as context to answer the users question. If you don't know the answer, just say that you don't know, don't try to make up an answer. {{LANGUAGE}}
2+
43
----------------
54
{{CONTEXT}}
65
----------------
76

87
User Question: {{QUESTION}}
8+
99
Your Reply:

lang/en/rephrase.prompt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
Given the previous conversation and the users follow-up question, rephrase the user's follow-up question to be a standalone question that is understandable without the previous context.
2-
{{LANGUAGE}}
3-
Only reply with the rephrased question, do not answer it.
1+
Your Task: Given the previous conversation and the users follow-up question, rephrase the user's follow-up question to be a standalone question that is understandable without the previous context. {{LANGUAGE}} Only reply with the rephrased question, do not answer it.
2+
3+
User Follow-up question: {{QUESTION}}
44

5-
Follow-up question: {{QUESTION}}
65
Standalone question:

0 commit comments

Comments
 (0)