|
| 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\Cloudflare; |
| 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 $accountId, |
| 32 | + #[\SensitiveParameter] private string $apiKey, |
| 33 | + private string $index, |
| 34 | + private int $dimensions = 1536, |
| 35 | + private string $metric = 'cosine', |
| 36 | + private string $endpointUrl = 'https://api.cloudflare.com/client/v4/accounts', |
| 37 | + ) { |
| 38 | + } |
| 39 | + |
| 40 | + public function setup(array $options = []): void |
| 41 | + { |
| 42 | + if ([] !== $options) { |
| 43 | + throw new InvalidArgumentException('No supported options.'); |
| 44 | + } |
| 45 | + |
| 46 | + $this->request('POST', 'vectorize/v2/indexes', [ |
| 47 | + 'config' => [ |
| 48 | + 'dimensions' => $this->dimensions, |
| 49 | + 'metric' => $this->metric, |
| 50 | + ], |
| 51 | + 'name' => $this->index, |
| 52 | + ]); |
| 53 | + } |
| 54 | + |
| 55 | + public function drop(): void |
| 56 | + { |
| 57 | + $this->request('DELETE', \sprintf('vectorize/v2/indexes/%s', $this->index)); |
| 58 | + } |
| 59 | + |
| 60 | + public function add(VectorDocument ...$documents): void |
| 61 | + { |
| 62 | + $payload = array_map( |
| 63 | + $this->convertToIndexableArray(...), |
| 64 | + $documents, |
| 65 | + ); |
| 66 | + |
| 67 | + $this->request('POST', \sprintf('vectorize/v2/indexes/%s/upsert', $this->index), function () use ($payload) { |
| 68 | + foreach ($payload as $entry) { |
| 69 | + yield json_encode($entry).\PHP_EOL; |
| 70 | + } |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + public function query(Vector $vector, array $options = []): array |
| 75 | + { |
| 76 | + $results = $this->request('POST', \sprintf('vectorize/v2/indexes/%s/query', $this->index), [ |
| 77 | + 'vector' => $vector->getData(), |
| 78 | + 'returnValues' => true, |
| 79 | + 'returnMetadata' => 'all', |
| 80 | + ]); |
| 81 | + |
| 82 | + return array_map($this->convertToVectorDocument(...), $results['result']['matches']); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @param array<string, mixed> $payload |
| 87 | + * |
| 88 | + * @return array<string, mixed> |
| 89 | + */ |
| 90 | + private function request(string $method, string $endpoint, \Closure|array $payload = []): array |
| 91 | + { |
| 92 | + $url = \sprintf('%s/%s/%s', $this->endpointUrl, $this->accountId, $endpoint); |
| 93 | + |
| 94 | + $options = [ |
| 95 | + 'auth_bearer' => $this->apiKey, |
| 96 | + ]; |
| 97 | + |
| 98 | + if ($payload instanceof \Closure) { |
| 99 | + $options['headers'] = [ |
| 100 | + 'Content-Type' => 'application/x-ndjson', |
| 101 | + ]; |
| 102 | + |
| 103 | + $options['body'] = $payload(); |
| 104 | + } |
| 105 | + |
| 106 | + if (\is_array($payload)) { |
| 107 | + $options['json'] = $payload; |
| 108 | + } |
| 109 | + |
| 110 | + $response = $this->httpClient->request($method, $url, $options); |
| 111 | + |
| 112 | + return $response->toArray(); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @return array<string, mixed> |
| 117 | + */ |
| 118 | + private function convertToIndexableArray(VectorDocument $document): array |
| 119 | + { |
| 120 | + return [ |
| 121 | + 'id' => $document->id->toRfc4122(), |
| 122 | + 'values' => $document->vector->getData(), |
| 123 | + 'metadata' => $document->metadata->getArrayCopy(), |
| 124 | + ]; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @param array<string, mixed> $data |
| 129 | + */ |
| 130 | + private function convertToVectorDocument(array $data): VectorDocument |
| 131 | + { |
| 132 | + $id = $data['id'] ?? throw new InvalidArgumentException('Missing "id" field in the document data.'); |
| 133 | + |
| 134 | + $vector = !\array_key_exists('values', $data) || null === $data['values'] |
| 135 | + ? new NullVector() |
| 136 | + : new Vector($data['values']); |
| 137 | + |
| 138 | + return new VectorDocument( |
| 139 | + id: Uuid::fromString($id), |
| 140 | + vector: $vector, |
| 141 | + metadata: new Metadata($data['metadata']), |
| 142 | + score: $data['score'] ?? null |
| 143 | + ); |
| 144 | + } |
| 145 | +} |
0 commit comments