getEmbedding())); $aGrouped[$sTable][] = $oDocument; } foreach ($aGrouped as $sTable => $aDocuments) { $this->tableName = $sTable; parent::addDocuments($aDocuments); } return $this; } public function deleteBy(string $sourceType, ?string $sourceName = null): NeuronAI\RAG\VectorStore\VectorStoreInterface { foreach (BxDolAIVectorStoreMariaDb::DIMENSIONS as $iDimension) { $this->tableName = BxDolAIVectorStoreMariaDb::tableForDimension($iDimension); parent::deleteBy($sourceType, $sourceName); } return $this; } public function similaritySearch(array $embedding): iterable { $this->tableName = BxDolAIVectorStoreMariaDb::tableForDimension(count($embedding)); $oStatement = $this->pdo->prepare(sprintf( <<<'SQL' SELECT id, content, sourceType, sourceName, metadata, VEC_DISTANCE_COSINE(embedding, VEC_FromText(:embedding)) AS distance FROM %s WHERE sourceType = :storeId ORDER BY distance ASC LIMIT %d SQL, $this->tableName, $this->topK, )); $oStatement->execute([ ':embedding' => json_encode($embedding), ':storeId' => $this->sStoreId, ]); $aDocuments = []; while ($aRow = $oStatement->fetch(PDO::FETCH_ASSOC)) { $oDocument = new NeuronAI\RAG\Document($aRow['content']); $oDocument->id = $aRow['id']; $oDocument->sourceType = $aRow['sourceType']; $oDocument->sourceName = $aRow['sourceName']; $oDocument->score = NeuronAI\RAG\VectorSimilarity::similarityFromDistance((float)$aRow['distance']); $aMetadata = json_decode($aRow['metadata'] ?? '{}', true); if (is_array($aMetadata)) { foreach ($aMetadata as $sKey => $mixedValue) { if (!in_array($sKey, ['content', 'sourceType', 'sourceName', 'score', 'embedding', 'id'], true)) $oDocument->addMetadata($sKey, $mixedValue); } } $aDocuments[] = $oDocument; } return $aDocuments; } } /** @} */