|
| 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\Supabase; |
| 13 | + |
| 14 | +use Symfony\AI\Platform\Vector\Vector; |
| 15 | +use Symfony\AI\Store\Document\Metadata; |
| 16 | +use Symfony\AI\Store\Document\VectorDocument; |
| 17 | +use Symfony\AI\Store\Exception\InvalidArgumentException; |
| 18 | +use Symfony\AI\Store\Exception\RuntimeException; |
| 19 | +use Symfony\AI\Store\StoreInterface; |
| 20 | +use Symfony\Component\Uid\Uuid; |
| 21 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 22 | + |
| 23 | +/** |
| 24 | + * @author Junaid Farooq <[email protected]> |
| 25 | + * |
| 26 | + * Requires pgvector extension to be enabled and a pre-configured table/function. |
| 27 | + * |
| 28 | + * @see https://github.com/pgvector/pgvector |
| 29 | + * @see https://supabase.com/docs/guides/ai/vector-columns |
| 30 | + * |
| 31 | + * Supabase store using REST & RPC. |
| 32 | + * |
| 33 | + * Note: Unlike Postgres Store, this store requires manual setup of: |
| 34 | + * 1. pgvector extension |
| 35 | + * 2. Table with vector column |
| 36 | + * 3. RPC function for similarity search |
| 37 | + * |
| 38 | + * This is because Supabase doesn't allow arbitrary SQL execution via REST API. |
| 39 | + */ |
| 40 | +final readonly class Store implements StoreInterface |
| 41 | +{ |
| 42 | + public function __construct( |
| 43 | + private HttpClientInterface $http, |
| 44 | + private string $url, |
| 45 | + private string $apiKey, |
| 46 | + private string $table = 'documents', |
| 47 | + private string $vectorFieldName = 'embedding', |
| 48 | + private int $vectorDimension = 1536, |
| 49 | + private string $functionName = 'match_documents', |
| 50 | + ) { |
| 51 | + } |
| 52 | + |
| 53 | + public function add(VectorDocument ...$documents): void |
| 54 | + { |
| 55 | + if (0 === \count($documents)) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + $rows = []; |
| 60 | + |
| 61 | + foreach ($documents as $document) { |
| 62 | + if (\count($document->vector->getData()) !== $this->vectorDimension) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + $rows[] = [ |
| 67 | + 'id' => $document->id->toRfc4122(), |
| 68 | + $this->vectorFieldName => $document->vector->getData(), |
| 69 | + 'metadata' => $document->metadata->getArrayCopy(), |
| 70 | + ]; |
| 71 | + } |
| 72 | + |
| 73 | + $chunkSize = 200; |
| 74 | + |
| 75 | + foreach (array_chunk($rows, $chunkSize) as $chunk) { |
| 76 | + $response = $this->http->request( |
| 77 | + 'POST', |
| 78 | + \sprintf('%s/rest/v1/%s', $this->url, $this->table), |
| 79 | + [ |
| 80 | + 'headers' => [ |
| 81 | + 'apikey' => $this->apiKey, |
| 82 | + 'Authorization' => 'Bearer '.$this->apiKey, |
| 83 | + 'Content-Type' => 'application/json', |
| 84 | + 'Prefer' => 'resolution=merge-duplicates', |
| 85 | + ], |
| 86 | + 'json' => $chunk, |
| 87 | + ] |
| 88 | + ); |
| 89 | + |
| 90 | + if ($response->getStatusCode() >= 400) { |
| 91 | + throw new RuntimeException('Supabase insert failed: '.$response->getContent(false)); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @param array{ |
| 98 | + * max_items?: int, |
| 99 | + * limit?: int, |
| 100 | + * min_score?: float |
| 101 | + * } $options |
| 102 | + */ |
| 103 | + public function query(Vector $vector, array $options = []): array |
| 104 | + { |
| 105 | + if (\count($vector->getData()) !== $this->vectorDimension) { |
| 106 | + throw new InvalidArgumentException("Vector dimension mismatch: expected {$this->vectorDimension}"); |
| 107 | + } |
| 108 | + |
| 109 | + $matchCount = $options['max_items'] ?? ($options['limit'] ?? 10); |
| 110 | + $threshold = $options['min_score'] ?? 0.0; |
| 111 | + |
| 112 | + $response = $this->http->request( |
| 113 | + 'POST', |
| 114 | + \sprintf('%s/rest/v1/rpc/%s', $this->url, $this->functionName), |
| 115 | + [ |
| 116 | + 'headers' => [ |
| 117 | + 'apikey' => $this->apiKey, |
| 118 | + 'Authorization' => 'Bearer '.$this->apiKey, |
| 119 | + 'Content-Type' => 'application/json', |
| 120 | + ], |
| 121 | + 'json' => [ |
| 122 | + 'query_embedding' => $vector->getData(), |
| 123 | + 'match_count' => $matchCount, |
| 124 | + 'match_threshold' => $threshold, |
| 125 | + ], |
| 126 | + ] |
| 127 | + ); |
| 128 | + |
| 129 | + if ($response->getStatusCode() >= 400) { |
| 130 | + throw new RuntimeException('Supabase query failed: '.$response->getContent(false)); |
| 131 | + } |
| 132 | + |
| 133 | + $records = json_decode($response->getContent(), true, 512, \JSON_THROW_ON_ERROR); |
| 134 | + $documents = []; |
| 135 | + |
| 136 | + foreach ($records as $record) { |
| 137 | + if ( |
| 138 | + !isset($record['id'], $record[$this->vectorFieldName], $record['metadata'], $record['score']) |
| 139 | + || !\is_string($record['id']) |
| 140 | + ) { |
| 141 | + continue; |
| 142 | + } |
| 143 | + |
| 144 | + $embedding = \is_array($record[$this->vectorFieldName]) ? $record[$this->vectorFieldName] : json_decode($record[$this->vectorFieldName] ?? '{}', true, 512, \JSON_THROW_ON_ERROR); |
| 145 | + $metadata = \is_array($record['metadata']) ? $record['metadata'] : json_decode($record['metadata'] ?? '{}', true, 512, \JSON_THROW_ON_ERROR); |
| 146 | + |
| 147 | + $documents[] = new VectorDocument( |
| 148 | + id: Uuid::fromString($record['id']), |
| 149 | + vector: new Vector($embedding), |
| 150 | + metadata: new Metadata($metadata), |
| 151 | + score: (float) $record['score'], |
| 152 | + ); |
| 153 | + } |
| 154 | + |
| 155 | + return $documents; |
| 156 | + } |
| 157 | +} |
0 commit comments