-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenable_pgvector.sql
More file actions
40 lines (37 loc) · 1.31 KB
/
Copy pathenable_pgvector.sql
File metadata and controls
40 lines (37 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
-- Enable the pgvector extension to work with embedding vectors
create extension if not exists vector;
-- Add an embedding column to the service_providers table
-- Note: Google's text-embedding-004 model generates 768-dimensional vectors
alter table public.service_providers
add column if not exists embedding vector(768);
-- Create a function to search for matching service providers
-- We'll use cosine distance (<=>) for the semantic similarity
create or replace function public.match_service_providers (
query_embedding vector(768),
match_threshold float,
match_count int
)
returns table (
id uuid,
business_name text,
category text,
description text,
similarity float
)
language sql stable
as $$
select
service_providers.id,
service_providers.business_name,
service_providers.category,
service_providers.description,
1 - (service_providers.embedding <=> query_embedding) as similarity
from service_providers
where 1 - (service_providers.embedding <=> query_embedding) > match_threshold
order by similarity desc
limit match_count;
$$;
-- Optional: Create an index for faster similarity searches (Recommended for production when you have many rows)
-- We use an HNSW index optimized for cosine distance
create index on public.service_providers
using hnsw (embedding vector_cosine_ops);