generated from lehmannerich/library-gpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.sql
58 lines (55 loc) · 1.1 KB
/
schema.sql
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
-- RUN 1st
create extension vector;
-- RUN 2nd
create table pg (
id bigserial primary key,
essay_title text,
essay_url text,
essay_date text,
essay_thanks text,
content text,
content_length bigint,
content_tokens bigint,
embedding vector (1536)
);
-- RUN 3rd after running the scripts
create or replace function pg_search (
query_embedding vector(1536),
similarity_threshold float,
match_count int
)
returns table (
id bigint,
essay_title text,
essay_url text,
essay_date text,
essay_thanks text,
content text,
content_length bigint,
content_tokens bigint,
similarity float
)
language plpgsql
as $$
begin
return query
select
pg.id,
pg.essay_title,
pg.essay_url,
pg.essay_date,
pg.essay_thanks,
pg.content,
pg.content_length,
pg.content_tokens,
1 - (pg.embedding <=> query_embedding) as similarity
from pg
where 1 - (pg.embedding <=> query_embedding) > similarity_threshold
order by pg.embedding <=> query_embedding
limit match_count;
end;
$$;
-- RUN 4th
create index on pg
using ivfflat (embedding vector_cosine_ops)
with (lists = 100);