|
| 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\Weaviate; |
| 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\ManagedStoreInterface; |
| 20 | +use Symfony\AI\Store\StoreInterface; |
| 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 ManagedStoreInterface, StoreInterface |
| 28 | +{ |
| 29 | + public function __construct( |
| 30 | + private HttpClientInterface $httpClient, |
| 31 | + private string $endpointUrl, |
| 32 | + #[\SensitiveParameter] private string $apiKey, |
| 33 | + private string $collection, |
| 34 | + ) { |
| 35 | + } |
| 36 | + |
| 37 | + public function setup(array $options = []): void |
| 38 | + { |
| 39 | + if ([] !== $options) { |
| 40 | + throw new InvalidArgumentException('No supported options.'); |
| 41 | + } |
| 42 | + |
| 43 | + $this->request('POST', 'v1/schema', [ |
| 44 | + 'class' => $this->collection, |
| 45 | + ]); |
| 46 | + } |
| 47 | + |
| 48 | + public function add(VectorDocument ...$documents): void |
| 49 | + { |
| 50 | + $this->request('POST', 'v1/batch/objects', [ |
| 51 | + 'fields' => [ |
| 52 | + 'ALL', |
| 53 | + ], |
| 54 | + 'objects' => array_map($this->convertToIndexableArray(...), $documents), |
| 55 | + ]); |
| 56 | + } |
| 57 | + |
| 58 | + public function query(Vector $vector, array $options = []): array |
| 59 | + { |
| 60 | + $results = $this->request('POST', 'v1/graphql', [ |
| 61 | + 'query' => \sprintf('{ |
| 62 | + Get { |
| 63 | + %s ( |
| 64 | + nearVector: { |
| 65 | + vector: [%s] |
| 66 | + } |
| 67 | + ) { |
| 68 | + uuid, |
| 69 | + vector, |
| 70 | + _metadata |
| 71 | + } |
| 72 | + } |
| 73 | + }', $this->collection, implode(', ', $vector->getData())), |
| 74 | + ]); |
| 75 | + |
| 76 | + return array_map($this->convertToVectorDocument(...), $results['data']['Get'][$this->collection]); |
| 77 | + } |
| 78 | + |
| 79 | + public function drop(): void |
| 80 | + { |
| 81 | + $this->request('DELETE', \sprintf('v1/schema/%s', $this->collection), []); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @param array<string, mixed> $payload |
| 86 | + * |
| 87 | + * @return array<string, mixed> |
| 88 | + */ |
| 89 | + private function request(string $method, string $endpoint, array $payload): array |
| 90 | + { |
| 91 | + $url = \sprintf('%s/%s', $this->endpointUrl, $endpoint); |
| 92 | + |
| 93 | + $finalPayload = [ |
| 94 | + 'auth_bearer' => $this->apiKey, |
| 95 | + ]; |
| 96 | + |
| 97 | + if ([] !== $payload) { |
| 98 | + $finalPayload['json'] = $payload; |
| 99 | + } |
| 100 | + |
| 101 | + $result = $this->httpClient->request($method, $url, $finalPayload); |
| 102 | + |
| 103 | + return $result->toArray(); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @return array<string, mixed> |
| 108 | + */ |
| 109 | + private function convertToIndexableArray(VectorDocument $document): array |
| 110 | + { |
| 111 | + return [ |
| 112 | + 'class' => $this->collection, |
| 113 | + 'id' => $document->id->toRfc4122(), |
| 114 | + 'vector' => $document->vector->getData(), |
| 115 | + 'properties' => [ |
| 116 | + 'uuid' => $document->id->toRfc4122(), |
| 117 | + 'vector' => $document->vector->getData(), |
| 118 | + '_metadata' => json_encode($document->metadata->getArrayCopy()), |
| 119 | + ], |
| 120 | + ]; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @param array<string, mixed> $data |
| 125 | + */ |
| 126 | + private function convertToVectorDocument(array $data): VectorDocument |
| 127 | + { |
| 128 | + $id = $data['uuid'] ?? throw new InvalidArgumentException('Missing "id" field in the document data.'); |
| 129 | + |
| 130 | + $vector = !\array_key_exists('vector', $data) || null === $data['vector'] |
| 131 | + ? new NullVector() : new Vector($data['vector']); |
| 132 | + |
| 133 | + return new VectorDocument(Uuid::fromString($id), $vector, new Metadata(json_decode($data['_metadata'], true))); |
| 134 | + } |
| 135 | +} |
0 commit comments