|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\AI\Store\Bridge\SurrealDB; |
| 13 | + |
| 14 | +use Symfony\AI\Platform\Vector\NullVector; |
| 15 | +use Symfony\AI\Platform\Vector\Vector; |
| 16 | +use Symfony\AI\Store\Document\Metadata; |
| 17 | +use Symfony\AI\Store\Document\VectorDocument; |
| 18 | +use Symfony\AI\Store\Exception\InvalidArgumentException; |
| 19 | +use Symfony\AI\Store\Exception\RuntimeException; |
| 20 | +use Symfony\AI\Store\InitializableStoreInterface; |
| 21 | +use Symfony\AI\Store\VectorStoreInterface; |
| 22 | +use Symfony\Component\Uid\Uuid; |
| 23 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author Guillaume Loulier <[email protected]> |
| 27 | + */ |
| 28 | +final class Store implements InitializableStoreInterface, VectorStoreInterface |
| 29 | +{ |
| 30 | + private const MAXIMUM_EMBEDDINGS_DIMENSIONS = 1275; |
| 31 | + |
| 32 | + public function __construct( |
| 33 | + private readonly HttpClientInterface $httpClient, |
| 34 | + private readonly string $endpointUrl, |
| 35 | + #[\SensitiveParameter] |
| 36 | + private readonly string $user, |
| 37 | + #[\SensitiveParameter] |
| 38 | + private readonly string $password, |
| 39 | + #[\SensitiveParameter] |
| 40 | + private readonly string $namespace, |
| 41 | + #[\SensitiveParameter] |
| 42 | + private readonly string $database, |
| 43 | + private readonly string $table = 'vectors', |
| 44 | + private readonly string $vectorFieldName = '_vectors', |
| 45 | + private readonly string $strategy = 'cosine', |
| 46 | + private readonly int $embeddingsDimension = self::MAXIMUM_EMBEDDINGS_DIMENSIONS, |
| 47 | + ) { |
| 48 | + } |
| 49 | + |
| 50 | + public function add(VectorDocument ...$documents): void |
| 51 | + { |
| 52 | + $authenticationToken = $this->authenticate([]); |
| 53 | + |
| 54 | + foreach ($documents as $document) { |
| 55 | + if (self::MAXIMUM_EMBEDDINGS_DIMENSIONS < $document->vector->getDimensions()) { |
| 56 | + throw new InvalidArgumentException(\sprintf('The SurrealDB HTTP API does not support embeddings with more than %d dimensions, found %d', self::MAXIMUM_EMBEDDINGS_DIMENSIONS, $document->vector->getDimensions())); |
| 57 | + } |
| 58 | + |
| 59 | + $this->request('POST', \sprintf('key/%s', $this->table), $this->convertToIndexableArray($document), [ |
| 60 | + 'Surreal-NS' => $this->namespace, |
| 61 | + 'Surreal-DB' => $this->database, |
| 62 | + 'Authorization' => \sprintf('Bearer %s', $authenticationToken), |
| 63 | + ]); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public function query(Vector $vector, array $options = [], ?float $minScore = null): array |
| 68 | + { |
| 69 | + if (self::MAXIMUM_EMBEDDINGS_DIMENSIONS < $vector->getDimensions()) { |
| 70 | + throw new InvalidArgumentException(\sprintf('The dimensions of the vector must be less than or equal to %d, found %d', self::MAXIMUM_EMBEDDINGS_DIMENSIONS, $vector->getDimensions())); |
| 71 | + } |
| 72 | + |
| 73 | + $authenticationToken = $this->authenticate($options); |
| 74 | + |
| 75 | + $vectors = json_encode($vector->getData()); |
| 76 | + |
| 77 | + $results = $this->request('POST', 'sql', \sprintf( |
| 78 | + 'SELECT id, %s, _metadata, vector::similarity::%s(%s, %s) AS distance FROM %s WHERE %s <|2|> %s;', |
| 79 | + $this->vectorFieldName, $this->strategy, $this->vectorFieldName, $vectors, $this->table, $this->vectorFieldName, $vectors, |
| 80 | + ), [ |
| 81 | + 'Surreal-NS' => $this->namespace, |
| 82 | + 'Surreal-DB' => $this->database, |
| 83 | + 'Authorization' => \sprintf('Bearer %s', $authenticationToken), |
| 84 | + ]); |
| 85 | + |
| 86 | + return array_map($this->convertToVectorDocument(...), $results[0]['result']); |
| 87 | + } |
| 88 | + |
| 89 | + public function initialize(array $options = []): void |
| 90 | + { |
| 91 | + $authenticationToken = $this->authenticate($options); |
| 92 | + |
| 93 | + $this->request('POST', 'sql', \sprintf( |
| 94 | + 'DEFINE INDEX %s_vectors ON %s FIELDS %s MTREE DIMENSION %d DIST %s TYPE F32', |
| 95 | + $this->table, $this->table, $this->vectorFieldName, $this->embeddingsDimension, $this->strategy |
| 96 | + ), [ |
| 97 | + 'Surreal-NS' => $this->namespace, |
| 98 | + 'Surreal-DB' => $this->database, |
| 99 | + 'Authorization' => \sprintf('Bearer %s', $authenticationToken), |
| 100 | + ]); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @param array<string, mixed>|string $payload |
| 105 | + * @param array<string, mixed> $extraHeaders |
| 106 | + * |
| 107 | + * @return array<string|int, mixed> |
| 108 | + */ |
| 109 | + private function request(string $method, string $endpoint, array|string $payload, array $extraHeaders = []): array |
| 110 | + { |
| 111 | + $url = \sprintf('%s/%s', $this->endpointUrl, $endpoint); |
| 112 | + |
| 113 | + $finalPayload = [ |
| 114 | + 'json' => $payload, |
| 115 | + ]; |
| 116 | + |
| 117 | + if (\is_string($payload)) { |
| 118 | + $finalPayload = [ |
| 119 | + 'body' => $payload, |
| 120 | + ]; |
| 121 | + } |
| 122 | + |
| 123 | + $response = $this->httpClient->request($method, $url, array_merge($finalPayload, [ |
| 124 | + 'headers' => array_merge($extraHeaders, [ |
| 125 | + 'Accept' => 'application/json', |
| 126 | + 'Content-Type' => 'application/json', |
| 127 | + ]), |
| 128 | + ])); |
| 129 | + |
| 130 | + return $response->toArray(); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @return array<string, mixed> |
| 135 | + */ |
| 136 | + private function convertToIndexableArray(VectorDocument $document): array |
| 137 | + { |
| 138 | + return [ |
| 139 | + 'id' => $document->id->toRfc4122(), |
| 140 | + $this->vectorFieldName => $document->vector->getData(), |
| 141 | + '_metadata' => array_merge($document->metadata->getArrayCopy(), [ |
| 142 | + '_id' => $document->id->toRfc4122(), |
| 143 | + ]), |
| 144 | + ]; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * @param array<string, mixed> $data |
| 149 | + */ |
| 150 | + private function convertToVectorDocument(array $data): VectorDocument |
| 151 | + { |
| 152 | + $id = $data['_metadata']['_id'] ?? throw new InvalidArgumentException('Missing "id" field in the document data'); |
| 153 | + |
| 154 | + $vector = !\array_key_exists($this->vectorFieldName, $data) || null === $data[$this->vectorFieldName] |
| 155 | + ? new NullVector() |
| 156 | + : new Vector($data[$this->vectorFieldName]); |
| 157 | + |
| 158 | + unset($data['_metadata']['_id']); |
| 159 | + |
| 160 | + return new VectorDocument( |
| 161 | + id: Uuid::fromString($id), |
| 162 | + vector: $vector, |
| 163 | + metadata: new Metadata(array_merge($data['_metadata'], [ |
| 164 | + $this->vectorFieldName => $data[$this->vectorFieldName], |
| 165 | + ])), |
| 166 | + ); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * @param array{ |
| 171 | + * namespacedUser?: bool |
| 172 | + * } $options The namespacedUser option is used to determine if the user is root or not, if not, both the namespace and database must be specified |
| 173 | + */ |
| 174 | + private function authenticate(array $options): string |
| 175 | + { |
| 176 | + $authenticationPayload = [ |
| 177 | + 'user' => $this->user, |
| 178 | + 'pass' => $this->password, |
| 179 | + ]; |
| 180 | + |
| 181 | + if (\array_key_exists('namespacedUser', $options) && !$options['namespacedUser']) { |
| 182 | + $authenticationPayload['ns'] = $this->namespace; |
| 183 | + $authenticationPayload['db'] = $this->database; |
| 184 | + } |
| 185 | + |
| 186 | + $authenticationResponse = $this->request('POST', 'signin', $authenticationPayload); |
| 187 | + |
| 188 | + if (!\array_key_exists('token', $authenticationResponse)) { |
| 189 | + throw new RuntimeException('The SurrealDB authentication response does not contain a token.'); |
| 190 | + } |
| 191 | + |
| 192 | + return $authenticationResponse['token']; |
| 193 | + } |
| 194 | +} |
0 commit comments