supabase-schema.sql
pgvector table, HNSW index and match_documents function.
Part of AI Chatbot for Your Docs (RAG) by Admin
-- Docs chatbot: run in Supabase → SQL Editor. create extension if not exists vector with schema extensions; create table public.documents ( id bigint generated always as identity primary key, source text not null, -- file path or URL the chunk came from chunk_index int not null, content text not null, embedding extensions.vector(1536) not null, -- text-embedding-3-small unique (source, chunk_index) ); create index documents_embedding_idx on public.documents using hnsw (embedding extensions.vector_cosine_ops); -- RLS on, no policies: only the server (service role key) reads and writes. alter table public.documents enable row level security; create or replace function public.match_documents( query_embedding extensions.vector(1536), match_count int default 6, min_similarity float default 0.3 ) returns table (id bigint, source text, content text, similarity float) language sql stable set search_path = public, extensions as $$ select d.id, d.source, d.content, 1 - (d.embedding <=> query_embedding) as similarity from public.documents d where 1 - (d.embedding <=> query_embedding) > min_similarity order by d.embedding <=> query_embedding limit match_count; $$;