xref: /plugin/aichat/cli.php (revision 7552f1aab0e641608239f92e08c409f2c4f17b1a)
18817535bSAndreas Gohr<?php
28817535bSAndreas Gohr
38817535bSAndreas Gohruse dokuwiki\plugin\aichat\Embeddings;
48817535bSAndreas Gohruse dokuwiki\plugin\aichat\OpenAI;
58817535bSAndreas Gohruse Hexogen\KDTree\FSKDTree;
68817535bSAndreas Gohruse Hexogen\KDTree\FSTreePersister;
78817535bSAndreas Gohruse Hexogen\KDTree\Item;
88817535bSAndreas Gohruse Hexogen\KDTree\ItemFactory;
98817535bSAndreas Gohruse Hexogen\KDTree\ItemList;
108817535bSAndreas Gohruse Hexogen\KDTree\KDTree;
118817535bSAndreas Gohruse Hexogen\KDTree\NearestSearch;
128817535bSAndreas Gohruse Hexogen\KDTree\Point;
138817535bSAndreas Gohruse splitbrain\phpcli\Options;
148817535bSAndreas Gohr
158817535bSAndreas Gohrrequire_once __DIR__ . '/vendor/autoload.php';
168817535bSAndreas Gohr
178817535bSAndreas Gohr/**
188817535bSAndreas Gohr * DokuWiki Plugin aichat (CLI Component)
198817535bSAndreas Gohr *
208817535bSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
218817535bSAndreas Gohr * @author  Andreas Gohr <gohr@cosmocode.de>
228817535bSAndreas Gohr */
238817535bSAndreas Gohrclass cli_plugin_aichat extends \dokuwiki\Extension\CLIPlugin
248817535bSAndreas Gohr{
258817535bSAndreas Gohr
268817535bSAndreas Gohr    /** @inheritDoc */
278817535bSAndreas Gohr    protected function setup(Options $options)
288817535bSAndreas Gohr    {
298817535bSAndreas Gohr        $options->setHelp('Manage the AI chatbot data');
308817535bSAndreas Gohr
318817535bSAndreas Gohr        $options->registerCommand('embed', 'Create embeddings for all pages');
328817535bSAndreas Gohr
338817535bSAndreas Gohr        $options->registerCommand('similar', 'Search for similar pages');
348817535bSAndreas Gohr        $options->registerArgument('query', 'Look up chunks similar to this query', true, 'similar');
358817535bSAndreas Gohr
368817535bSAndreas Gohr        $options->registerCommand('ask', 'Ask a question');
378817535bSAndreas Gohr        $options->registerArgument('question', 'The question to ask', true, 'ask');
388817535bSAndreas Gohr    }
398817535bSAndreas Gohr
408817535bSAndreas Gohr    /** @inheritDoc */
418817535bSAndreas Gohr    protected function main(Options $options)
428817535bSAndreas Gohr    {
438817535bSAndreas Gohr        switch ($options->getCmd()) {
448817535bSAndreas Gohr
458817535bSAndreas Gohr            case 'embed':
468817535bSAndreas Gohr                $this->createEmbeddings();
478817535bSAndreas Gohr                break;
488817535bSAndreas Gohr            case 'similar':
498817535bSAndreas Gohr                $this->similar($options->getArgs()[0]);
508817535bSAndreas Gohr                break;
51*7552f1aaSAndreas Gohr            case 'ask':
52*7552f1aaSAndreas Gohr                $this->ask($options->getArgs()[0]);
53*7552f1aaSAndreas Gohr                break;
548817535bSAndreas Gohr            default:
558817535bSAndreas Gohr                echo $options->help();
568817535bSAndreas Gohr        }
578817535bSAndreas Gohr    }
588817535bSAndreas Gohr
59*7552f1aaSAndreas Gohr    protected function ask($query) {
60*7552f1aaSAndreas Gohr        /** @var helper_plugin_aichat_prompt $prompt */
61*7552f1aaSAndreas Gohr        $prompt = plugin_load('helper', 'aichat_prompt');
62*7552f1aaSAndreas Gohr
63*7552f1aaSAndreas Gohr        $result = $prompt->askQuestion($query);
64*7552f1aaSAndreas Gohr
65*7552f1aaSAndreas Gohr        echo $result['answer'];
66*7552f1aaSAndreas Gohr        echo "\n\nSources:\n";
67*7552f1aaSAndreas Gohr        foreach($result['sources'] as $source) {
68*7552f1aaSAndreas Gohr            echo $source['meta']['pageid'] . "\n";
69*7552f1aaSAndreas Gohr        }
70*7552f1aaSAndreas Gohr    }
71*7552f1aaSAndreas Gohr
728817535bSAndreas Gohr    protected function similar($query)
738817535bSAndreas Gohr    {
748817535bSAndreas Gohr
758817535bSAndreas Gohr        $openAI = new OpenAI($this->getConf('openaikey'), $this->getConf('openaiorg'));
768817535bSAndreas Gohr
778817535bSAndreas Gohr        $embedding = new Embeddings($openAI, $this);
788817535bSAndreas Gohr
798817535bSAndreas Gohr        var_dump($embedding->getSimilarChunks($query));
808817535bSAndreas Gohr    }
818817535bSAndreas Gohr
828817535bSAndreas Gohr    protected function createEmbeddings()
838817535bSAndreas Gohr    {
848817535bSAndreas Gohr        $openAI = new OpenAI($this->getConf('openaikey'), $this->getConf('openaiorg'));
858817535bSAndreas Gohr
868817535bSAndreas Gohr        $embeddings = new Embeddings($openAI, $this);
878817535bSAndreas Gohr        $embeddings->createNewIndex();
888817535bSAndreas Gohr    }
898817535bSAndreas Gohr
908817535bSAndreas Gohr
918817535bSAndreas Gohr}
928817535bSAndreas Gohr
93