The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
updates to the enterprise architecture
Copplec9f8902909c7
diff --git a/apps/docs/pages/guides/ai/engineering-for-scale.mdx b/apps/docs/pages/guides/ai/engineering-for-scale.mdxindex 502ee246ead..a74bfe39d50 100644--- a/apps/docs/pages/guides/ai/engineering-for-scale.mdx+++ b/apps/docs/pages/guides/ai/engineering-for-scale.mdx@@ -12,80 +12,115 @@ Content sources for vectors can be extremely large. As you grow you should run y## Simple workloads-For small workloads it's typical to store your data in a single database. You can expose collections to your application with [views](/docs/guides/database/tables#views):--- Main Project- - `vecs` (schema)- - `collection_one`- - `collection_two`- - `collection_three`- - `public` (schema)- - `collection_one` (view)- - `collection_two` (view)- - `collection_three` (view)+For small workloads it's typical to store your data in a single database.++If you've used [Vecs](/docs/guides/ai/vecs-python-client) to create 3 different collections, you can expose collections to your web or mobile application using [views](/docs/guides/database/tables#views):++++For example, with 3 collections, called `docs`, `posts`, and `images`, we could expose the "docs" inside the public schema like this:++```sql+create view public.docs as+select+ id,+ embedding,+ metadata, # Expose the metadata as JSON+ (metadata->>'url')::text as url # Extract the URL as a string+from vector+```++You can then use any of the client libraries to access your collections within your applications:++{/* prettier-ignore */}+```js+const { data, error } = await supabase+ .from('docs')+ .select('id, embedding, metadata')+ .eq('url', '/hello-world')+```## Enterprise workloads-At this stage you should distribute the collections into their own projects, and start querying them directly wherever possible. You can still access them through foreign data wrappers.--- Project1- - `vecs`- - `collection_one`-- Project2- - `vecs`- - `collection_two`- - `collection_three`-- Main project- - `public`- - `collection_one` (foreign table)- - `collection_two` (foreign table)- - `collection_three` (foreign table)--## Choosing a database size--We can estimate the size you require either by the dimensionality of embeddings you require, or by the number of queries you expect to run. This is guidance only, and so you should monitor your database closely to manage any growth that you expect.--### By dimensionality--| `Compute Add-on` | `Dimensions` | `Estimated max vectors` |-| ---------------- | ------------ | ----------------------- |-| None | 512 | TBD |-| | 768 | TBD |-| | 1024 | TBD |-| Small | 512 | TBD |-| | 768 | TBD |-| | 1024 | TBD |-| Medium | 512 | TBD |-| | 768 | TBD |-| | 1024 | TBD |-| Large | 512 | TBD |-| | 768 | TBD |-| | 1024 | TBD |-| XL | 512 | TBD |-| | 768 | TBD |-| | 1024 | TBD |-| 2XL | 512 | TBD |-| | 768 | TBD |-| | 1024 | TBD |-| 4XL | 512 | TBD |-| | 768 | TBD |-| | 1024 | TBD |-| 8XL | 512 | TBD |-| | 768 | TBD |-| | 1024 | TBD |-| 8XL | 512 | TBD |-| | 768 | TBD |-| | 1024 | TBD |-| 12XL | 512 | TBD |-| | 768 | TBD |-| | 1024 | TBD |-| 16XL | 512 | TBD |-| | 768 | TBD |-| | 1024 | TBD |--### By queries per second--- [ ] TODO+As you move into production, we recommend running splitting your collections into separate projects. This is because it allows your vector stores to scale independently of your production data. Vectors typically grow faster than operational data, and they have different resource requirements. Running them on separate databases removes the single-point-of-failure.++++You can use as many secondary databases as you need to manage your collections. With this architecture, you have 2 options for accessing collections within your application:++1. Query the collections directly using Vecs.+2. Access the collections from your Primary database through a Wrapper.++You can use both of these in tandem to suit your use-case. We recommend option `1` wherever possible, as it offers the most scalability.++### Query collections using Vecs++Vecs provides methods for querying collections, either using a [cosine similarity function](https://supabase.github.io/vecs/api/#basic) or with [metadata filtering](https://supabase.github.io/vecs/api/#metadata-filtering).++```python+# cosine similarity+docs.query(query_vector=[0.4,0.5,0.6], limit=5)++# metadata filtering+docs.query(+ query_vector=[0.4,0.5,0.6],+ limit=5,+ filters={"year": {"$eq": 2012}}, # metadata filters+)+```++### Accessing external collections using Wrappers++Supabase supports [Foreign Data Wrappers](/blog/postgres-foreign-data-wrappers-rust). Wrappers allow you connect two databases together so that you can query them over the network.++This involves 2 steps: connecting to your remote database from the primary, and creating a Foreign Table.++#### Connecting your remote database++Inside your Primary database we need to provide the credentials to access the secondary database:++```sql+create extension postgres_fdw;++create server docs_server+foreign data wrapper postgres_fdw+options (host 'db.xxx.supabase.co', port '5432', dbname 'postgres');++create user mapping for docs_user+server docs_server+options (user 'postgres', password 'password');+```++#### Create a foreign table++We can now create a foreign table to access the data in our secondary project.++```sql+create foreign table docs (+ id text not null,+ embedding vector(1536),+ metadata jsonb,+ url text+)+server docs_server+options (schema_name 'public', table_name 'docs');+```++This looks very similar to our View example above, and you can continue to use the client libraries to access your collections through the foreign table:++{/* prettier-ignore */}+```js+const { data, error } = await supabase+ .from('docs')+ .select('id, embedding, metadata')+ .eq('url', '/hello-world')+```++### Enterprise architecture++This diagram provides an example architecture, allowing you to access the collections either with our client libraries or using Vecs. You can add as many secondary databases as you need, in this example we show one only:++export const Page = ({ children }) => <Layout meta={meta} children={children} />diff --git a/apps/docs/pages/guides/ai/structured-unstructured-embeddings.mdx b/apps/docs/pages/guides/ai/structured-unstructured-embeddings.mdxindex 05a3bf896c0..e1a18bc4126 100644--- a/apps/docs/pages/guides/ai/structured-unstructured-embeddings.mdx+++ b/apps/docs/pages/guides/ai/structured-unstructured-embeddings.mdx@@ -2,9 +2,12 @@ import Layout from '~/layouts/DefaultGuideLayout'export const meta = {id: 'structured-unstructured-embeddings',- title: 'Structured and Unstructured embeddings',- description: 'Structured and Unstructured embeddings',- sidebar_label: 'Structured and Unstructured embeddings',+ title: 'Structured and unstructured embeddings',+ description:+ 'Supabase is flexible enough to provide structured and unstructured embeddings using pgvector.',+ subtitle:+ 'Supabase is flexible enough to provide structured and unstructured embeddings using pgvector.',+ sidebar_label: 'Structured and unstructured embeddings',}Most vector stores treat embeddings like NoSQL, unstructured data. Supabase is flexible enough to fit either a structured or an unstructured approach.@@ -14,15 +17,20 @@ Compare these code snippets:## Structured```sql-create table (- id serial primary key,+create table docs (+ id uuid primary key,content text,- embedding vector(1536),- metadata jsonb+ url string,+ embedding vector(1536));++insert into docs+ (id, content, url, embedding)+values+ ('79409372-7556-4ccc-ab8f-5786a6cfa4f7', 'Hello world', '/hello-world', [100, 200, 300]);```-A structured approach is usually defined in SQL, and managed via Database Migrations.+A structured approach is usually defined in SQL, and managed via database [migrations](/docs/guides/getting-started/local-development#database-migrations).## Unstructured@@ -30,9 +38,14 @@ A structured approach is usually defined in SQL, and managed via Database Migratimport vecsdocs = vx.create_collection(name="docs", dimension=1536)++docs.upsert(vectors=[+ ('79409372-7556-4ccc-ab8f-5786a6cfa4f7', [100, 200, 300], { url: '/hello-world' })+])+```-An unstructured approach is usually defined in Python, and has a much looser table definition.+An unstructured approach is usually defined in Python and has a looser table definition, storing metadata as a json document along with the embedding.## Choosing the right modeldiff --git a/apps/docs/public/img/ai/scaling/multi-database.png b/apps/docs/public/img/ai/scaling/multi-database.pngnew file mode 100644index 00000000000..7593bc443fdBinary files /dev/null and b/apps/docs/public/img/ai/scaling/multi-database.png differdiff --git a/apps/docs/public/img/ai/scaling/single-database.png b/apps/docs/public/img/ai/scaling/single-database.pngnew file mode 100644index 00000000000..d48fed75d9cBinary files /dev/null and b/apps/docs/public/img/ai/scaling/single-database.png differdiff --git a/apps/docs/public/img/ai/scaling/with-secondaries.png b/apps/docs/public/img/ai/scaling/with-secondaries.pngnew file mode 100644index 00000000000..50c3798f7c6Binary files /dev/null and b/apps/docs/public/img/ai/scaling/with-secondaries.png differ