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