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