# Supabase

## Prerequisite

1. Register an account for [Supabase](https://supabase.com/)
2. Click **New project**

<figure><img src="https://662370747-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3VoWwSsyrEg0DEvIIjv9%2Fuploads%2Fgit-blob-c715f65183eee1d01b0936df3aafde0e3e751b4d%2Fimage%20(8)%20(2)%20(1).png?alt=media" alt=""><figcaption></figcaption></figure>

3. Input required fields

| Field Name                | Description                                        |
| ------------------------- | -------------------------------------------------- |
| **Name**                  | name of the project to be created. (e.g. AcmeCorp) |
| **Database** **Password** | password to your postgres database                 |

<figure><img src="https://662370747-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3VoWwSsyrEg0DEvIIjv9%2Fuploads%2Fgit-blob-e2b67a3295e23f5a96223d89314f29a8ea946ad4%2Fimage%20(25)%20(1).png?alt=media" alt=""><figcaption></figcaption></figure>

4. Click **Create new project** and wait for the project to finish setting up
5. Click **SQL Editor**

<figure><img src="https://662370747-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3VoWwSsyrEg0DEvIIjv9%2Fuploads%2Fgit-blob-750a4decae55891ed08df36711cb34cbe3582f16%2Fimage%20(7)%20(2).png?alt=media" alt=""><figcaption></figcaption></figure>

6. Click **New query**

<figure><img src="https://662370747-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3VoWwSsyrEg0DEvIIjv9%2Fuploads%2Fgit-blob-08d7ca748f5ed4b350ce65e13c047262f9cbbe90%2Fimage%20(36)%20(1).png?alt=media" alt=""><figcaption></figcaption></figure>

7. Copy and Paste the below SQL query and run it by `Ctrl + Enter` or click **RUN**. Take note of the table name and function name.

* **Table name**: `documents`
* **Query name**: `match_documents`

```plsql
-- Enable the pgvector extension to work with embedding vectors
create extension vector;

-- Create a table to store your documents
create table documents (
  id bigserial primary key,
  content text, -- corresponds to Document.pageContent
  metadata jsonb, -- corresponds to Document.metadata
  embedding vector(1536) -- 1536 works for OpenAI embeddings, change if needed
);

-- Create a function to search for documents
create function match_documents (
  query_embedding vector(1536),
  match_count int DEFAULT null,
  filter jsonb DEFAULT '{}'
) returns table (
  id bigint,
  content text,
  metadata jsonb,
  similarity float
)
language plpgsql
as $$
#variable_conflict use_column
begin
  return query
  select
    id,
    content,
    metadata,
    1 - (documents.embedding <=> query_embedding) as similarity
  from documents
  where metadata @> filter
  order by documents.embedding <=> query_embedding
  limit match_count;
end;
$$;

```

If some cases, you might be using [Record Manager](https://tailwindsdocs.innovativesol.com/readme/chatflows/langchain/record-managers) to keep track of the upserts and prevent duplications. Since Record Manager generates a random UUID for each embeddings, you will have to change the id column entity to text:

```sql
-- Enable the pgvector extension to work with embedding vectors
create extension vector;

-- Create a table to store your documents
create table documents (
  id text primary key, -- CHANGE TO TEXT
  content text,
  metadata jsonb,
  embedding vector(1536)
);

-- Create a function to search for documents
create function match_documents (
  query_embedding vector(1536),
  match_count int DEFAULT null,
  filter jsonb DEFAULT '{}'
) returns table (
  id text, -- CHANGE TO TEXT
  content text,
  metadata jsonb,
  similarity float
)
language plpgsql
as $$
#variable_conflict use_column
begin
  return query
  select
    id,
    content,
    metadata,
    1 - (documents.embedding <=> query_embedding) as similarity
  from documents
  where metadata @> filter
  order by documents.embedding <=> query_embedding
  limit match_count;
end;
$$;

```

<figure><img src="https://662370747-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3VoWwSsyrEg0DEvIIjv9%2Fuploads%2Fgit-blob-fdb3193ea6882277c0e3efdcd6ee8eef9cd97eeb%2Fimage%20(19)%20(1).png?alt=media" alt=""><figcaption></figcaption></figure>

## Setup

1. Click **Project Settings**
2. Get your **Project URL & API Key**
3. Copy and Paste each details (*API Key, URL, Table Name, Query Name*) into **Supabase** node

<figure><img src="https://662370747-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3VoWwSsyrEg0DEvIIjv9%2Fuploads%2Fgit-blob-1e867fb8b25da69be87498ce357a8671085752cd%2Fimage%20(85).png?alt=media" alt="" width="331"><figcaption></figcaption></figure>

4. **Document** can be connected with any node under [**Document Loader**](https://tailwindsdocs.innovativesol.com/readme/chatflows/langchain/document-loaders) category
5. **Embeddings** can be connected with any node under [**Embeddings** ](https://tailwindsdocs.innovativesol.com/readme/chatflows/langchain/embeddings)category

## Filtering

Let's say you have different documents upserted, each specified with a unique value under the metadata key `{source}`

<figure><img src="https://662370747-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3VoWwSsyrEg0DEvIIjv9%2Fuploads%2Fgit-blob-1188256371a312e3c7a25391407dbc17735b5c70%2FUntitled.png?alt=media" alt=""><figcaption></figcaption></figure>

You can use metadata filtering to query specific metadata:

**UI**

<figure><img src="https://662370747-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3VoWwSsyrEg0DEvIIjv9%2Fuploads%2Fgit-blob-1484e2b01ac9581d5f3b184afedbd08ec22efc42%2Fimage%20(9)%20(1)%20(1)%20(1)%20(1)%20(2)%20(1).png?alt=media" alt="" width="232"><figcaption></figcaption></figure>

**API**

```json
"overrideConfig": {
    "supabaseMetadataFilter": {
        "source": "henry"
    }
}
```

## Resources

* [LangChain JS Supabase](https://js.langchain.com/docs/modules/indexes/vector_stores/integrations/supabase)
* [Supabase Blog Post](https://supabase.com/blog/openai-embeddings-postgres-vector)
* [Metadata Filtering](https://js.langchain.com/docs/integrations/vectorstores/supabase#metadata-filtering)
