-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpsycopg_example.py
151 lines (137 loc) · 5.07 KB
/
psycopg_example.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import os
import numpy as np
import psycopg
from scipy.sparse import coo_array
from pgvecto_rs.psycopg import register_vector
from pgvecto_rs.types import Hnsw, IndexOption, SparseVector
URL = "postgresql://{username}:{password}@{host}:{port}/{db_name}".format(
port=os.getenv("DB_PORT", "5432"),
host=os.getenv("DB_HOST", "localhost"),
username=os.getenv("DB_USER", "postgres"),
password=os.getenv("DB_PASS", "mysecretpassword"),
db_name=os.getenv("DB_NAME", "postgres"),
)
# =================================
# Dense Vector Example
# =================================
# Connect to the DB and init things
with psycopg.connect(URL) as conn:
conn.execute("CREATE EXTENSION IF NOT EXISTS vectors;")
register_vector(conn)
conn.execute("DROP TABLE IF EXISTS documents;")
conn.execute(
"CREATE TABLE documents (id SERIAL PRIMARY KEY, text TEXT NOT NULL, embedding vector(3) NOT NULL);",
)
conn.commit()
try:
# Insert 3 rows into the table
conn.execute(
"INSERT INTO documents (text, embedding) VALUES (%s, %s);",
("hello world", [1, 2, 3]),
)
conn.execute(
"INSERT INTO documents (text, embedding) VALUES (%s, %s);",
("hello postgres", [1.0, 2.0, 4.0]),
)
conn.execute(
"INSERT INTO documents (text, embedding) VALUES (%s, %s);",
("hello pgvecto.rs", np.array([1, 3, 4])),
)
# Create index for the vectors
conn.execute(
"CREATE INDEX embedding_idx ON documents USING \
vectors (embedding vector_l2_ops) WITH (options=$${}$$);".format(
IndexOption(index=Hnsw(), threads=1).dumps()
),
)
conn.commit()
# Select the row "hello pgvecto.rs"
cur = conn.execute(
"SELECT * FROM documents WHERE text = %s;",
("hello pgvecto.rs",),
)
target = cur.fetchone()[2]
# Select all the rows and sort them
# by the l2_distance to "hello pgvecto.rs"
cur = conn.execute(
"SELECT text, embedding, embedding <-> %s AS distance FROM documents ORDER BY distance;",
(target,),
)
for text, emb, dis in cur.fetchall():
print((text, emb.to_numpy(), dis))
# The output will be:
# ```
# ('hello pgvecto.rs', array([1., 3., 4.], dtype=float32), 0.0)
# ('hello postgres', array([1., 2., 4.], dtype=float32), 1.0)
# ('hello world', array([1., 2., 3.], dtype=float32), 2.0)
# ```
finally:
# Drop the table
conn.execute("DROP TABLE IF EXISTS documents;")
conn.commit()
# =================================
# Sparse Vector Example
# =================================
# Connect to the DB and init things
with psycopg.connect(URL) as conn:
conn.execute("CREATE EXTENSION IF NOT EXISTS vectors;")
register_vector(conn)
conn.execute("DROP TABLE IF EXISTS documents;")
conn.execute(
"CREATE TABLE documents (id SERIAL PRIMARY KEY, text TEXT NOT NULL, embedding svector(60) NOT NULL);",
)
conn.commit()
try:
# Insert 3 rows into the table
conn.execute(
"INSERT INTO documents (text, embedding) VALUES (%s, %s);",
("hello world", SparseVector({0: 2, 1: 4, 2: 6}, 60)),
)
conn.execute(
"INSERT INTO documents (text, embedding) VALUES (%s, %s);",
(
"hello postgres",
SparseVector(
coo_array(
(np.array([2.0, 3.0]), np.array([[1, 2]])),
shape=(60,),
)
),
),
)
conn.execute(
"INSERT INTO documents (text, embedding) VALUES (%s, %s);",
("hello pgvecto.rs", SparseVector.from_parts(60, [0, 2], [1.0, 3.0])),
)
# Create index for the vectors
conn.execute(
"CREATE INDEX embedding_idx ON documents USING \
vectors (embedding svector_l2_ops) WITH (options=$${}$$);".format(
IndexOption(index=Hnsw(), threads=1).dumps()
),
)
conn.commit()
# Select the row "hello pgvecto.rs"
cur = conn.execute(
"SELECT * FROM documents WHERE text = %s;",
("hello pgvecto.rs",),
)
target = cur.fetchone()[2]
# Select all the rows and sort them
# by the l2_distance to "hello pgvecto.rs"
cur = conn.execute(
"SELECT text, embedding, embedding <-> %s AS distance FROM documents ORDER BY distance;",
(target,),
)
for row in cur.fetchall():
print(row)
# The output will be:
# ```
# ('hello pgvecto.rs', SparseVector({0: 1.0, 2: 3.0}, 60), 0.0)
# ('hello postgres', SparseVector({1: 2.0, 2: 3.0}, 60), 5.0)
# ('hello world', SparseVector({0: 2.0, 1: 4.0, 2: 6.0}, 60), 26.0)
# ```
finally:
# Drop the table
conn.execute("DROP TABLE IF EXISTS documents;")
conn.commit()