Files
supabase/examples/ai/edge-functions/supabase/migrations/20240410031515_vector-search.sql
Thor 雷神 Schaeff 7d9d5b3b02 chore: AI in EF start. (#22566)
* chore: AI in EF start.

* chore: complete demo.
2024-04-11 13:05:17 +08:00

25 lines
932 B
PL/PgSQL

-- Matches document sections using vector similarity search on embeddings
--
-- Returns a setof embeddings so that we can use PostgREST resource embeddings (joins with other tables)
-- Additional filtering like limits can be chained to this function call
create or replace function query_embeddings(embedding vector(384), match_threshold float)
returns setof embeddings
language plpgsql
as $$
#variable_conflict use_variable
begin
return query
select *
from embeddings
-- The inner product is negative, so we negate match_threshold
where embeddings.embedding <#> embedding < -match_threshold
-- Our embeddings are normalized to length 1, so cosine similarity
-- and inner product will produce the same query results.
-- Using inner product which can be computed faster.
--
-- For the different distance functions, see https://github.com/pgvector/pgvector
order by embeddings.embedding <#> embedding;
end;
$$;