/
01-pg-provision.py
61 lines (57 loc) · 1.26 KB
/
01-pg-provision.py
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
59
60
61
import psycopg2
from pgvector.psycopg2 import register_vector
import config
conn = psycopg2.connect(
user=config.PGUSER,
password=config.PGPASSWORD,
database=config.PGDATABASE,
host=config.PGHOST,
port=config.PGPORT,
)
cur = conn.cursor()
cur.execute("SET search_path TO " + 'test')
conn.commit()
cur.execute("""
create table perconavec (
id bigserial primary key,
content text,
url text,
embedding vector(1024)
);
""")
conn.commit()
cur.execute("""
create or replace function match_documents (
query_embedding vector(1024),
match_threshold float,
match_count int
)
returns table (
id bigint,
content text,
url text,
similarity float
)
language sql stable
as $$
select
perconavec.id,
perconavec.content,
perconavec.url,
1 - (perconavec.embedding <=> query_embedding) as similarity
from perconavec
where
perconavec.embedding <=> query_embedding < 1 - match_threshold
order by perconavec.embedding <=> query_embedding
limit match_count;
$$;
""")
conn.commit()
cur.execute("""
create index on perconavec using ivfflat (embedding vector_cosine_ops)
with
(lists = 100);
""")
conn.commit()
cur.close()
conn.close()