The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
reworks the docs a bit
Copple8cd2a8c9de42
diff --git a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.tsindex 68761ad2543..8889919ed42 100644--- a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts+++ b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts@@ -866,7 +866,7 @@ export const ai: NavMenuConstant = {{name: 'Quickstarts',url: undefined,- items: [{ name: 'Vecs for Data Scientists', url: '/guides/ai/vecs-for-data-scientists' }],+ items: [{ name: 'Python client', url: '/guides/ai/vecs-python-client' }],},{name: 'Guides',@@ -882,13 +882,17 @@ export const ai: NavMenuConstant = {url: undefined,items: [{- name: 'Docs search with OpenAI',- url: '/guides/ai/examples/docs-search',+ name: 'Headless Vector Search',+ url: '/guides/ai/examples/headless-vector-search',},{name: 'Hugging Face inference with Edge Functions',url: '/guides/ai/hugging-face-inference-edge-functions',},+ {+ name: 'Vector search with OpenAI and Next.js',+ url: '/guides/ai/examples/nextjs-vector-search',+ },],},{diff --git a/apps/docs/pages/guides/ai/engineering-for-scale.mdx b/apps/docs/pages/guides/ai/engineering-for-scale.mdxindex 205dbc7013f..502ee246ead 100644--- a/apps/docs/pages/guides/ai/engineering-for-scale.mdx+++ b/apps/docs/pages/guides/ai/engineering-for-scale.mdx@@ -8,11 +8,9 @@ export const meta = {sidebar_label: 'Engineering for Scale',}-Because content sources for vectors can be extremely large, as you grow you should run your Vector workloads across several independent databases (sometimes called "pods"). This allows each collection to scale independently.+Content sources for vectors can be extremely large. As you grow you should run your Vector workloads across several secondary databases (sometimes called "pods"), which allows each collection to scale independently.-- [ ] TODO: PMC - add diagram--## Small workloads+## Simple workloadsFor 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):diff --git a/apps/docs/pages/guides/ai/examples/headless-vector-search.mdx b/apps/docs/pages/guides/ai/examples/headless-vector-search.mdxnew file mode 100644index 00000000000..ca98d09eaa2--- /dev/null+++ b/apps/docs/pages/guides/ai/examples/headless-vector-search.mdx@@ -0,0 +1,24 @@+import Layout from '~/layouts/DefaultGuideLayout'+import StepHikeCompact from '~/components/StepHikeCompact'++export const meta = {+ title: 'Headless Vector Search',+ subtitle: 'A toolkit to perform vector similarity search on your knowledge base embeddings.',+ breadcrumb: 'AI Examples',+}++Provides a vector/similarity search for any documentation site or knowledge base. It's headless, so that you can integrate it into your existing website.++This toolkit consists of 2 parts:++- The [Headless Vector Search](https://github.com/supabase/headless-vector-search) template which you can deploy in your own organization.+- A [GitHub Action](https://github.com/supabase/embeddings-generator) which will ingest your markdown files, convert them to embeddings, and store them in your database.++## Tech stack++- Supabase: Database & Edge Functions.+- OpenAI: Embeddings and completions.+- GitHub Actions: for ingesting your markdown docs.++export const Page = ({ children }) => <Layout meta={meta} children={children} />+export default Pagediff --git a/apps/docs/pages/guides/ai/examples/docs-search.mdx b/apps/docs/pages/guides/ai/examples/nextjs-vector-search.mdxsimilarity index 98%rename from apps/docs/pages/guides/ai/examples/docs-search.mdxrename to apps/docs/pages/guides/ai/examples/nextjs-vector-search.mdxindex 433a7d69801..d2498e041eb 100644--- a/apps/docs/pages/guides/ai/examples/docs-search.mdx+++ b/apps/docs/pages/guides/ai/examples/nextjs-vector-search.mdx@@ -2,15 +2,15 @@ import Layout from '~/layouts/DefaultGuideLayout'import StepHikeCompact from '~/components/StepHikeCompact'export const meta = {- title: 'Docs search using OpenAI',+ title: 'Vector search using OpenAI and Next',subtitle:'Learn how to build a ChatGPT-style doc search powered by Next.js, OpenAI, and Supabase.',- breadcrumb: 'OpenAI',+ breadcrumb: 'AI Examples',video: 'https://www.youtube.com/v/xmfNUCjszh4',tocVideo: 'xmfNUCjszh4',}-In this tutorial we'll look at how you can build a custom ChatGPT-like search experience for your own knowledge base. You will:+In this tutorial we'll build a custom ChatGPT-like search experience for your own knowledge base using Next.js. You will:1. Convert your markdown into embeddings using OpenAI.2. Store you embeddings in Postgres using pgvector.diff --git a/apps/docs/pages/guides/ai/structured-unstructured-embeddings.mdx b/apps/docs/pages/guides/ai/structured-unstructured-embeddings.mdxindex 554d4ad28d6..90c69fb4f90 100644--- a/apps/docs/pages/guides/ai/structured-unstructured-embeddings.mdx+++ b/apps/docs/pages/guides/ai/structured-unstructured-embeddings.mdx@@ -7,9 +7,9 @@ export const meta = {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. Compare these code snippets -+Most vector stores treat embeddings like NoSQL, unstructured data. Supabase is flexible enough to fit either a structured or an unstructured approach. Compare these code snippets:-Structured (SQL):+**Structured (SQL)**```sqlcreate table (@@ -20,7 +20,7 @@ create table ();```-Unstructured (Python):+**Unstructured (Python)**```pyimport vecs@@ -28,11 +28,11 @@ import vecsdocs = vx.create_collection(name="docs", dimension=1536)```-Both of these create a table where you can store your embeddings and some metadata.-## Choosing the right model-- Structured embeddings are typically co-located with some content that's already stored in your database.+Both approaches create a table where you can store your embeddings and some metadata. You should choose the best approach for your use-case.++- Structured embeddings are typically co-located with some content that is already stored in your database.- Unstructured embeddings are typically defined at runtime, better-suited for a large body of external content.Both approaches are fine, and the one you should choose depends on your use-case.diff --git a/apps/docs/pages/guides/ai/vecs-for-data-scientists.mdx b/apps/docs/pages/guides/ai/vecs-for-data-scientists.mdxdeleted file mode 100644index 6f1ab7d25d6..00000000000--- a/apps/docs/pages/guides/ai/vecs-for-data-scientists.mdx+++ /dev/null@@ -1,121 +0,0 @@-import Layout from '~/layouts/DefaultGuideLayout'-import StepHikeCompact from '~/components/StepHikeCompact'--export const meta = {- id: 'ai-vecs-for-data-scientists',- title: 'Vecs for Data Scientists',- subtitle:- 'Vecs is a Python client for managing vector stores in PostgreSQL using the pgvector extension.',- breadcrumb: 'AI Quickstarts',-}--<StepHikeCompact>-- <StepHikeCompact.Step step={1}>- <StepHikeCompact.Details title="Get a database running locally">-- Follow the Supabase CLI [Installation](/docs/guides/cli#installation) guides to install the CLI on your machine.-- You can now start a local Postgres instance in any folders using the `init` and `start` commands. Make sure you have Docker running!-- </StepHikeCompact.Details>-- <StepHikeCompact.Code>-- ```bash- # Initialize your project- supabase init-- # Start Postgres- supabase start- ````-- </StepHikeCompact.Code>-- </StepHikeCompact.Step>-- <StepHikeCompact.Step step={2}>-- <StepHikeCompact.Details title="Create a collection">-- [ ] Todo - what is the "npm" version of Python? Use venv?-- Run this in your Terminal-- </StepHikeCompact.Details>-- <StepHikeCompact.Code>-- ```bash Terminal- import vecs-- # create vector store client- vx = vecs.create_client("postgresql://postgres:postgres@localhost:54322/postgres")-- # create a collection of vectors with 3 dimensions- docs = vx.create_collection(name="docs", dimension=3)- ```-- </StepHikeCompact.Code>-- </StepHikeCompact.Step>-- <StepHikeCompact.Step step={3}>- <StepHikeCompact.Details title="Add some embeddings">-- Add some embeddings.-- </StepHikeCompact.Details>-- <StepHikeCompact.Code>-- ```bash Terminal- import vecs-- # create vector store client- docs = vecs.get_collection(name="docs")-- # a collection of vectors with 3 dimensions- vectors=[- ("vec0", [0.1, 0.2, 0.3], {"year": 1973}),- ("vec1", [0.7, 0.8, 0.9], {"year": 2012})- ]-- # insert our vectors- docs.upsert(vectors=vectors)- ```-- </StepHikeCompact.Code>-- </StepHikeCompact.Step>-- <StepHikeCompact.Step step={4}>- <StepHikeCompact.Details title="Query your embeddings">-- Query your embeddings.-- </StepHikeCompact.Details>- <StepHikeCompact.Code>-- ```bash Terminal- import Vecs-- docs = vecs.get_collection(name="docs")-- # query the collection filtering metadata for "year" = 2012- docs.query(- query_vector=[0.4,0.5,0.6], # required- limit=1, # number of records to return- filters={"year": {"$eq": 2012}}, # metadata filters- )- ```-- </StepHikeCompact.Code>-- </StepHikeCompact.Step>--</StepHikeCompact>--export const Page = ({ children }) => <Layout meta={meta} children={children} hideToc={true} />--export default Pagediff --git a/apps/docs/pages/guides/ai/vecs-python-client.mdx b/apps/docs/pages/guides/ai/vecs-python-client.mdxnew file mode 100644index 00000000000..66c23bd0b76--- /dev/null+++ b/apps/docs/pages/guides/ai/vecs-python-client.mdx@@ -0,0 +1,218 @@+import Layout from '~/layouts/DefaultGuideLayout'+import StepHikeCompact from '~/components/StepHikeCompact'++export const meta = {+ id: 'ai-vecs-python-client',+ title: 'Python client',+ subtitle: 'Manage unstructured vector stores in PostgreSQL.',+ breadcrumb: 'AI Quickstarts',+}++Supabase provides a Python client called [`vecs`](https://github.com/supabase/vecs) for managing unstructured vector stores. This client provides a set of useful tools for creating and querying collections in PostgreSQL using the [pgvector](/docs/guides/database/extensions/pgvector) extension.++## Quick start++Let's see how Vecs works using a local database. Make sure you have the Supabase CLI [installed](/docs/guides/cli#installation) on your machine.++### Initialize your project++Start a local Postgres instance in any folder using the `init` and `start` commands. Make sure you have Docker running!++```bash+# Initialize your project+supabase init++# Start Postgres+supabase start+```++### Create a collection++Inside a Python shell, run the following commands to create a new collection called "docs", with 3 dimensions.++```py+import vecs++# create vector store client+vx = vecs.create_client("postgresql://postgres:postgres@localhost:54322/postgres")++# create a collection of vectors with 3 dimensions+docs = vx.create_collection(name="docs", dimension=3)+```++### Add embeddings++Now we can insert some embeddings into our "docs" collection using the `usert()` command:++```py+import vecs++# create vector store client+docs = vecs.get_collection(name="docs")++# a collection of vectors with 3 dimensions+vectors=[+ ("vec0", [0.1, 0.2, 0.3], {"year": 1973}),+ ("vec1", [0.7, 0.8, 0.9], {"year": 2012})+]++# insert our vectors+docs.upsert(vectors=vectors)+```++### Query the collection++You can now query the collection to retrieve a relevant match:++```py+import vecs++docs = vecs.get_collection(name="docs")++# query the collection filtering metadata for "year" = 2012+docs.query(+ query_vector=[0.4,0.5,0.6], # required+ limit=1, # number of records to return+ filters={"year": {"$eq": 2012}}, # metadata filters+)+```++## API++Find the full API in the [official API docs](https://supabase.github.io/vecs/api).++### Connecting++Before you can interact with vecs, create the client to communicate with Postgres.++```python+import vecs++DB_CONNECTION = "postgresql://<user>:<password>@<host>:<port>/<db_name>"++# create vector store client+vx = vecs.create_client(DB_CONNECTION)+```++### Create collection++You can create a collection to store vectors specifying the collections name and the number of dimensions in the vectors you intend to store.++```python+docs = vx.create_collection(name="docs", dimension=3)+```++If another collection exists with the same name,++### Get an existing collection++To access a previously created collection, use `get_collection` to retrieve it by name++```python+docs = vx.get_collection(name="docs")+```++### Upserting vectors++`vecs` combines the concepts of "insert" and "update" into "upsert". Upserting records adds them to the collection if the `id` is not present, or updates the existing record if the `id` does exist.++```python+# add records to the collection+docs.upsert(+ vectors=[+ (+ "vec0", # the vector's identifier+ [0.1, 0.2, 0.3], # the vector. list or np.array+ {"year": 1973} # associated metadata+ ),+ (+ "vec1",+ [0.7, 0.8, 0.9],+ {"year": 2012}+ )+ ]+)+```++### Create an index++Collections can be queried immediately after being created.+However, for good performance, the collection should be indexed after records have been upserted.++Indexes should be created **after** the collection has been populated with records. Building an index on an empty collection will result in significantly reduced recall. Once the index has been created you can still upsert new documents into the collection but you should rebuild the index if the size of the collection more than doubles.++Only one index may exist per-collection. By default, creating an index will replace any existing index.++To create an index:++```python+##+# INSERT RECORDS HERE+##++# index the collection to be queried by cosine distance+docs.create_index(measure=vecs.IndexMeasure.cosine_distance)+```++Available options for query `measure` are:++- `vecs.IndexMeasure.cosine_distance`+- `vecs.IndexMeasure.l2_distance`+- `vecs.IndexMeasure.max_inner_product`++which correspond to different methods for comparing query vectors to the vectors in the database.++If you aren't sure which to use, stick with the default (cosine_distance) by omitting the parameter i.e.: `docs.create_index()`.++<Admonition type="note">++The time required to create an index grows with the number of records and size of vectors. For a few thousand records expect sub-minute a response in under a minute. It may take a few minutes for larger collections.++</Admonition>++### Query++Be aware that indexes are essential for good performance. If you do not create an index, every query will return a warning that includes the `IndexMeasure` you should index.++#### Basic++The simplest form of search is to provide a query vector.++```python+docs.query(+ query_vector=[0.4,0.5,0.6], # required+ limit=5, # number of records to return+ filters={}, # metadata filters+ measure="cosine_distance", # distance measure to use+ include_value=False, # should distance measure values be returned?+ include_metadata=False, # should record metadata be returned?+)+```++Which returns a list of vector record `ids`.++#### Metadata Filtering++The metadata that is associated with each record can also be filtered during a query.++As an example, `{"year": {"$eq": 2005}}` filters a `year` metadata key to be equal to 2005++In context:++```python+docs.query(+ query_vector=[0.4,0.5,0.6],+ filters={"year": {"$eq": 2012}}, # metadata filters+)+```++For a complete reference, see the [metadata guide](https://supabase.github.io/vecs/concepts_metadata/).++## Resources++- Official Vecs Documentation:+- Source Code:++export const Page = ({ children }) => <Layout meta={meta} children={children} />++export default Page