|
| 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\Typesense; |
| 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 | + public function __construct( |
| 30 | + private HttpClientInterface $httpClient, |
| 31 | + private string $endpointUrl, |
| 32 | + #[\SensitiveParameter] private string $apiKey, |
| 33 | + #[\SensitiveParameter] private string $collection, |
| 34 | + private string $vectorFieldName = '_vectors', |
| 35 | + private int $embeddingsDimension = 1536, |
| 36 | + ) { |
| 37 | + } |
| 38 | + |
| 39 | + public function add(VectorDocument ...$documents): void |
| 40 | + { |
| 41 | + foreach ($documents as $document) { |
| 42 | + $this->request('POST', \sprintf('collections/%s/documents', $this->collection), $this->convertToIndexableArray($document)); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public function query(Vector $vector, array $options = []): array |
| 47 | + { |
| 48 | + $documents = $this->request('POST', 'multi_search', [ |
| 49 | + 'searches' => [ |
| 50 | + [ |
| 51 | + 'collection' => $this->collection, |
| 52 | + 'q' => '*', |
| 53 | + 'vector_query' => \sprintf('%s:([%s], k:%d)', $this->vectorFieldName, implode(', ', $vector->getData()), $options['k'] ?? 10), |
| 54 | + ], |
| 55 | + ], |
| 56 | + ]); |
| 57 | + |
| 58 | + return array_map($this->convertToVectorDocument(...), $documents['results'][0]['hits']); |
| 59 | + } |
| 60 | + |
| 61 | + public function initialize(array $options = []): void |
| 62 | + { |
| 63 | + if ([] !== $options) { |
| 64 | + throw new InvalidArgumentException('No supported options.'); |
| 65 | + } |
| 66 | + |
| 67 | + $this->request('POST', 'collections', [ |
| 68 | + 'name' => $this->collection, |
| 69 | + 'fields' => [ |
| 70 | + [ |
| 71 | + 'name' => 'id', |
| 72 | + 'type' => 'string', |
| 73 | + ], |
| 74 | + [ |
| 75 | + 'name' => $this->vectorFieldName, |
| 76 | + 'type' => 'float[]', |
| 77 | + 'num_dim' => $this->embeddingsDimension, |
| 78 | + ], |
| 79 | + [ |
| 80 | + 'name' => 'metadata', |
| 81 | + 'type' => 'string', |
| 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 | + $result = $this->httpClient->request($method, $url, [ |
| 96 | + 'headers' => [ |
| 97 | + 'X-TYPESENSE-API-KEY' => $this->apiKey, |
| 98 | + ], |
| 99 | + 'json' => $payload, |
| 100 | + ]); |
| 101 | + |
| 102 | + return $result->toArray(); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @return array<string, mixed> |
| 107 | + */ |
| 108 | + private function convertToIndexableArray(VectorDocument $document): array |
| 109 | + { |
| 110 | + return [ |
| 111 | + 'id' => $document->id->toRfc4122(), |
| 112 | + $this->vectorFieldName => $document->vector->getData(), |
| 113 | + 'metadata' => json_encode($document->metadata->getArrayCopy()), |
| 114 | + ]; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @param array<string, mixed> $data |
| 119 | + */ |
| 120 | + private function convertToVectorDocument(array $data): VectorDocument |
| 121 | + { |
| 122 | + $document = $data['document'] ?? throw new InvalidArgumentException('Missing "document" field in the document data.'); |
| 123 | + |
| 124 | + $id = $document['id'] ?? throw new InvalidArgumentException('Missing "id" field in the document data.'); |
| 125 | + |
| 126 | + $vector = !\array_key_exists($this->vectorFieldName, $document) || null === $document[$this->vectorFieldName] |
| 127 | + ? new NullVector() : new Vector($document[$this->vectorFieldName]); |
| 128 | + |
| 129 | + $score = $data['vector_distance'] ?? null; |
| 130 | + |
| 131 | + return new VectorDocument(Uuid::fromString($id), $vector, new Metadata(json_decode($document['metadata'], true)), $score); |
| 132 | + } |
| 133 | +} |
0 commit comments