xref: /plugin/aichat/cli.php (revision 8817535b0c67f8b10e9b8c05dcdf58fc17827423)
1<?php
2
3use dokuwiki\plugin\aichat\Embeddings;
4use dokuwiki\plugin\aichat\OpenAI;
5use Hexogen\KDTree\FSKDTree;
6use Hexogen\KDTree\FSTreePersister;
7use Hexogen\KDTree\Item;
8use Hexogen\KDTree\ItemFactory;
9use Hexogen\KDTree\ItemList;
10use Hexogen\KDTree\KDTree;
11use Hexogen\KDTree\NearestSearch;
12use Hexogen\KDTree\Point;
13use splitbrain\phpcli\Options;
14
15require_once __DIR__ . '/vendor/autoload.php';
16
17/**
18 * DokuWiki Plugin aichat (CLI Component)
19 *
20 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
21 * @author  Andreas Gohr <gohr@cosmocode.de>
22 */
23class cli_plugin_aichat extends \dokuwiki\Extension\CLIPlugin
24{
25
26    /** @inheritDoc */
27    protected function setup(Options $options)
28    {
29        $options->setHelp('Manage the AI chatbot data');
30
31        $options->registerCommand('embed', 'Create embeddings for all pages');
32
33        $options->registerCommand('similar', 'Search for similar pages');
34        $options->registerArgument('query', 'Look up chunks similar to this query', true, 'similar');
35
36        $options->registerCommand('ask', 'Ask a question');
37        $options->registerArgument('question', 'The question to ask', true, 'ask');
38    }
39
40    /** @inheritDoc */
41    protected function main(Options $options)
42    {
43        switch ($options->getCmd()) {
44
45            case 'embed':
46                $this->createEmbeddings();
47                break;
48            case 'similar':
49                $this->similar($options->getArgs()[0]);
50                break;
51            default:
52                echo $options->help();
53        }
54    }
55
56    protected function similar($query)
57    {
58
59        $openAI = new OpenAI($this->getConf('openaikey'), $this->getConf('openaiorg'));
60
61        $embedding = new Embeddings($openAI, $this);
62
63        var_dump($embedding->getSimilarChunks($query));
64    }
65
66    protected function createEmbeddings()
67    {
68        $openAI = new OpenAI($this->getConf('openaikey'), $this->getConf('openaiorg'));
69
70        $embeddings = new Embeddings($openAI, $this);
71        $embeddings->createNewIndex();
72    }
73
74
75}
76
77