1<?php 2 3namespace dokuwiki\plugin\aichat; 4 5use dokuwiki\plugin\aichat\backend\AbstractStorage; 6use dokuwiki\plugin\aichat\backend\Chunk; 7use dokuwiki\plugin\aichat\backend\KDTreeStorage; 8use dokuwiki\plugin\aichat\backend\SQLiteStorage; 9use dokuwiki\Search\Indexer; 10use Hexogen\KDTree\Exception\ValidationException; 11use splitbrain\phpcli\CLI; 12use TikToken\Encoder; 13use Vanderlee\Sentence\Sentence; 14 15/** 16 * Manage the embeddings index 17 * 18 * Pages are split into chunks of 1000 tokens each. For each chunk the embedding vector is fetched from 19 * OpenAI and stored in the Storage backend. 20 */ 21class Embeddings 22{ 23 24 const MAX_TOKEN_LEN = 1000; 25 26 27 /** @var OpenAI */ 28 protected $openAI; 29 /** @var CLI|null */ 30 protected $logger; 31 32 /** @var AbstractStorage */ 33 protected $storage; 34 35 /** 36 * @param OpenAI $openAI 37 */ 38 public function __construct(OpenAI $openAI) 39 { 40 $this->openAI = $openAI; 41 //$this->storage = new KDTreeStorage(); // FIXME make configurable 42 $this->storage = new SQLiteStorage(); // FIXME make configurable 43 } 44 45 /** 46 * Access storage 47 * 48 * @return AbstractStorage 49 */ 50 public function getStorage() 51 { 52 return $this->storage; 53 } 54 55 /** 56 * Add a logger instance 57 * 58 * @param CLI $logger 59 * @return void 60 */ 61 public function setLogger(CLI $logger) 62 { 63 $this->logger = $logger; 64 } 65 66 /** 67 * Update the embeddings storage 68 * 69 * @param string $skipRE Regular expression to filter out pages (full RE with delimiters) 70 * @param bool $clear Should any existing storage be cleared before updating? 71 * @return void 72 * @throws \Exception 73 */ 74 public function createNewIndex($skipRE = '', $clear = false) 75 { 76 $indexer = new Indexer(); 77 $pages = $indexer->getPages(); 78 79 $this->storage->startCreation(1536, $clear); 80 foreach ($pages as $pid => $page) { 81 $chunkID = $pid * 100; // chunk IDs start at page ID * 100 82 83 if ( 84 !page_exists($page) || 85 isHiddenPage($page) || 86 filesize(wikiFN($page)) < 150 || // skip very small pages 87 ($skipRE && preg_match($skipRE, $page)) 88 ) { 89 // this page should not be in the index (anymore) 90 $this->storage->deletePageChunks($page, $chunkID); 91 continue; 92 } 93 94 $firstChunk = $this->storage->getChunk($chunkID); 95 if ($firstChunk && @filemtime(wikiFN($page)) < $firstChunk->getCreated()) { 96 // page is older than the chunks we have, reuse the existing chunks 97 $this->storage->reusePageChunks($page, $chunkID); 98 if ($this->logger) $this->logger->info("Reusing chunks for $page"); 99 } else { 100 // page is newer than the chunks we have, create new chunks 101 $this->storage->deletePageChunks($page, $chunkID); 102 $this->storage->addPageChunks($this->createPageChunks($page, $chunkID)); 103 } 104 } 105 $this->storage->finalizeCreation(); 106 } 107 108 /** 109 * Split the given page, fetch embedding vectors and return Chunks 110 * 111 * Will use the text renderer plugin if available to get the rendered text. 112 * Otherwise the raw wiki text is used. 113 * 114 * @param string $page Name of the page to split 115 * @param int $firstChunkID The ID of the first chunk of this page 116 * @return Chunk[] A list of chunks created for this page 117 * @throws \Exception 118 */ 119 protected function createPageChunks($page, $firstChunkID) 120 { 121 $chunkList = []; 122 123 $textRenderer = plugin_load('renderer', 'text'); 124 if ($textRenderer) { 125 global $ID; 126 $ID = $page; 127 $text = p_cached_output(wikiFN($page), 'text', $page); 128 } else { 129 $text = rawWiki($page); 130 } 131 132 $parts = $this->splitIntoChunks($text); 133 foreach ($parts as $part) { 134 if(trim($part) == '') continue; // skip empty chunks 135 136 try { 137 $embedding = $this->openAI->getEmbedding($part); 138 } catch (\Exception $e) { 139 if ($this->logger) { 140 $this->logger->error( 141 'Failed to get embedding for chunk of page {page}: {msg}', 142 ['page' => $page, 'msg' => $e->getMessage()] 143 ); 144 } 145 continue; 146 } 147 $chunkList[] = new Chunk($page, $firstChunkID, $part, $embedding); 148 $firstChunkID++; 149 } 150 if ($this->logger) { 151 if(count($chunkList)) { 152 $this->logger->success('{id} split into {count} chunks', ['id' => $page, 'count' => count($chunkList)]); 153 } else { 154 $this->logger->warning('{id} could not be split into chunks', ['id' => $page]); 155 } 156 } 157 return $chunkList; 158 } 159 160 /** 161 * Do a nearest neighbor search for chunks similar to the given question 162 * 163 * Returns only chunks the current user is allowed to read, may return an empty result. 164 * 165 * @param string $query The question 166 * @param int $limit The number of results to return 167 * @return Chunk[] 168 * @throws \Exception 169 */ 170 public function getSimilarChunks($query, $limit = 4) 171 { 172 global $auth; 173 $vector = $this->openAI->getEmbedding($query); 174 175 $chunks = $this->storage->getSimilarChunks($vector, $limit); 176 $result = []; 177 foreach ($chunks as $chunk) { 178 // filter out chunks the user is not allowed to read 179 if ($auth && auth_quickaclcheck($chunk->getPage()) < AUTH_READ) continue; 180 $result[] = $chunk; 181 if (count($result) >= $limit) break; 182 } 183 return $result; 184 } 185 186 187 /** 188 * @param $text 189 * @return array 190 * @throws \Exception 191 * @todo maybe add overlap support 192 * @todo support splitting too long sentences 193 */ 194 public function splitIntoChunks($text) 195 { 196 $sentenceSplitter = new Sentence(); 197 $tiktok = new Encoder(); 198 199 $chunks = []; 200 $sentences = $sentenceSplitter->split($text); 201 202 $chunklen = 0; 203 $chunk = ''; 204 while ($sentence = array_shift($sentences)) { 205 $slen = count($tiktok->encode($sentence)); 206 if ($slen > self::MAX_TOKEN_LEN) { 207 // sentence is too long, we need to split it further 208 if ($this->logger) $this->logger->warning('Sentence too long, splitting not implemented yet'); 209 continue; 210 } 211 212 if ($chunklen + $slen < self::MAX_TOKEN_LEN) { 213 // add to current chunk 214 $chunk .= $sentence; 215 $chunklen += $slen; 216 } else { 217 // start new chunk 218 $chunks[] = $chunk; 219 $chunk = $sentence; 220 $chunklen = $slen; 221 } 222 } 223 $chunks[] = $chunk; 224 225 return $chunks; 226 } 227} 228