xref: /plugin/aichat/cli.php (revision 7ee8b02d54a468f0b7f9bba54177ef52db1e95de)
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');
435786be46SAndreas Gohr
445786be46SAndreas 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;
675786be46SAndreas Gohr            case 'info':
685786be46SAndreas Gohr                $this->treeinfo();
695786be46SAndreas Gohr                break;
708817535bSAndreas Gohr            default:
718817535bSAndreas Gohr                echo $options->help();
728817535bSAndreas Gohr        }
738817535bSAndreas Gohr    }
748817535bSAndreas Gohr
75c4584168SAndreas Gohr    /**
765786be46SAndreas Gohr     * @return void
775786be46SAndreas Gohr     */
785786be46SAndreas Gohr    protected function treeinfo()
795786be46SAndreas Gohr    {
80*7ee8b02dSAndreas Gohr        $stats = $this->helper->getEmbeddings()->getStorage()->statistics();
81*7ee8b02dSAndreas Gohr        foreach($stats as $key => $value) {
82*7ee8b02dSAndreas Gohr            echo $key . ': ' . $value. "\n";
83*7ee8b02dSAndreas Gohr        }
845786be46SAndreas Gohr    }
855786be46SAndreas Gohr
865786be46SAndreas Gohr    /**
87ad38c5fdSAndreas Gohr     * Split the given page into chunks and print them
88ad38c5fdSAndreas Gohr     *
89ad38c5fdSAndreas Gohr     * @param string $page
90ad38c5fdSAndreas Gohr     * @return void
91ad38c5fdSAndreas Gohr     * @throws Exception
92ad38c5fdSAndreas Gohr     */
93ad38c5fdSAndreas Gohr    protected function split($page)
94ad38c5fdSAndreas Gohr    {
95ad38c5fdSAndreas Gohr        $text = rawWiki($page);
96ad38c5fdSAndreas Gohr        $chunks = $this->helper->getEmbeddings()->splitIntoChunks($text);
97ad38c5fdSAndreas Gohr        foreach ($chunks as $chunk) {
98ad38c5fdSAndreas Gohr            echo $chunk;
99ad38c5fdSAndreas Gohr            echo "\n";
100ad38c5fdSAndreas Gohr            $this->colors->ptln('--------------------------------', Colors::C_LIGHTPURPLE);
101ad38c5fdSAndreas Gohr        }
102ad38c5fdSAndreas Gohr        $this->success('Split into ' . count($chunks) . ' chunks');
103ad38c5fdSAndreas Gohr    }
104ad38c5fdSAndreas Gohr
105ad38c5fdSAndreas Gohr    /**
106c4584168SAndreas Gohr     * Interactive Chat Session
107c4584168SAndreas Gohr     *
108c4584168SAndreas Gohr     * @return void
109c4584168SAndreas Gohr     * @throws Exception
110c4584168SAndreas Gohr     */
111c4584168SAndreas Gohr    protected function chat()
112c4584168SAndreas Gohr    {
113c4584168SAndreas Gohr        $history = [];
114c4584168SAndreas Gohr        while ($q = $this->readLine('Your Question')) {
115c4584168SAndreas Gohr            if ($history) {
1160337f47fSAndreas Gohr                $question = $this->helper->rephraseChatQuestion($q, $history);
117c4584168SAndreas Gohr                $this->colors->ptln("Interpretation: $question", Colors::C_LIGHTPURPLE);
118c4584168SAndreas Gohr            } else {
119c4584168SAndreas Gohr                $question = $q;
120c4584168SAndreas Gohr            }
1210337f47fSAndreas Gohr            $result = $this->helper->askQuestion($question);
122c4584168SAndreas Gohr            $history[] = [$q, $result['answer']];
123c4584168SAndreas Gohr            $this->printAnswer($result);
124c4584168SAndreas Gohr        }
125c4584168SAndreas Gohr    }
126c4584168SAndreas Gohr
127c4584168SAndreas Gohr    /**
128c4584168SAndreas Gohr     * Print the given detailed answer in a nice way
129c4584168SAndreas Gohr     *
130c4584168SAndreas Gohr     * @param array $answer
131c4584168SAndreas Gohr     * @return void
132c4584168SAndreas Gohr     */
133c4584168SAndreas Gohr    protected function printAnswer($answer)
134c4584168SAndreas Gohr    {
135c4584168SAndreas Gohr        $this->colors->ptln($answer['answer'], Colors::C_LIGHTCYAN);
136c4584168SAndreas Gohr        echo "\n";
137c4584168SAndreas Gohr        foreach ($answer['sources'] as $source) {
138c4584168SAndreas Gohr            $this->colors->ptln("\t" . $source['meta']['pageid'], Colors::C_LIGHTBLUE);
139c4584168SAndreas Gohr        }
140c4584168SAndreas Gohr        echo "\n";
141c4584168SAndreas Gohr    }
142c4584168SAndreas Gohr
143c4584168SAndreas Gohr    /**
144c4584168SAndreas Gohr     * Handle a single, standalone question
145c4584168SAndreas Gohr     *
146c4584168SAndreas Gohr     * @param string $query
147c4584168SAndreas Gohr     * @return void
148c4584168SAndreas Gohr     * @throws Exception
149c4584168SAndreas Gohr     */
150c4584168SAndreas Gohr    protected function ask($query)
151c4584168SAndreas Gohr    {
1520337f47fSAndreas Gohr        $result = $this->helper->askQuestion($query);
153c4584168SAndreas Gohr        $this->printAnswer($result);
1547552f1aaSAndreas Gohr    }
1557552f1aaSAndreas Gohr
156c4584168SAndreas Gohr    /**
157c4584168SAndreas Gohr     * Get the pages that are similar to the query
158c4584168SAndreas Gohr     *
159c4584168SAndreas Gohr     * @param string $query
160c4584168SAndreas Gohr     * @return void
161c4584168SAndreas Gohr     */
1628817535bSAndreas Gohr    protected function similar($query)
1638817535bSAndreas Gohr    {
1640337f47fSAndreas Gohr        $sources = $this->helper->getEmbeddings()->getSimilarChunks($query);
165c4584168SAndreas Gohr        foreach ($sources as $source) {
166*7ee8b02dSAndreas Gohr            $this->colors->ptln($source->getPage(), Colors::C_LIGHTBLUE);
167c4584168SAndreas Gohr        }
1688817535bSAndreas Gohr    }
1698817535bSAndreas Gohr
170c4584168SAndreas Gohr    /**
171c4584168SAndreas Gohr     * Recreate chunks and embeddings for all pages
172c4584168SAndreas Gohr     *
173c4584168SAndreas Gohr     * @return void
174ad38c5fdSAndreas Gohr     * @todo make skip regex configurable
175c4584168SAndreas Gohr     */
1768817535bSAndreas Gohr    protected function createEmbeddings()
1778817535bSAndreas Gohr    {
178ad38c5fdSAndreas Gohr        ini_set('memory_limit', -1); // we may need a lot of memory here
179ad38c5fdSAndreas Gohr        $this->helper->getEmbeddings()->createNewIndex('/(^|:)(playground|sandbox)(:|$)/');
180ad38c5fdSAndreas Gohr        $this->notice('Peak memory used: {memory}', ['memory' => filesize_h(memory_get_peak_usage(true))]);
1818817535bSAndreas Gohr    }
1828817535bSAndreas Gohr
183c4584168SAndreas Gohr    /**
184c4584168SAndreas Gohr     * Interactively ask for a value from the user
185c4584168SAndreas Gohr     *
186c4584168SAndreas Gohr     * @param string $prompt
187c4584168SAndreas Gohr     * @return string
188c4584168SAndreas Gohr     */
189c4584168SAndreas Gohr    protected function readLine($prompt)
190c4584168SAndreas Gohr    {
191c4584168SAndreas Gohr        $value = '';
1928817535bSAndreas Gohr
193c4584168SAndreas Gohr        while ($value === '') {
194c4584168SAndreas Gohr            echo $prompt;
195c4584168SAndreas Gohr            echo ': ';
196c4584168SAndreas Gohr
197c4584168SAndreas Gohr            $fh = fopen('php://stdin', 'r');
198c4584168SAndreas Gohr            $value = trim(fgets($fh));
199c4584168SAndreas Gohr            fclose($fh);
200c4584168SAndreas Gohr        }
201c4584168SAndreas Gohr
202c4584168SAndreas Gohr        return $value;
203c4584168SAndreas Gohr    }
2048817535bSAndreas Gohr}
2058817535bSAndreas Gohr
206