-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_vector_schema.sql
More file actions
40 lines (40 loc) · 1.45 KB
/
Copy pathcreate_vector_schema.sql
File metadata and controls
40 lines (40 loc) · 1.45 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;
-- Create a table to store your documents
create table if not exists embeddings (
id bigserial primary key,
content text,
-- Represents the text content of the document
metadata jsonb,
-- Stores metadata like title, url, provider_id, etc.
embedding vector(1536) -- OpenAI / Gemini embedding dimensionality (usually 1536)
);
-- Turn on RLS
alter table embeddings enable row level security;
-- Allow everyone to read (for RAG context) - Adjust restricted if needed
create policy "Allow public read access" on embeddings for
select to public using (true);
-- Allow authenticated users (or service role) to insert/update
create policy "Allow auth insert" on embeddings for
insert to authenticated with check (true);
-- Create a function to search for documents
create or replace function match_documents (
query_embedding vector(1536),
match_threshold float,
match_count int
) returns table (
id bigint,
content text,
metadata jsonb,
similarity float
) language plpgsql as $$ begin return query
select embeddings.id,
embeddings.content,
embeddings.metadata,
1 - (embeddings.embedding <=> query_embedding) as similarity
from embeddings
where 1 - (embeddings.embedding <=> query_embedding) > match_threshold
order by embeddings.embedding <=> query_embedding
limit match_count;
end;
$$;