|
| 1 | +Supabase Bridge |
| 2 | +=============== |
| 3 | + |
| 4 | +The Supabase bridge provides vector storage capabilities using Supabase's pgvector extension through the REST API. |
| 5 | + |
| 6 | +.. note:: |
| 7 | + |
| 8 | +* Unlike the Postgres Store, the Supabase Store requires manual setup of the database schema |
| 9 | +* because Supabase doesn't allow arbitrary SQL execution via REST API. |
| 10 | + |
| 11 | +Installation |
| 12 | +------------ |
| 13 | + |
| 14 | +The Supabase bridge requires the pgvector extension and pre-configured database objects. |
| 15 | + |
| 16 | +Requirements |
| 17 | +~~~~~~~~~~~~ |
| 18 | + |
| 19 | +* Supabase project with pgvector extension enabled |
| 20 | +* Pre-configured table with vector column |
| 21 | +* Pre-configured RPC function for similarity search |
| 22 | + |
| 23 | +Database Setup |
| 24 | +-------------- |
| 25 | + |
| 26 | +Execute the following SQL commands in your Supabase SQL Editor: |
| 27 | + |
| 28 | +Enable ``pgvector`` extension |
| 29 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 30 | + |
| 31 | +.. code-block:: sql |
| 32 | +
|
| 33 | + CREATE EXTENSION IF NOT EXISTS vector; |
| 34 | +
|
| 35 | +Create the `documents` table |
| 36 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 37 | + |
| 38 | +.. code-block:: sql |
| 39 | +
|
| 40 | + CREATE TABLE IF NOT EXISTS documents ( |
| 41 | + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), |
| 42 | + embedding vector(1536) NOT NULL, |
| 43 | + metadata JSONB |
| 44 | + ); |
| 45 | +
|
| 46 | +Create the similarity search function |
| 47 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 48 | + |
| 49 | +.. code-block:: sql |
| 50 | +
|
| 51 | + CREATE OR REPLACE FUNCTION match_documents( |
| 52 | + query_embedding vector(1536), |
| 53 | + match_count int DEFAULT 10, |
| 54 | + match_threshold float DEFAULT 0.0 |
| 55 | + ) |
| 56 | + RETURNS TABLE ( |
| 57 | + id UUID, |
| 58 | + embedding vector, |
| 59 | + metadata JSONB, |
| 60 | + score float |
| 61 | + ) |
| 62 | + LANGUAGE sql |
| 63 | + AS $$ |
| 64 | + SELECT |
| 65 | + documents.id, |
| 66 | + documents.embedding, |
| 67 | + documents.metadata, |
| 68 | + 1- (documents.embedding <=> query_embedding) AS score |
| 69 | + FROM documents |
| 70 | + WHERE 1- (documents.embedding <=> query_embedding) >= match_threshold |
| 71 | + ORDER BY documents.embedding <=> query_embedding ASC |
| 72 | + LIMIT match_count; |
| 73 | + $$; |
| 74 | +
|
| 75 | +Create an index for better performance |
| 76 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 77 | + |
| 78 | +.. code-block:: sql |
| 79 | +
|
| 80 | + CREATE INDEX IF NOT EXISTS documents_embedding_idx |
| 81 | + ON documents USING ivfflat (embedding vector_cosine_ops); |
| 82 | +
|
| 83 | +Configuration |
| 84 | +------------- |
| 85 | + |
| 86 | +Basic Configuration |
| 87 | +~~~~~~~~~~~~~~~~~~~ |
| 88 | + |
| 89 | +.. code-block:: php |
| 90 | +
|
| 91 | + use Symfony\AI\Store\Bridge\Supabase\Store; |
| 92 | + use Symfony\Component\HttpClient\HttpClient; |
| 93 | +
|
| 94 | + $store = new Store( |
| 95 | + HttpClient::create(), |
| 96 | + 'https://your-project.supabase.co', |
| 97 | + 'your-anon-key', |
| 98 | + 'documents', // table name |
| 99 | + 'embedding', // vector field name |
| 100 | + 1536, // vector dimension |
| 101 | + 'match_documents' // function name |
| 102 | + ); |
| 103 | +
|
| 104 | +Bundle Configuration |
| 105 | +~~~~~~~~~~~~~~~~~~~~ |
| 106 | + |
| 107 | +.. code-block:: yaml |
| 108 | +
|
| 109 | + # config/packages/ai.yaml |
| 110 | + ai: |
| 111 | + store: |
| 112 | + supabase: |
| 113 | + my_supabase_store: |
| 114 | + url: 'https://your-project.supabase.co' |
| 115 | + api_key: '%env(SUPABASE_API_KEY)%' |
| 116 | + table: 'documents' |
| 117 | + vector_field: 'embedding' |
| 118 | + vector_dimension: 1536 |
| 119 | + function_name: 'match_documents' |
| 120 | +
|
| 121 | +Environment Variables |
| 122 | +~~~~~~~~~~~~~~~~~~~~~ |
| 123 | + |
| 124 | +.. code-block:: bash |
| 125 | +
|
| 126 | + # .env.local |
| 127 | + SUPABASE_URL=https://your-project.supabase.co |
| 128 | + SUPABASE_API_KEY=your-supabase-anon-key |
| 129 | +
|
| 130 | +Usage |
| 131 | +----- |
| 132 | + |
| 133 | +Adding Documents |
| 134 | +~~~~~~~~~~~~~~~~ |
| 135 | + |
| 136 | +.. code-block:: php |
| 137 | +
|
| 138 | + use Symfony\AI\Platform\Vector\Vector; |
| 139 | + use Symfony\AI\Store\Document\Metadata; |
| 140 | + use Symfony\AI\Store\Document\VectorDocument; |
| 141 | + use Symfony\Component\Uid\Uuid; |
| 142 | +
|
| 143 | + $document = new VectorDocument( |
| 144 | + Uuid::v4(), |
| 145 | + new Vector([0.1, 0.2, 0.3, /* ... 1536 dimensions */]), |
| 146 | + new Metadata(['title' => 'My Document', 'category' => 'example']) |
| 147 | + ); |
| 148 | +
|
| 149 | + $store->add($document); |
| 150 | +
|
| 151 | +Querying Documents |
| 152 | +~~~~~~~~~~~~~~~~~~ |
| 153 | + |
| 154 | +.. code-block:: php |
| 155 | +
|
| 156 | + $queryVector = new Vector([0.1, 0.2, 0.3, /* ... 1536 dimensions */]); |
| 157 | +
|
| 158 | + $results = $store->query($queryVector, [ |
| 159 | + 'max_items' => 10, |
| 160 | + 'min_score' => 0.7 |
| 161 | + ]); |
| 162 | +
|
| 163 | + foreach ($results as $document) { |
| 164 | + echo "ID: " . $document->id . "\n"; |
| 165 | + echo "Score: " . $document->score . "\n"; |
| 166 | + echo "Metadata: " . json_encode($document->metadata->getArrayCopy()) . "\n"; |
| 167 | + } |
| 168 | +
|
| 169 | +Customization |
| 170 | +------------- |
| 171 | + |
| 172 | +You can customize the Supabase setup for different requirements: |
| 173 | + |
| 174 | +Table Name |
| 175 | +~~~~~~~~~~ |
| 176 | + |
| 177 | +Change ``documents`` to your preferred table name in both the SQL setup and configuration. |
| 178 | + |
| 179 | +Vector Field Name |
| 180 | +~~~~~~~~~~~~~~~~~ |
| 181 | + |
| 182 | +Change ``embedding`` to your preferred field name in both the SQL setup and configuration. |
| 183 | + |
| 184 | +Vector Dimension |
| 185 | +~~~~~~~~~~~~~~~~ |
| 186 | + |
| 187 | +Change ``1536`` to match your embedding model's dimensions in both the SQL setup and configuration. |
| 188 | + |
| 189 | +Distance Metric |
| 190 | +~~~~~~~~~~~~~~~ |
| 191 | + |
| 192 | +* Cosine: ``<=>`` (default, recommended for most embeddings) |
| 193 | +* Euclidean: ``<->`` |
| 194 | +* Inner Product: ``<#>`` |
| 195 | + |
| 196 | +Index Type |
| 197 | +~~~~~~~~~~ |
| 198 | + |
| 199 | +* ``ivfflat``: Good balance of speed and accuracy |
| 200 | +* ``hnsw``: Better for high-dimensional vectors (requires PostgreSQL 14+) |
| 201 | + |
| 202 | +Limitations |
| 203 | +----------- |
| 204 | + |
| 205 | +* Manual schema setup required (no automatic table creation) |
| 206 | +* Limited to Supabase's REST API capabilities |
| 207 | +* Requires pre-configured RPC functions for complex queries |
| 208 | +* Vector dimension must be consistent across all documents |
| 209 | + |
| 210 | +Performance Considerations |
| 211 | +-------------------------- |
| 212 | + |
| 213 | +* Use appropriate index types based on your vector dimensions |
| 214 | +* Consider using ``hnsw`` indexes for high-dimensional vectors |
| 215 | +* Batch document insertions when possible (up to 200 documents per request) |
| 216 | +* Monitor your Supabase usage limits and quotas |
| 217 | + |
| 218 | +Security Considerations |
| 219 | +----------------------- |
| 220 | + |
| 221 | +* Use row-level security (RLS) policies if needed |
| 222 | +* Consider using service role keys for server-side operations |
| 223 | +* Validate vector dimensions in your application code |
| 224 | +* Implement proper error handling for API failures |
| 225 | + |
| 226 | +Additional Resources |
| 227 | +-------------------- |
| 228 | + |
| 229 | +* [Supabase Vector Documentation](https://supabase.com/docs/guides/ai/vector-columns) |
| 230 | +* [pgvector Documentation](https://github.com/pgvector/pgvector) |
| 231 | +* [Symfony AI Store Documentation](../../../README.md) |
0 commit comments