Skip to content

Instantly share code, notes, and snippets.

@juliooa
Created March 30, 2024 04:36
Show Gist options
  • Save juliooa/96f579e30570146c6544a2fcedd7e1a0 to your computer and use it in GitHub Desktop.
Save juliooa/96f579e30570146c6544a2fcedd7e1a0 to your computer and use it in GitHub Desktop.
Similarity function for embeddings, to use in Supabase
CREATE OR REPLACE FUNCTION public.match_documents(
query_embedding vector,
match_threshold double precision,
match_count integer
) RETURNS TABLE(
id bigint,
content text,
similarity double precision,
metadata jsonb
) LANGUAGE sql STABLE AS $function$
select documents.id,
documents.content,
1 - (documents.embedding <=> query_embedding) as similarity,
documents.metadata
from documents
where documents.embedding <=> query_embedding < 1 - match_threshold
order by documents.embedding <=> query_embedding
limit match_count;
$function$;
@juliooa
Copy link
Author

juliooa commented Mar 30, 2024

You should have a table called documents with columns: id, content, metadata, embedding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment