xref: /plugin/aichat/Embeddings.php (revision 30b9cbc7090f3bc7eca85292f4ff98a1cf513f8f)
18817535bSAndreas Gohr<?php
28817535bSAndreas Gohr
38817535bSAndreas Gohrnamespace dokuwiki\plugin\aichat;
48817535bSAndreas Gohr
57ebc7895Ssplitbrainuse dokuwiki\Extension\PluginInterface;
6f6ef2e50SAndreas Gohruse dokuwiki\plugin\aichat\Model\AbstractModel;
7f6ef2e50SAndreas Gohruse dokuwiki\plugin\aichat\Storage\AbstractStorage;
88817535bSAndreas Gohruse dokuwiki\Search\Indexer;
92ecc089aSAndreas Gohruse splitbrain\phpcli\CLI;
108817535bSAndreas Gohruse TikToken\Encoder;
118817535bSAndreas Gohruse Vanderlee\Sentence\Sentence;
128817535bSAndreas Gohr
139da5f0dfSAndreas Gohr/**
149da5f0dfSAndreas Gohr * Manage the embeddings index
159da5f0dfSAndreas Gohr *
169da5f0dfSAndreas Gohr * Pages are split into chunks of 1000 tokens each. For each chunk the embedding vector is fetched from
177ee8b02dSAndreas Gohr * OpenAI and stored in the Storage backend.
189da5f0dfSAndreas Gohr */
198817535bSAndreas Gohrclass Embeddings
208817535bSAndreas Gohr{
2168908844SAndreas Gohr    /** @var int maximum overlap between chunks in tokens */
22*30b9cbc7Ssplitbrain    final public const MAX_OVERLAP_LEN = 200;
238817535bSAndreas Gohr
24f6ef2e50SAndreas Gohr    /** @var AbstractModel */
25f6ef2e50SAndreas Gohr    protected $model;
262ecc089aSAndreas Gohr    /** @var CLI|null */
272ecc089aSAndreas Gohr    protected $logger;
2868908844SAndreas Gohr    /** @var Encoder */
2968908844SAndreas Gohr    protected $tokenEncoder;
308817535bSAndreas Gohr
317ee8b02dSAndreas Gohr    /** @var AbstractStorage */
327ee8b02dSAndreas Gohr    protected $storage;
337ee8b02dSAndreas Gohr
3468908844SAndreas Gohr    /** @var array remember sentences when chunking */
3568908844SAndreas Gohr    private $sentenceQueue = [];
3668908844SAndreas Gohr
37f6ef2e50SAndreas Gohr    public function __construct(AbstractModel $model, AbstractStorage $storage)
388817535bSAndreas Gohr    {
39f6ef2e50SAndreas Gohr        $this->model = $model;
40f6ef2e50SAndreas Gohr        $this->storage = $storage;
417ee8b02dSAndreas Gohr    }
427ee8b02dSAndreas Gohr
437ee8b02dSAndreas Gohr    /**
447ee8b02dSAndreas Gohr     * Access storage
457ee8b02dSAndreas Gohr     *
467ee8b02dSAndreas Gohr     * @return AbstractStorage
477ee8b02dSAndreas Gohr     */
487ee8b02dSAndreas Gohr    public function getStorage()
497ee8b02dSAndreas Gohr    {
507ee8b02dSAndreas Gohr        return $this->storage;
512ecc089aSAndreas Gohr    }
522ecc089aSAndreas Gohr
532ecc089aSAndreas Gohr    /**
542ecc089aSAndreas Gohr     * Add a logger instance
552ecc089aSAndreas Gohr     *
562ecc089aSAndreas Gohr     * @return void
572ecc089aSAndreas Gohr     */
582ecc089aSAndreas Gohr    public function setLogger(CLI $logger)
592ecc089aSAndreas Gohr    {
608817535bSAndreas Gohr        $this->logger = $logger;
618817535bSAndreas Gohr    }
628817535bSAndreas Gohr
632ecc089aSAndreas Gohr    /**
6468908844SAndreas Gohr     * Get the token encoder instance
6568908844SAndreas Gohr     *
6668908844SAndreas Gohr     * @return Encoder
6768908844SAndreas Gohr     */
6868908844SAndreas Gohr    public function getTokenEncoder()
6968908844SAndreas Gohr    {
707ebc7895Ssplitbrain        if (!$this->tokenEncoder instanceof Encoder) {
7168908844SAndreas Gohr            $this->tokenEncoder = new Encoder();
7268908844SAndreas Gohr        }
7368908844SAndreas Gohr        return $this->tokenEncoder;
7468908844SAndreas Gohr    }
7568908844SAndreas Gohr
7668908844SAndreas Gohr    /**
775284515dSAndreas Gohr     * Update the embeddings storage
782ecc089aSAndreas Gohr     *
79ad38c5fdSAndreas Gohr     * @param string $skipRE Regular expression to filter out pages (full RE with delimiters)
805284515dSAndreas Gohr     * @param bool $clear Should any existing storage be cleared before updating?
812ecc089aSAndreas Gohr     * @return void
825284515dSAndreas Gohr     * @throws \Exception
832ecc089aSAndreas Gohr     */
845284515dSAndreas Gohr    public function createNewIndex($skipRE = '', $clear = false)
858817535bSAndreas Gohr    {
868817535bSAndreas Gohr        $indexer = new Indexer();
878817535bSAndreas Gohr        $pages = $indexer->getPages();
888817535bSAndreas Gohr
89f6ef2e50SAndreas Gohr        $this->storage->startCreation($clear);
905aa45b4dSAndreas Gohr        foreach ($pages as $pid => $page) {
915aa45b4dSAndreas Gohr            $chunkID = $pid * 100; // chunk IDs start at page ID * 100
925aa45b4dSAndreas Gohr
935284515dSAndreas Gohr            if (
945284515dSAndreas Gohr                !page_exists($page) ||
955284515dSAndreas Gohr                isHiddenPage($page) ||
964e206c13SAndreas Gohr                filesize(wikiFN($page)) < 150 || // skip very small pages
97*30b9cbc7Ssplitbrain                ($skipRE && preg_match($skipRE, (string) $page))
985284515dSAndreas Gohr            ) {
995284515dSAndreas Gohr                // this page should not be in the index (anymore)
1005284515dSAndreas Gohr                $this->storage->deletePageChunks($page, $chunkID);
1015284515dSAndreas Gohr                continue;
1025284515dSAndreas Gohr            }
1035284515dSAndreas Gohr
1047ee8b02dSAndreas Gohr            $firstChunk = $this->storage->getChunk($chunkID);
1057ee8b02dSAndreas Gohr            if ($firstChunk && @filemtime(wikiFN($page)) < $firstChunk->getCreated()) {
1065aa45b4dSAndreas Gohr                // page is older than the chunks we have, reuse the existing chunks
1077ee8b02dSAndreas Gohr                $this->storage->reusePageChunks($page, $chunkID);
1087ebc7895Ssplitbrain                if ($this->logger instanceof CLI) $this->logger->info("Reusing chunks for $page");
1095aa45b4dSAndreas Gohr            } else {
1105aa45b4dSAndreas Gohr                // page is newer than the chunks we have, create new chunks
1117ee8b02dSAndreas Gohr                $this->storage->deletePageChunks($page, $chunkID);
1127ee8b02dSAndreas Gohr                $this->storage->addPageChunks($this->createPageChunks($page, $chunkID));
1135aa45b4dSAndreas Gohr            }
1145aa45b4dSAndreas Gohr        }
1157ee8b02dSAndreas Gohr        $this->storage->finalizeCreation();
1165aa45b4dSAndreas Gohr    }
1175aa45b4dSAndreas Gohr
1185aa45b4dSAndreas Gohr    /**
1197ee8b02dSAndreas Gohr     * Split the given page, fetch embedding vectors and return Chunks
1205aa45b4dSAndreas Gohr     *
12188305719SAndreas Gohr     * Will use the text renderer plugin if available to get the rendered text.
12288305719SAndreas Gohr     * Otherwise the raw wiki text is used.
12388305719SAndreas Gohr     *
1245aa45b4dSAndreas Gohr     * @param string $page Name of the page to split
1257ee8b02dSAndreas Gohr     * @param int $firstChunkID The ID of the first chunk of this page
1267ee8b02dSAndreas Gohr     * @return Chunk[] A list of chunks created for this page
1275aa45b4dSAndreas Gohr     * @throws \Exception
1285aa45b4dSAndreas Gohr     */
1297ee8b02dSAndreas Gohr    protected function createPageChunks($page, $firstChunkID)
1305aa45b4dSAndreas Gohr    {
1317ee8b02dSAndreas Gohr        $chunkList = [];
13288305719SAndreas Gohr
13388305719SAndreas Gohr        $textRenderer = plugin_load('renderer', 'text');
1347ebc7895Ssplitbrain        if ($textRenderer instanceof PluginInterface) {
13588305719SAndreas Gohr            global $ID;
13688305719SAndreas Gohr            $ID = $page;
13788305719SAndreas Gohr            $text = p_cached_output(wikiFN($page), 'text', $page);
13888305719SAndreas Gohr        } else {
13988305719SAndreas Gohr            $text = rawWiki($page);
14088305719SAndreas Gohr        }
14188305719SAndreas Gohr
14288305719SAndreas Gohr        $parts = $this->splitIntoChunks($text);
1437ee8b02dSAndreas Gohr        foreach ($parts as $part) {
144*30b9cbc7Ssplitbrain            if (trim((string) $part) == '') continue; // skip empty chunks
14593c1dbf4SAndreas Gohr
146ad38c5fdSAndreas Gohr            try {
147f6ef2e50SAndreas Gohr                $embedding = $this->model->getEmbedding($part);
148ad38c5fdSAndreas Gohr            } catch (\Exception $e) {
1497ebc7895Ssplitbrain                if ($this->logger instanceof CLI) {
150ad38c5fdSAndreas Gohr                    $this->logger->error(
151ad38c5fdSAndreas Gohr                        'Failed to get embedding for chunk of page {page}: {msg}',
152ad38c5fdSAndreas Gohr                        ['page' => $page, 'msg' => $e->getMessage()]
153ad38c5fdSAndreas Gohr                    );
154ad38c5fdSAndreas Gohr                }
155ad38c5fdSAndreas Gohr                continue;
156ad38c5fdSAndreas Gohr            }
1577ee8b02dSAndreas Gohr            $chunkList[] = new Chunk($page, $firstChunkID, $part, $embedding);
1587ee8b02dSAndreas Gohr            $firstChunkID++;
1598817535bSAndreas Gohr        }
1607ebc7895Ssplitbrain        if ($this->logger instanceof CLI) {
1617ebc7895Ssplitbrain            if ($chunkList !== []) {
162f8d5ae01SAndreas Gohr                $this->logger->success(
163f8d5ae01SAndreas Gohr                    '{id} split into {count} chunks',
164f8d5ae01SAndreas Gohr                    ['id' => $page, 'count' => count($chunkList)]
165f8d5ae01SAndreas Gohr                );
16693c1dbf4SAndreas Gohr            } else {
16793c1dbf4SAndreas Gohr                $this->logger->warning('{id} could not be split into chunks', ['id' => $page]);
16893c1dbf4SAndreas Gohr            }
1698817535bSAndreas Gohr        }
1707ee8b02dSAndreas Gohr        return $chunkList;
1718817535bSAndreas Gohr    }
1728817535bSAndreas Gohr
1739e81bea7SAndreas Gohr    /**
1749e81bea7SAndreas Gohr     * Do a nearest neighbor search for chunks similar to the given question
1759e81bea7SAndreas Gohr     *
1769e81bea7SAndreas Gohr     * Returns only chunks the current user is allowed to read, may return an empty result.
17768908844SAndreas Gohr     * The number of returned chunks depends on the MAX_CONTEXT_LEN setting.
1789e81bea7SAndreas Gohr     *
1799e81bea7SAndreas Gohr     * @param string $query The question
180e33a1d7aSAndreas Gohr     * @param string $lang Limit results to this language
1817ee8b02dSAndreas Gohr     * @return Chunk[]
1829e81bea7SAndreas Gohr     * @throws \Exception
1839e81bea7SAndreas Gohr     */
184e33a1d7aSAndreas Gohr    public function getSimilarChunks($query, $lang = '')
1858817535bSAndreas Gohr    {
1869e81bea7SAndreas Gohr        global $auth;
187f6ef2e50SAndreas Gohr        $vector = $this->model->getEmbedding($query);
1888817535bSAndreas Gohr
189f6ef2e50SAndreas Gohr        $fetch = ceil(
190f6ef2e50SAndreas Gohr            ($this->model->getMaxContextTokenLength() / $this->model->getMaxEmbeddingTokenLength())
191f6ef2e50SAndreas Gohr            * 1.5 // fetch a few more than needed, since not all chunks are maximum length
192f6ef2e50SAndreas Gohr        );
193aee9b383SAndreas Gohr
194aee9b383SAndreas Gohr        $time = microtime(true);
195e33a1d7aSAndreas Gohr        $chunks = $this->storage->getSimilarChunks($vector, $lang, $fetch);
1967ebc7895Ssplitbrain        if ($this->logger instanceof CLI) {
197aee9b383SAndreas Gohr            $this->logger->info(
198aee9b383SAndreas Gohr                'Fetched {count} similar chunks from store in {time} seconds',
199aee9b383SAndreas Gohr                ['count' => count($chunks), 'time' => round(microtime(true) - $time, 2)]
200aee9b383SAndreas Gohr            );
201aee9b383SAndreas Gohr        }
20268908844SAndreas Gohr
20368908844SAndreas Gohr        $size = 0;
2048817535bSAndreas Gohr        $result = [];
2057ee8b02dSAndreas Gohr        foreach ($chunks as $chunk) {
2069e81bea7SAndreas Gohr            // filter out chunks the user is not allowed to read
2077ee8b02dSAndreas Gohr            if ($auth && auth_quickaclcheck($chunk->getPage()) < AUTH_READ) continue;
20868908844SAndreas Gohr
20968908844SAndreas Gohr            $chunkSize = count($this->getTokenEncoder()->encode($chunk->getText()));
210f6ef2e50SAndreas Gohr            if ($size + $chunkSize > $this->model->getMaxContextTokenLength()) break; // we have enough
21168908844SAndreas Gohr
2129e81bea7SAndreas Gohr            $result[] = $chunk;
21368908844SAndreas Gohr            $size += $chunkSize;
2148817535bSAndreas Gohr        }
2158817535bSAndreas Gohr        return $result;
2168817535bSAndreas Gohr    }
2178817535bSAndreas Gohr
2185786be46SAndreas Gohr
2195786be46SAndreas Gohr    /**
2208817535bSAndreas Gohr     * @param $text
2218817535bSAndreas Gohr     * @return array
2228817535bSAndreas Gohr     * @throws \Exception
2238817535bSAndreas Gohr     * @todo support splitting too long sentences
2248817535bSAndreas Gohr     */
225ad38c5fdSAndreas Gohr    public function splitIntoChunks($text)
2268817535bSAndreas Gohr    {
2278817535bSAndreas Gohr        $sentenceSplitter = new Sentence();
22868908844SAndreas Gohr        $tiktok = $this->getTokenEncoder();
2298817535bSAndreas Gohr
2308817535bSAndreas Gohr        $chunks = [];
2318817535bSAndreas Gohr        $sentences = $sentenceSplitter->split($text);
2328817535bSAndreas Gohr
2338817535bSAndreas Gohr        $chunklen = 0;
2348817535bSAndreas Gohr        $chunk = '';
2358817535bSAndreas Gohr        while ($sentence = array_shift($sentences)) {
2368817535bSAndreas Gohr            $slen = count($tiktok->encode($sentence));
237f6ef2e50SAndreas Gohr            if ($slen > $this->model->getMaxEmbeddingTokenLength()) {
2388817535bSAndreas Gohr                // sentence is too long, we need to split it further
239f8d5ae01SAndreas Gohr                if ($this->logger instanceof CLI) $this->logger->warning(
240f8d5ae01SAndreas Gohr                    'Sentence too long, splitting not implemented yet'
241f8d5ae01SAndreas Gohr                );
242ad38c5fdSAndreas Gohr                continue;
2438817535bSAndreas Gohr            }
2448817535bSAndreas Gohr
245f6ef2e50SAndreas Gohr            if ($chunklen + $slen < $this->model->getMaxEmbeddingTokenLength()) {
2468817535bSAndreas Gohr                // add to current chunk
2478817535bSAndreas Gohr                $chunk .= $sentence;
2488817535bSAndreas Gohr                $chunklen += $slen;
24968908844SAndreas Gohr                // remember sentence for overlap check
25068908844SAndreas Gohr                $this->rememberSentence($sentence);
2518817535bSAndreas Gohr            } else {
25268908844SAndreas Gohr                // add current chunk to result
2538817535bSAndreas Gohr                $chunks[] = $chunk;
25468908844SAndreas Gohr
25568908844SAndreas Gohr                // start new chunk with remembered sentences
2567ebc7895Ssplitbrain                $chunk = implode(' ', $this->sentenceQueue);
25768908844SAndreas Gohr                $chunk .= $sentence;
25868908844SAndreas Gohr                $chunklen = count($tiktok->encode($chunk));
2598817535bSAndreas Gohr            }
2608817535bSAndreas Gohr        }
2618817535bSAndreas Gohr        $chunks[] = $chunk;
2628817535bSAndreas Gohr
2638817535bSAndreas Gohr        return $chunks;
2648817535bSAndreas Gohr    }
26568908844SAndreas Gohr
26668908844SAndreas Gohr    /**
26768908844SAndreas Gohr     * Add a sentence to the queue of remembered sentences
26868908844SAndreas Gohr     *
26968908844SAndreas Gohr     * @param string $sentence
27068908844SAndreas Gohr     * @return void
27168908844SAndreas Gohr     */
27268908844SAndreas Gohr    protected function rememberSentence($sentence)
27368908844SAndreas Gohr    {
27468908844SAndreas Gohr        // add sentence to queue
27568908844SAndreas Gohr        $this->sentenceQueue[] = $sentence;
27668908844SAndreas Gohr
27768908844SAndreas Gohr        // remove oldest sentences from queue until we are below the max overlap
27868908844SAndreas Gohr        $encoder = $this->getTokenEncoder();
2797ebc7895Ssplitbrain        while (count($encoder->encode(implode(' ', $this->sentenceQueue))) > self::MAX_OVERLAP_LEN) {
28068908844SAndreas Gohr            array_shift($this->sentenceQueue);
28168908844SAndreas Gohr        }
28268908844SAndreas Gohr    }
2838817535bSAndreas Gohr}
284