-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
24 lines (21 loc) · 850 Bytes
/
Copy pathschema.sql
File metadata and controls
24 lines (21 loc) · 850 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
create extension if not exists vector;
create table if not exists chunks (
id bigserial primary key,
source text not null,
content text not null,
embedding vector(384) not null -- all-MiniLM-L6-v2 = 384 dims
);
-- cosine-distance index; lists tuned for small corpora
create index if not exists chunks_embedding_idx
on chunks using ivfflat (embedding vector_cosine_ops) with (lists = 10);
-- top-k retrieval, returned as (source, content, similarity)
create or replace function match_chunks(query_embedding vector(384), k int)
returns table (source text, content text, similarity float)
language sql stable as $$
select c.source,
c.content,
1 - (c.embedding <=> query_embedding) as similarity
from chunks c
order by c.embedding <=> query_embedding
limit k;
$$;