|
| 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\Meilisearch; |
| 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\InitializableStoreInterface; |
| 20 | +use Symfony\AI\Store\VectorStoreInterface; |
| 21 | +use Symfony\Component\Uid\Uuid; |
| 22 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 23 | + |
| 24 | +/** |
| 25 | + * @author Guillaume Loulier <[email protected]> |
| 26 | + */ |
| 27 | +final readonly class Store implements InitializableStoreInterface, VectorStoreInterface |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @param string $embedder The name of the embedder where vectors are stored |
| 31 | + * @param string $vectorFieldName The name of the field int the index that contains the vector |
| 32 | + */ |
| 33 | + public function __construct( |
| 34 | + private HttpClientInterface $httpClient, |
| 35 | + private string $endpointUrl, |
| 36 | + #[\SensitiveParameter] private string $apiKey, |
| 37 | + private string $indexName, |
| 38 | + private string $embedder = 'default', |
| 39 | + private string $vectorFieldName = '_vectors', |
| 40 | + private int $embeddingsDimension = 1536, |
| 41 | + ) { |
| 42 | + } |
| 43 | + |
| 44 | + public function add(VectorDocument ...$documents): void |
| 45 | + { |
| 46 | + $this->request('PUT', \sprintf('indexes/%s/documents', $this->indexName), array_map( |
| 47 | + $this->convertToIndexableArray(...), $documents) |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + public function query(Vector $vector, array $options = [], ?float $minScore = null): array |
| 52 | + { |
| 53 | + $response = $this->request('POST', \sprintf('indexes/%s/search', $this->indexName), [ |
| 54 | + 'vector' => $vector->getData(), |
| 55 | + 'showRankingScore' => true, |
| 56 | + 'retrieveVectors' => true, |
| 57 | + 'hybrid' => [ |
| 58 | + 'embedder' => $this->embedder, |
| 59 | + 'semanticRatio' => 1.0, |
| 60 | + ], |
| 61 | + ]); |
| 62 | + |
| 63 | + return array_map($this->convertToVectorDocument(...), $response['hits']); |
| 64 | + } |
| 65 | + |
| 66 | + public function initialize(array $options = []): void |
| 67 | + { |
| 68 | + if ([] !== $options) { |
| 69 | + throw new InvalidArgumentException('No supported options'); |
| 70 | + } |
| 71 | + |
| 72 | + $this->request('POST', 'indexes', [ |
| 73 | + 'uid' => $this->indexName, |
| 74 | + 'primaryKey' => 'id', |
| 75 | + ]); |
| 76 | + |
| 77 | + $this->request('PATCH', \sprintf('indexes/%s/settings', $this->indexName), [ |
| 78 | + 'embedders' => [ |
| 79 | + $this->embedder => [ |
| 80 | + 'source' => 'userProvided', |
| 81 | + 'dimensions' => $this->embeddingsDimension, |
| 82 | + ], |
| 83 | + ], |
| 84 | + ]); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @param array<string, mixed> $payload |
| 89 | + * |
| 90 | + * @return array<string, mixed> |
| 91 | + */ |
| 92 | + private function request(string $method, string $endpoint, array $payload): array |
| 93 | + { |
| 94 | + $url = \sprintf('%s/%s', $this->endpointUrl, $endpoint); |
| 95 | + $response = $this->httpClient->request($method, $url, [ |
| 96 | + 'headers' => [ |
| 97 | + 'Authorization' => \sprintf('Bearer %s', $this->apiKey), |
| 98 | + ], |
| 99 | + 'json' => $payload, |
| 100 | + ]); |
| 101 | + |
| 102 | + return $response->toArray(); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @return array<string, mixed> |
| 107 | + */ |
| 108 | + private function convertToIndexableArray(VectorDocument $document): array |
| 109 | + { |
| 110 | + return array_merge([ |
| 111 | + 'id' => $document->id->toRfc4122(), |
| 112 | + $this->vectorFieldName => [ |
| 113 | + $this->embedder => [ |
| 114 | + 'embeddings' => $document->vector->getData(), |
| 115 | + 'regenerate' => false, |
| 116 | + ], |
| 117 | + ], |
| 118 | + ], $document->metadata->getArrayCopy()); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @param array<string, mixed> $data |
| 123 | + */ |
| 124 | + private function convertToVectorDocument(array $data): VectorDocument |
| 125 | + { |
| 126 | + return new VectorDocument( |
| 127 | + id: Uuid::fromString($data['id']), |
| 128 | + vector: !\array_key_exists($this->vectorFieldName, $data) || null === $data[$this->vectorFieldName] |
| 129 | + ? new NullVector() |
| 130 | + : new Vector($data[$this->vectorFieldName][$this->embedder]['embeddings']), |
| 131 | + metadata: new Metadata($data), |
| 132 | + ); |
| 133 | + } |
| 134 | +} |
0 commit comments