xref: /plugin/aichat/cli.php (revision 2ecc089ac91783d306cff496bbc24b69b4e0ad9a)
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');
22*2ecc089aSAndreas 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');
408817535bSAndreas Gohr    }
418817535bSAndreas Gohr
428817535bSAndreas Gohr    /** @inheritDoc */
438817535bSAndreas Gohr    protected function main(Options $options)
448817535bSAndreas Gohr    {
458817535bSAndreas Gohr        switch ($options->getCmd()) {
468817535bSAndreas Gohr
478817535bSAndreas Gohr            case 'embed':
488817535bSAndreas Gohr                $this->createEmbeddings();
498817535bSAndreas Gohr                break;
508817535bSAndreas Gohr            case 'similar':
518817535bSAndreas Gohr                $this->similar($options->getArgs()[0]);
528817535bSAndreas Gohr                break;
537552f1aaSAndreas Gohr            case 'ask':
547552f1aaSAndreas Gohr                $this->ask($options->getArgs()[0]);
557552f1aaSAndreas Gohr                break;
56c4584168SAndreas Gohr            case 'chat':
57c4584168SAndreas Gohr                $this->chat();
58c4584168SAndreas Gohr                break;
598817535bSAndreas Gohr            default:
608817535bSAndreas Gohr                echo $options->help();
618817535bSAndreas Gohr        }
628817535bSAndreas Gohr    }
638817535bSAndreas Gohr
64c4584168SAndreas Gohr    /**
65c4584168SAndreas Gohr     * Interactive Chat Session
66c4584168SAndreas Gohr     *
67c4584168SAndreas Gohr     * @return void
68c4584168SAndreas Gohr     * @throws Exception
69c4584168SAndreas Gohr     */
70c4584168SAndreas Gohr    protected function chat()
71c4584168SAndreas Gohr    {
72c4584168SAndreas Gohr        $history = [];
73c4584168SAndreas Gohr        while ($q = $this->readLine('Your Question')) {
74c4584168SAndreas Gohr            if ($history) {
750337f47fSAndreas Gohr                $question = $this->helper->rephraseChatQuestion($q, $history);
76c4584168SAndreas Gohr                $this->colors->ptln("Interpretation: $question", Colors::C_LIGHTPURPLE);
77c4584168SAndreas Gohr            } else {
78c4584168SAndreas Gohr                $question = $q;
79c4584168SAndreas Gohr            }
800337f47fSAndreas Gohr            $result = $this->helper->askQuestion($question);
81c4584168SAndreas Gohr            $history[] = [$q, $result['answer']];
82c4584168SAndreas Gohr            $this->printAnswer($result);
83c4584168SAndreas Gohr        }
84c4584168SAndreas Gohr    }
85c4584168SAndreas Gohr
86c4584168SAndreas Gohr    /**
87c4584168SAndreas Gohr     * Print the given detailed answer in a nice way
88c4584168SAndreas Gohr     *
89c4584168SAndreas Gohr     * @param array $answer
90c4584168SAndreas Gohr     * @return void
91c4584168SAndreas Gohr     */
92c4584168SAndreas Gohr    protected function printAnswer($answer)
93c4584168SAndreas Gohr    {
94c4584168SAndreas Gohr        $this->colors->ptln($answer['answer'], Colors::C_LIGHTCYAN);
95c4584168SAndreas Gohr        echo "\n";
96c4584168SAndreas Gohr        foreach ($answer['sources'] as $source) {
97c4584168SAndreas Gohr            $this->colors->ptln("\t" . $source['meta']['pageid'], Colors::C_LIGHTBLUE);
98c4584168SAndreas Gohr        }
99c4584168SAndreas Gohr        echo "\n";
100c4584168SAndreas Gohr    }
101c4584168SAndreas Gohr
102c4584168SAndreas Gohr    /**
103c4584168SAndreas Gohr     * Handle a single, standalone question
104c4584168SAndreas Gohr     *
105c4584168SAndreas Gohr     * @param string $query
106c4584168SAndreas Gohr     * @return void
107c4584168SAndreas Gohr     * @throws Exception
108c4584168SAndreas Gohr     */
109c4584168SAndreas Gohr    protected function ask($query)
110c4584168SAndreas Gohr    {
1110337f47fSAndreas Gohr        $result = $this->helper->askQuestion($query);
112c4584168SAndreas Gohr        $this->printAnswer($result);
1137552f1aaSAndreas Gohr    }
1147552f1aaSAndreas Gohr
115c4584168SAndreas Gohr    /**
116c4584168SAndreas Gohr     * Get the pages that are similar to the query
117c4584168SAndreas Gohr     *
118c4584168SAndreas Gohr     * @param string $query
119c4584168SAndreas Gohr     * @return void
120c4584168SAndreas Gohr     */
1218817535bSAndreas Gohr    protected function similar($query)
1228817535bSAndreas Gohr    {
1230337f47fSAndreas Gohr        $sources = $this->helper->getEmbeddings()->getSimilarChunks($query);
124c4584168SAndreas Gohr        foreach ($sources as $source) {
125c4584168SAndreas Gohr            $this->colors->ptln($source['meta']['pageid'], Colors::C_LIGHTBLUE);
126c4584168SAndreas Gohr        }
1278817535bSAndreas Gohr    }
1288817535bSAndreas Gohr
129c4584168SAndreas Gohr    /**
130c4584168SAndreas Gohr     * Recreate chunks and embeddings for all pages
131c4584168SAndreas Gohr     *
132c4584168SAndreas Gohr     * @return void
133c4584168SAndreas Gohr     */
1348817535bSAndreas Gohr    protected function createEmbeddings()
1358817535bSAndreas Gohr    {
1360337f47fSAndreas Gohr        $this->helper->getEmbeddings()->createNewIndex();
1378817535bSAndreas Gohr    }
1388817535bSAndreas Gohr
139c4584168SAndreas Gohr    /**
140c4584168SAndreas Gohr     * Interactively ask for a value from the user
141c4584168SAndreas Gohr     *
142c4584168SAndreas Gohr     * @param string $prompt
143c4584168SAndreas Gohr     * @return string
144c4584168SAndreas Gohr     */
145c4584168SAndreas Gohr    protected function readLine($prompt)
146c4584168SAndreas Gohr    {
147c4584168SAndreas Gohr        $value = '';
1488817535bSAndreas Gohr
149c4584168SAndreas Gohr        while ($value === '') {
150c4584168SAndreas Gohr            echo $prompt;
151c4584168SAndreas Gohr            echo ': ';
152c4584168SAndreas Gohr
153c4584168SAndreas Gohr            $fh = fopen('php://stdin', 'r');
154c4584168SAndreas Gohr            $value = trim(fgets($fh));
155c4584168SAndreas Gohr            fclose($fh);
156c4584168SAndreas Gohr        }
157c4584168SAndreas Gohr
158c4584168SAndreas Gohr        return $value;
159c4584168SAndreas Gohr    }
1608817535bSAndreas Gohr}
1618817535bSAndreas Gohr
162