More information on Supabase vector column here
Enable pgvector extensions:
- Go to the Database page in the Dashboard.
- Click on Extensions in the sidebar.
- Search for "vector" and enable the extension.
Create table documents:
create table documents (
id serial primary key,
title text not null
);Create table chunks:
create table chunks (
id serial primary key,
document_id serial not null,
body text not null,
embedding vector(1536),
foreign key (document_id) references documents (id)
);Create rpc function:
create or replace function match_documents (
query_embedding vector(1536),
match_threshold float,
match_count int,
document_ids int[]
)
returns table (
id bigint,
body text,
document_id int,
similarity float
)
language sql stable
as $$
select
chunks.id,
chunks.body,
chunks.document_id,
1 - (chunks.embedding <=> query_embedding) as similarity
from chunks
where
1 - (chunks.embedding <=> query_embedding) > match_threshold
AND chunks.document_id = any(document_ids)
order by similarity desc
limit match_count;
$$;