|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace NeuronAI\RAG\VectorStore; |
| 6 | + |
| 7 | +use NeuronAI\RAG\Document; |
| 8 | +use OpenSearch\Client; |
| 9 | +use Exception; |
| 10 | + |
| 11 | +use function array_key_exists; |
| 12 | +use function array_keys; |
| 13 | +use function array_map; |
| 14 | +use function count; |
| 15 | +use function in_array; |
| 16 | +use function max; |
| 17 | + |
| 18 | +class OpenSearchVectorStore implements VectorStoreInterface |
| 19 | +{ |
| 20 | + protected bool $vectorDimSet = false; |
| 21 | + |
| 22 | + protected array $filters = []; |
| 23 | + |
| 24 | + public function __construct( |
| 25 | + protected Client $client, |
| 26 | + protected string $index, |
| 27 | + protected int $topK = 4, |
| 28 | + ) { |
| 29 | + } |
| 30 | + |
| 31 | + protected function checkIndexStatus(Document $document): void |
| 32 | + { |
| 33 | + $indexExists = $this->client->indices()->exists(['index' => $this->index]); |
| 34 | + |
| 35 | + if ($indexExists) { |
| 36 | + $this->mapVectorDimension(count($document->getEmbedding())); |
| 37 | + |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + $properties = [ |
| 42 | + 'content' => [ |
| 43 | + 'type' => 'text', |
| 44 | + ], |
| 45 | + 'sourceType' => [ |
| 46 | + 'type' => 'keyword', |
| 47 | + ], |
| 48 | + 'sourceName' => [ |
| 49 | + 'type' => 'keyword', |
| 50 | + ], |
| 51 | + 'embedding' => [ |
| 52 | + 'type' => 'knn_vector', |
| 53 | + 'dimension' => count($document->getEmbedding()), |
| 54 | + 'index' => true, |
| 55 | + 'method' => [ |
| 56 | + 'name' => 'hnsw', |
| 57 | + 'engine' => 'lucene', |
| 58 | + 'space_type' => 'cosinesimil', |
| 59 | + 'parameters' => [ |
| 60 | + 'encoder' => [ |
| 61 | + 'name' => 'sq' |
| 62 | + ] |
| 63 | + ] |
| 64 | + ], |
| 65 | + ] |
| 66 | + ]; |
| 67 | + |
| 68 | + // Map metadata |
| 69 | + foreach (array_keys($document->metadata) as $name) { |
| 70 | + $properties[$name] = [ |
| 71 | + 'type' => 'keyword', |
| 72 | + ]; |
| 73 | + } |
| 74 | + |
| 75 | + $this->client->indices()->create([ |
| 76 | + 'index' => $this->index, |
| 77 | + 'body' => [ |
| 78 | + 'settings' => [ |
| 79 | + 'index' => [ |
| 80 | + 'knn' => true, |
| 81 | + 'number_of_replicas' => 0, |
| 82 | + ], |
| 83 | + ], |
| 84 | + 'mappings' => [ |
| 85 | + 'properties' => $properties |
| 86 | + ] |
| 87 | + ] |
| 88 | + ]); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @throws Exception |
| 93 | + */ |
| 94 | + public function addDocument(Document $document): VectorStoreInterface |
| 95 | + { |
| 96 | + if ($document->embedding === []) { |
| 97 | + throw new Exception('Document embedding must be set before adding a document'); |
| 98 | + } |
| 99 | + |
| 100 | + $this->checkIndexStatus($document); |
| 101 | + |
| 102 | + $this->client->index([ |
| 103 | + 'index' => $this->index, |
| 104 | + 'body' => [ |
| 105 | + 'embedding' => $document->getEmbedding(), |
| 106 | + 'content' => $document->getContent(), |
| 107 | + 'sourceType' => $document->getSourceType(), |
| 108 | + 'sourceName' => $document->getSourceName(), |
| 109 | + ...$document->metadata, |
| 110 | + ], |
| 111 | + ]); |
| 112 | + |
| 113 | + $this->client->indices()->refresh(['index' => $this->index]); |
| 114 | + |
| 115 | + return $this; |
| 116 | + } |
| 117 | + |
| 118 | + public function addDocuments(array $documents): VectorStoreInterface |
| 119 | + { |
| 120 | + if ($documents === []) { |
| 121 | + return $this; |
| 122 | + } |
| 123 | + |
| 124 | + if (empty($documents[0]->getEmbedding())) { |
| 125 | + throw new Exception('Document embedding must be set before adding a document'); |
| 126 | + } |
| 127 | + |
| 128 | + $this->checkIndexStatus($documents[0]); |
| 129 | + |
| 130 | + /* |
| 131 | + * Generate a bulk payload |
| 132 | + */ |
| 133 | + $params = ['body' => []]; |
| 134 | + foreach ($documents as $document) { |
| 135 | + $params['body'][] = [ |
| 136 | + 'index' => [ |
| 137 | + '_index' => $this->index, |
| 138 | + ], |
| 139 | + ]; |
| 140 | + $params['body'][] = [ |
| 141 | + 'embedding' => $document->getEmbedding(), |
| 142 | + 'content' => $document->getContent(), |
| 143 | + 'sourceType' => $document->getSourceType(), |
| 144 | + 'sourceName' => $document->getSourceName(), |
| 145 | + ...$document->metadata, |
| 146 | + ]; |
| 147 | + } |
| 148 | + $this->client->bulk($params); |
| 149 | + $this->client->indices()->refresh(['index' => $this->index]); |
| 150 | + return $this; |
| 151 | + } |
| 152 | + |
| 153 | + public function deleteBySource(string $sourceType, string $sourceName): VectorStoreInterface |
| 154 | + { |
| 155 | + $this->client->deleteByQuery([ |
| 156 | + 'index' => $this->index, |
| 157 | + 'q' => "sourceType:{$sourceType} AND sourceName:{$sourceName}", |
| 158 | + 'body' => [] |
| 159 | + ]); |
| 160 | + $this->client->indices()->refresh(['index' => $this->index]); |
| 161 | + return $this; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * @return Document[] |
| 166 | + */ |
| 167 | + public function similaritySearch(array $embedding): iterable |
| 168 | + { |
| 169 | + $searchParams = [ |
| 170 | + 'index' => $this->index, |
| 171 | + 'body' => [ |
| 172 | + 'query' => [ |
| 173 | + 'knn' => [ |
| 174 | + 'embedding' => [ |
| 175 | + 'vector' => $embedding, |
| 176 | + 'k' => max(50, $this->topK * 4), |
| 177 | + ], |
| 178 | + ], |
| 179 | + ], |
| 180 | + 'sort' => [ |
| 181 | + '_score' => [ |
| 182 | + 'order' => 'desc', |
| 183 | + ], |
| 184 | + ], |
| 185 | + ], |
| 186 | + ]; |
| 187 | + |
| 188 | + // Hybrid search |
| 189 | + if ($this->filters !== []) { |
| 190 | + $searchParams['body']['query']['knn']['filter'] = $this->filters; |
| 191 | + } |
| 192 | + |
| 193 | + $response = $this->client->search($searchParams); |
| 194 | + |
| 195 | + return array_map(function (array $item): Document { |
| 196 | + $document = new Document($item['_source']['content']); |
| 197 | + //$document->embedding = $item['_source']['embedding']; // avoid carrying large data |
| 198 | + $document->sourceType = $item['_source']['sourceType']; |
| 199 | + $document->sourceName = $item['_source']['sourceName']; |
| 200 | + $document->score = $item['_score']; |
| 201 | + |
| 202 | + foreach ($item['_source'] as $name => $value) { |
| 203 | + if (!in_array($name, ['content', 'sourceType', 'sourceName', 'score', 'embedding', 'id'])) { |
| 204 | + $document->addMetadata($name, $value); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + return $document; |
| 209 | + }, $response['hits']['hits']); |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * Map vector embeddings dimension on the fly. |
| 214 | + */ |
| 215 | + private function mapVectorDimension(int $dimension): void |
| 216 | + { |
| 217 | + if ($this->vectorDimSet) { |
| 218 | + return; |
| 219 | + } |
| 220 | + |
| 221 | + $response = $this->client->indices()->getFieldMapping([ |
| 222 | + 'index' => $this->index, |
| 223 | + 'fields' => 'embedding', |
| 224 | + ]); |
| 225 | + |
| 226 | + $mappings = $response[$this->index]['mappings']; |
| 227 | + |
| 228 | + if ( |
| 229 | + array_key_exists('embedding', $mappings) |
| 230 | + && $mappings['embedding']['mapping']['embedding']['dimension'] === $dimension |
| 231 | + ) { |
| 232 | + return; |
| 233 | + } |
| 234 | + |
| 235 | + $this->client->indices()->putMapping([ |
| 236 | + 'index' => $this->index, |
| 237 | + 'body' => [ |
| 238 | + 'properties' => [ |
| 239 | + 'embedding' => [ |
| 240 | + 'type' => 'knn_vector', |
| 241 | + 'dimension' => $dimension, |
| 242 | + 'index' => true, |
| 243 | + 'method' => [ |
| 244 | + 'name' => 'hnsw', |
| 245 | + 'engine' => 'lucene', |
| 246 | + 'space_type' => 'cosinesimil', |
| 247 | + 'parameters' => [ |
| 248 | + 'encoder' => [ |
| 249 | + 'name' => 'sq' |
| 250 | + ] |
| 251 | + ] |
| 252 | + |
| 253 | + ], |
| 254 | + ], |
| 255 | + ], |
| 256 | + ], |
| 257 | + ]); |
| 258 | + |
| 259 | + $this->vectorDimSet = true; |
| 260 | + } |
| 261 | + |
| 262 | + public function withFilters(array $filters): self |
| 263 | + { |
| 264 | + $this->filters = $filters; |
| 265 | + return $this; |
| 266 | + } |
| 267 | +} |
0 commit comments