22
33use dokuwiki \Extension \CLIPlugin ;
44use dokuwiki \plugin \aichat \Chunk ;
5+ use dokuwiki \plugin \aichat \ModelFactory ;
56use dokuwiki \Search \Indexer ;
67use splitbrain \phpcli \Colors ;
78use splitbrain \phpcli \Options ;
@@ -18,11 +19,13 @@ class cli_plugin_aichat extends CLIPlugin
1819 /** @var helper_plugin_aichat */
1920 protected $ helper ;
2021
22+ /** @inheritdoc */
2123 public function __construct ($ autocatch = true )
2224 {
2325 parent ::__construct ($ autocatch );
2426 $ this ->helper = plugin_load ('helper ' , 'aichat ' );
2527 $ this ->helper ->setLogger ($ this );
28+ $ this ->loadConfig ();
2629 }
2730
2831 /** @inheritDoc */
@@ -77,7 +80,10 @@ protected function setup(Options $options)
7780 /** @inheritDoc */
7881 protected function main (Options $ options )
7982 {
80- $ this ->loadConfig ();
83+ if ($ this ->loglevel ['debug ' ]['enabled ' ]) {
84+ $ this ->helper ->factory ->setDebug (true );
85+ }
86+
8187 ini_set ('memory_limit ' , -1 );
8288 switch ($ options ->getCmd ()) {
8389 case 'embed ' :
@@ -219,52 +225,26 @@ protected function split($page)
219225 */
220226 protected function chat ()
221227 {
222- if ($ this ->loglevel ['debug ' ]['enabled ' ]) {
223- $ this ->helper ->getChatModel ()->setDebug (true );
224- $ this ->helper ->getRephraseModel ()->setDebug (true );
225- $ this ->helper ->getEmbedModel ()->setDebug (true );
226- }
227-
228228 $ history = [];
229229 while ($ q = $ this ->readLine ('Your Question ' )) {
230230 $ this ->helper ->getChatModel ()->resetUsageStats ();
231231 $ this ->helper ->getRephraseModel ()->resetUsageStats ();
232- $ this ->helper ->getEmbedModel ()->resetUsageStats ();
232+ $ this ->helper ->getEmbeddingModel ()->resetUsageStats ();
233233 $ result = $ this ->helper ->askChatQuestion ($ q , $ history );
234234 $ this ->colors ->ptln ("Interpretation: {$ result ['question ' ]}" , Colors::C_LIGHTPURPLE );
235235 $ history [] = [$ result ['question ' ], $ result ['answer ' ]];
236236 $ this ->printAnswer ($ result );
237237 }
238238 }
239239
240+ /**
241+ * Print information about the available models
242+ *
243+ * @return void
244+ */
240245 protected function models ()
241246 {
242- $ result = [
243- 'chat ' => [],
244- 'embedding ' => [],
245- ];
246-
247-
248- $ jsons = glob (__DIR__ . '/Model/*/models.json ' );
249- foreach ($ jsons as $ json ) {
250- $ models = json_decode (file_get_contents ($ json ), true );
251- foreach ($ models as $ type => $ model ) {
252- $ namespace = basename (dirname ($ json ));
253- foreach ($ model as $ name => $ info ) {
254-
255-
256- $ class = '\\dokuwiki \\plugin \\aichat \\Model \\' . $ namespace . '\\' . ucfirst ($ type ) . 'Model ' ;
257- try {
258- new $ class ($ name , $ this ->conf );
259- $ info ['confok ' ] = true ;
260- } catch (Exception $ e ) {
261- $ info ['confok ' ] = false ;
262- }
263-
264- $ result [$ type ]["$ namespace $ name " ] = $ info ;
265- }
266- }
267- }
247+ $ result = (new ModelFactory ($ this ->conf ))->getModels ();
268248
269249 $ td = new TableFormatter ($ this ->colors );
270250 $ cols = [30 , 20 , 20 , '* ' ];
@@ -284,7 +264,7 @@ protected function models()
284264 $ info ['description ' ] . "\n"
285265 ],
286266 [
287- $ info ['confok ' ] ? Colors::C_LIGHTGREEN : Colors::C_LIGHTRED ,
267+ $ info ['instance ' ] ? Colors::C_LIGHTGREEN : Colors::C_LIGHTRED ,
288268 ]
289269 );
290270 }
@@ -307,7 +287,7 @@ protected function models()
307287 $ info ['description ' ] . "\n"
308288 ],
309289 [
310- $ info ['confok ' ] ? Colors::C_LIGHTGREEN : Colors::C_LIGHTRED ,
290+ $ info ['instance ' ] ? Colors::C_LIGHTGREEN : Colors::C_LIGHTRED ,
311291 ]
312292 );
313293 }
@@ -324,12 +304,6 @@ protected function models()
324304 */
325305 protected function ask ($ query )
326306 {
327- if ($ this ->loglevel ['debug ' ]['enabled ' ]) {
328- $ this ->helper ->getChatModel ()->setDebug (true );
329- $ this ->helper ->getRephraseModel ()->setDebug (true );
330- $ this ->helper ->getEmbedModel ()->setDebug (true );
331- }
332-
333307 $ result = $ this ->helper ->askQuestion ($ query );
334308 $ this ->printAnswer ($ result );
335309 }
@@ -441,7 +415,7 @@ protected function printUsage()
441415 {
442416 $ chat = $ this ->helper ->getChatModel ()->getUsageStats ();
443417 $ rephrase = $ this ->helper ->getRephraseModel ()->getUsageStats ();
444- $ embed = $ this ->helper ->getEmbedModel ()->getUsageStats ();
418+ $ embed = $ this ->helper ->getEmbeddingModel ()->getUsageStats ();
445419
446420 $ this ->info (
447421 'Made {requests} requests in {time}s to models. Used {tokens} tokens for about ${cost}. ' ,
0 commit comments