|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpLlm\LlmChain\Store\MongoDB; |
| 6 | + |
| 7 | +use MongoDB\BSON\Binary; |
| 8 | +use MongoDB\Client; |
| 9 | +use MongoDB\Collection; |
| 10 | +use PhpLlm\LlmChain\Document\Document; |
| 11 | +use PhpLlm\LlmChain\Document\Metadata; |
| 12 | +use PhpLlm\LlmChain\Document\Vector; |
| 13 | +use PhpLlm\LlmChain\Store\VectorStoreInterface; |
| 14 | +use Psr\Log\LoggerInterface; |
| 15 | +use Symfony\Component\Uid\Uuid; |
| 16 | + |
| 17 | +/** |
| 18 | + * @see https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-overview/ |
| 19 | + * |
| 20 | + * For this store you need to create a separate MongoDB Atlas Search index. |
| 21 | + * The index needs to be created with the following settings: |
| 22 | + * { |
| 23 | + * "fields": [ |
| 24 | + * { |
| 25 | + * "numDimensions": 1536, |
| 26 | + * "path": "vector", |
| 27 | + * "similarity": "euclidean", |
| 28 | + * "type": "vector" |
| 29 | + * } |
| 30 | + * ] |
| 31 | + * } |
| 32 | + * |
| 33 | + * Note, that the `path` key needs to match the $vectorFieldName. |
| 34 | + * |
| 35 | + * For the `similarity` key you can choose between `euclidean`, `cosine` and `dotProduct`. |
| 36 | + * {@see https://www.mongodb.com/docs/atlas/atlas-search/field-types/knn-vector/#define-the-index-for-the-fts-field-type-type} |
| 37 | + * |
| 38 | + * @author Oskar Stark <[email protected]> |
| 39 | + */ |
| 40 | +final readonly class Store implements VectorStoreInterface |
| 41 | +{ |
| 42 | + /** |
| 43 | + * @param string $databaseName The name of the database |
| 44 | + * @param string $collectionName The name of the collection |
| 45 | + * @param string $indexName The name of the Atlas Search index |
| 46 | + * @param string $vectorFieldName The name of the field int the index that contains the vector |
| 47 | + * @param bool $bulkWrite Use bulk write operations |
| 48 | + */ |
| 49 | + public function __construct( |
| 50 | + private Client $client, |
| 51 | + private LoggerInterface $logger, |
| 52 | + private string $databaseName, |
| 53 | + private string $collectionName, |
| 54 | + private string $indexName, |
| 55 | + private string $vectorFieldName = 'vector', |
| 56 | + private bool $bulkWrite = false, |
| 57 | + ) { |
| 58 | + } |
| 59 | + |
| 60 | + public function addDocument(Document $document): void |
| 61 | + { |
| 62 | + $this->addDocuments([$document]); |
| 63 | + } |
| 64 | + |
| 65 | + public function addDocuments(array $documents): void |
| 66 | + { |
| 67 | + $operations = []; |
| 68 | + |
| 69 | + foreach ($documents as $document) { |
| 70 | + if (!$document->hasVector()) { |
| 71 | + $this->logger->warning('Document {id} does not have a vector', ['id' => $document->id]); |
| 72 | + } |
| 73 | + |
| 74 | + $operation = [ |
| 75 | + ['_id' => $this->toBinary($document->id)], // we use binary for the id, because of storage efficiency |
| 76 | + array_filter([ |
| 77 | + 'metadata' => $document->metadata, |
| 78 | + $this->vectorFieldName => $document->vector->getData(), |
| 79 | + 'text' => $document->text, |
| 80 | + ]), |
| 81 | + ['upsert' => true], // insert if not exists |
| 82 | + ]; |
| 83 | + |
| 84 | + if ($this->bulkWrite) { |
| 85 | + $operations[] = ['replaceOne' => $operation]; |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + $this->getCollection()->replaceOne(...$operation); |
| 90 | + } |
| 91 | + |
| 92 | + if ($this->bulkWrite) { |
| 93 | + $this->getCollection()->bulkWrite($operations); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @param array{ |
| 99 | + * limit?: positive-int, |
| 100 | + * numCandidates?: positive-int, |
| 101 | + * filter?: array<mixed> |
| 102 | + * } $options |
| 103 | + * |
| 104 | + * @return Document[] |
| 105 | + */ |
| 106 | + public function query(Vector $vector, array $options = []): array |
| 107 | + { |
| 108 | + $results = $this->getCollection()->aggregate([ |
| 109 | + [ |
| 110 | + '$vectorSearch' => array_merge([ |
| 111 | + 'index' => $this->indexName, |
| 112 | + 'path' => $this->vectorFieldName, |
| 113 | + 'queryVector' => $vector->getData(), |
| 114 | + 'numCandidates' => 200, |
| 115 | + 'limit' => 5, |
| 116 | + ], $options), |
| 117 | + ], |
| 118 | + [ |
| 119 | + '$addFields' => [ |
| 120 | + 'score' => ['$meta' => 'vectorSearchScore'], |
| 121 | + ], |
| 122 | + ], |
| 123 | + ], ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']]); |
| 124 | + |
| 125 | + $documents = []; |
| 126 | + |
| 127 | + foreach ($results as $result) { |
| 128 | + $documents[] = Document::fromVector( |
| 129 | + Vector::create1536($result[$this->vectorFieldName]), |
| 130 | + $this->toUuid($result['_id']), |
| 131 | + new Metadata($result['metadata'] ?? []), |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + return $documents; |
| 136 | + } |
| 137 | + |
| 138 | + private function getCollection(): Collection |
| 139 | + { |
| 140 | + return $this->client->selectCollection($this->databaseName, $this->collectionName); |
| 141 | + } |
| 142 | + |
| 143 | + private function toBinary(Uuid $uuid): Binary |
| 144 | + { |
| 145 | + return new Binary($uuid->toBinary(), Binary::TYPE_UUID); |
| 146 | + } |
| 147 | + |
| 148 | + private function toUuid(Binary $binary): Uuid |
| 149 | + { |
| 150 | + return Uuid::fromString($binary->getData()); |
| 151 | + } |
| 152 | +} |
0 commit comments