-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathschema.sql
49 lines (46 loc) · 1012 Bytes
/
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
-- RUN 1st
create extension vector;
-- RUN 2nd
create table chatgpt (
id bigserial primary key,
content text,
content_length bigint,
content_tokens bigint,
page_num bigint,
embedding vector (1536)
);
-- RUN 3rd after running the scripts
create or replace function chatgpt_search (
query_embedding vector(1536),
similarity_threshold float,
match_count int
)
returns table (
id bigint,
content text,
content_length bigint,
content_tokens bigint,
page_num bigint,
similarity float
)
language plpgsql
as $$
begin
return query
select
chatgpt.id,
chatgpt.content,
chatgpt.content_length,
chatgpt.content_tokens,
chatgpt.page_num,
1 - (chatgpt.embedding <=> query_embedding) as similarity
from chatgpt
where 1 - (chatgpt.embedding <=> query_embedding) > similarity_threshold
order by chatgpt.embedding <=> query_embedding
limit match_count;
end;
$$;
-- RUN 4th
create index on chatgpt
using ivfflat (embedding vector_cosine_ops)
with (lists = 100);