xref: /plugin/aichat/cli.php (revision 5786be46be4ea7477d2002e973db2f0e45f3db8b)
18817535bSAndreas Gohr<?php
28817535bSAndreas Gohr
3c4584168SAndreas Gohruse splitbrain\phpcli\Colors;
48817535bSAndreas Gohruse splitbrain\phpcli\Options;
58817535bSAndreas Gohr
68817535bSAndreas Gohr
78817535bSAndreas Gohr/**
88817535bSAndreas Gohr * DokuWiki Plugin aichat (CLI Component)
98817535bSAndreas Gohr *
108817535bSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
118817535bSAndreas Gohr * @author  Andreas Gohr <gohr@cosmocode.de>
128817535bSAndreas Gohr */
138817535bSAndreas Gohrclass cli_plugin_aichat extends \dokuwiki\Extension\CLIPlugin
148817535bSAndreas Gohr{
150337f47fSAndreas Gohr    /** @var helper_plugin_aichat */
160337f47fSAndreas Gohr    protected $helper;
170337f47fSAndreas Gohr
180337f47fSAndreas Gohr    public function __construct($autocatch = true)
190337f47fSAndreas Gohr    {
200337f47fSAndreas Gohr        parent::__construct($autocatch);
210337f47fSAndreas Gohr        $this->helper = plugin_load('helper', 'aichat');
222ecc089aSAndreas Gohr        $this->helper->getEmbeddings()->setLogger($this);
230337f47fSAndreas Gohr    }
240337f47fSAndreas Gohr
258817535bSAndreas Gohr
268817535bSAndreas Gohr    /** @inheritDoc */
278817535bSAndreas Gohr    protected function setup(Options $options)
288817535bSAndreas Gohr    {
299da5f0dfSAndreas Gohr        $options->setHelp('Manage and query 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');
38c4584168SAndreas Gohr
39c4584168SAndreas Gohr        $options->registerCommand('chat', 'Start an interactive chat session');
40ad38c5fdSAndreas Gohr
41ad38c5fdSAndreas Gohr        $options->registerCommand('split', 'Split a page into chunks (for debugging)');
42ad38c5fdSAndreas Gohr        $options->registerArgument('page', 'The page to split', true, 'split');
43*5786be46SAndreas Gohr
44*5786be46SAndreas Gohr        $options->registerCommand('info', 'Get Info about the K-D Tree');
458817535bSAndreas Gohr    }
468817535bSAndreas Gohr
478817535bSAndreas Gohr    /** @inheritDoc */
488817535bSAndreas Gohr    protected function main(Options $options)
498817535bSAndreas Gohr    {
508817535bSAndreas Gohr        switch ($options->getCmd()) {
518817535bSAndreas Gohr
528817535bSAndreas Gohr            case 'embed':
538817535bSAndreas Gohr                $this->createEmbeddings();
548817535bSAndreas Gohr                break;
558817535bSAndreas Gohr            case 'similar':
568817535bSAndreas Gohr                $this->similar($options->getArgs()[0]);
578817535bSAndreas Gohr                break;
587552f1aaSAndreas Gohr            case 'ask':
597552f1aaSAndreas Gohr                $this->ask($options->getArgs()[0]);
607552f1aaSAndreas Gohr                break;
61c4584168SAndreas Gohr            case 'chat':
62c4584168SAndreas Gohr                $this->chat();
63c4584168SAndreas Gohr                break;
64ad38c5fdSAndreas Gohr            case 'split':
65ad38c5fdSAndreas Gohr                $this->split($options->getArgs()[0]);
66ad38c5fdSAndreas Gohr                break;
67*5786be46SAndreas Gohr            case 'info':
68*5786be46SAndreas Gohr                $this->treeinfo();
69*5786be46SAndreas Gohr                break;
708817535bSAndreas Gohr            default:
718817535bSAndreas Gohr                echo $options->help();
728817535bSAndreas Gohr        }
738817535bSAndreas Gohr    }
748817535bSAndreas Gohr
75c4584168SAndreas Gohr    /**
76*5786be46SAndreas Gohr     * @return void
77*5786be46SAndreas Gohr     */
78*5786be46SAndreas Gohr    protected function treeinfo()
79*5786be46SAndreas Gohr    {
80*5786be46SAndreas Gohr        $tree = $this->helper->getEmbeddings()->getTree();
81*5786be46SAndreas Gohr        echo 'Items: ' . $tree->getItemCount() . "\n";
82*5786be46SAndreas Gohr        echo 'Dimensions: ' . $tree->getDimensionCount() . "\n";
83*5786be46SAndreas Gohr    }
84*5786be46SAndreas Gohr
85*5786be46SAndreas Gohr    /**
86ad38c5fdSAndreas Gohr     * Split the given page into chunks and print them
87ad38c5fdSAndreas Gohr     *
88ad38c5fdSAndreas Gohr     * @param string $page
89ad38c5fdSAndreas Gohr     * @return void
90ad38c5fdSAndreas Gohr     * @throws Exception
91ad38c5fdSAndreas Gohr     */
92ad38c5fdSAndreas Gohr    protected function split($page)
93ad38c5fdSAndreas Gohr    {
94ad38c5fdSAndreas Gohr        $text = rawWiki($page);
95ad38c5fdSAndreas Gohr        $chunks = $this->helper->getEmbeddings()->splitIntoChunks($text);
96ad38c5fdSAndreas Gohr        foreach ($chunks as $chunk) {
97ad38c5fdSAndreas Gohr            echo $chunk;
98ad38c5fdSAndreas Gohr            echo "\n";
99ad38c5fdSAndreas Gohr            $this->colors->ptln('--------------------------------', Colors::C_LIGHTPURPLE);
100ad38c5fdSAndreas Gohr        }
101ad38c5fdSAndreas Gohr        $this->success('Split into ' . count($chunks) . ' chunks');
102ad38c5fdSAndreas Gohr    }
103ad38c5fdSAndreas Gohr
104ad38c5fdSAndreas Gohr    /**
105c4584168SAndreas Gohr     * Interactive Chat Session
106c4584168SAndreas Gohr     *
107c4584168SAndreas Gohr     * @return void
108c4584168SAndreas Gohr     * @throws Exception
109c4584168SAndreas Gohr     */
110c4584168SAndreas Gohr    protected function chat()
111c4584168SAndreas Gohr    {
112c4584168SAndreas Gohr        $history = [];
113c4584168SAndreas Gohr        while ($q = $this->readLine('Your Question')) {
114c4584168SAndreas Gohr            if ($history) {
1150337f47fSAndreas Gohr                $question = $this->helper->rephraseChatQuestion($q, $history);
116c4584168SAndreas Gohr                $this->colors->ptln("Interpretation: $question", Colors::C_LIGHTPURPLE);
117c4584168SAndreas Gohr            } else {
118c4584168SAndreas Gohr                $question = $q;
119c4584168SAndreas Gohr            }
1200337f47fSAndreas Gohr            $result = $this->helper->askQuestion($question);
121c4584168SAndreas Gohr            $history[] = [$q, $result['answer']];
122c4584168SAndreas Gohr            $this->printAnswer($result);
123c4584168SAndreas Gohr        }
124c4584168SAndreas Gohr    }
125c4584168SAndreas Gohr
126c4584168SAndreas Gohr    /**
127c4584168SAndreas Gohr     * Print the given detailed answer in a nice way
128c4584168SAndreas Gohr     *
129c4584168SAndreas Gohr     * @param array $answer
130c4584168SAndreas Gohr     * @return void
131c4584168SAndreas Gohr     */
132c4584168SAndreas Gohr    protected function printAnswer($answer)
133c4584168SAndreas Gohr    {
134c4584168SAndreas Gohr        $this->colors->ptln($answer['answer'], Colors::C_LIGHTCYAN);
135c4584168SAndreas Gohr        echo "\n";
136c4584168SAndreas Gohr        foreach ($answer['sources'] as $source) {
137c4584168SAndreas Gohr            $this->colors->ptln("\t" . $source['meta']['pageid'], Colors::C_LIGHTBLUE);
138c4584168SAndreas Gohr        }
139c4584168SAndreas Gohr        echo "\n";
140c4584168SAndreas Gohr    }
141c4584168SAndreas Gohr
142c4584168SAndreas Gohr    /**
143c4584168SAndreas Gohr     * Handle a single, standalone question
144c4584168SAndreas Gohr     *
145c4584168SAndreas Gohr     * @param string $query
146c4584168SAndreas Gohr     * @return void
147c4584168SAndreas Gohr     * @throws Exception
148c4584168SAndreas Gohr     */
149c4584168SAndreas Gohr    protected function ask($query)
150c4584168SAndreas Gohr    {
1510337f47fSAndreas Gohr        $result = $this->helper->askQuestion($query);
152c4584168SAndreas Gohr        $this->printAnswer($result);
1537552f1aaSAndreas Gohr    }
1547552f1aaSAndreas Gohr
155c4584168SAndreas Gohr    /**
156c4584168SAndreas Gohr     * Get the pages that are similar to the query
157c4584168SAndreas Gohr     *
158c4584168SAndreas Gohr     * @param string $query
159c4584168SAndreas Gohr     * @return void
160c4584168SAndreas Gohr     */
1618817535bSAndreas Gohr    protected function similar($query)
1628817535bSAndreas Gohr    {
1630337f47fSAndreas Gohr        $sources = $this->helper->getEmbeddings()->getSimilarChunks($query);
164c4584168SAndreas Gohr        foreach ($sources as $source) {
165c4584168SAndreas Gohr            $this->colors->ptln($source['meta']['pageid'], Colors::C_LIGHTBLUE);
166c4584168SAndreas Gohr        }
1678817535bSAndreas Gohr    }
1688817535bSAndreas Gohr
169c4584168SAndreas Gohr    /**
170c4584168SAndreas Gohr     * Recreate chunks and embeddings for all pages
171c4584168SAndreas Gohr     *
172c4584168SAndreas Gohr     * @return void
173ad38c5fdSAndreas Gohr     * @todo make skip regex configurable
174c4584168SAndreas Gohr     */
1758817535bSAndreas Gohr    protected function createEmbeddings()
1768817535bSAndreas Gohr    {
177ad38c5fdSAndreas Gohr        ini_set('memory_limit', -1); // we may need a lot of memory here
178ad38c5fdSAndreas Gohr        $this->helper->getEmbeddings()->createNewIndex('/(^|:)(playground|sandbox)(:|$)/');
179ad38c5fdSAndreas Gohr        $this->notice('Peak memory used: {memory}', ['memory' => filesize_h(memory_get_peak_usage(true))]);
1808817535bSAndreas Gohr    }
1818817535bSAndreas Gohr
182c4584168SAndreas Gohr    /**
183c4584168SAndreas Gohr     * Interactively ask for a value from the user
184c4584168SAndreas Gohr     *
185c4584168SAndreas Gohr     * @param string $prompt
186c4584168SAndreas Gohr     * @return string
187c4584168SAndreas Gohr     */
188c4584168SAndreas Gohr    protected function readLine($prompt)
189c4584168SAndreas Gohr    {
190c4584168SAndreas Gohr        $value = '';
1918817535bSAndreas Gohr
192c4584168SAndreas Gohr        while ($value === '') {
193c4584168SAndreas Gohr            echo $prompt;
194c4584168SAndreas Gohr            echo ': ';
195c4584168SAndreas Gohr
196c4584168SAndreas Gohr            $fh = fopen('php://stdin', 'r');
197c4584168SAndreas Gohr            $value = trim(fgets($fh));
198c4584168SAndreas Gohr            fclose($fh);
199c4584168SAndreas Gohr        }
200c4584168SAndreas Gohr
201c4584168SAndreas Gohr        return $value;
202c4584168SAndreas Gohr    }
2038817535bSAndreas Gohr}
2048817535bSAndreas Gohr
205