Skip to content

Commit 2821faf

Browse files
rm perf.sh & refactor examples.
Signed-off-by: my-vegetable-has-exploded <[email protected]>
1 parent 8c593e0 commit 2821faf

File tree

4 files changed

+109
-122
lines changed

4 files changed

+109
-122
lines changed

bindings/python/examples/perf.sh

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import os
2+
3+
import numpy as np
4+
import psycopg
5+
6+
from pgvecto_rs.psycopg import register_vector
7+
8+
URL = "postgresql://{username}:{password}@{host}:{port}/{db_name}".format(
9+
port=os.getenv("DB_PORT", "5432"),
10+
host=os.getenv("DB_HOST", "localhost"),
11+
username=os.getenv("DB_USER", "postgres"),
12+
password=os.getenv("DB_PASS", "mysecretpassword"),
13+
db_name=os.getenv("DB_NAME", "postgres"),
14+
)
15+
16+
# Connect to the DB and init things
17+
with psycopg.connect(URL) as conn:
18+
conn.execute("CREATE EXTENSION IF NOT EXISTS vectors;")
19+
register_vector(conn)
20+
conn.execute("DROP TABLE IF EXISTS documents;")
21+
conn.execute(
22+
"CREATE TABLE documents (id SERIAL PRIMARY KEY, embedding vector(3) NOT NULL);",
23+
)
24+
conn.commit()
25+
try:
26+
embeddings = [
27+
np.array([1, 2, 3]),
28+
np.array([1.0, 2.0, 4.0]),
29+
np.array([1, 3, 4]),
30+
]
31+
32+
with conn.cursor() as cursor, cursor.copy(
33+
"COPY documents (embedding) FROM STDIN (FORMAT BINARY)"
34+
) as copy:
35+
# write row by row
36+
for e in embeddings:
37+
copy.write_row([e])
38+
copy.write_row([[1, 3, 5]])
39+
conn.commit()
40+
41+
# Select the rows using binary format
42+
cur = conn.execute(
43+
"SELECT * FROM documents;",
44+
binary=True,
45+
)
46+
for row in cur.fetchall():
47+
print(row[0], ": ", row[1])
48+
49+
# output will be:
50+
# 1 : [1.0, 2.0, 3.0]
51+
# 2 : [1.0, 2.0, 4.0]
52+
# 3 : [1.0, 3.0, 4.0]
53+
# 4 : [1.0, 3.0, 5.0]
54+
finally:
55+
# Drop the table
56+
conn.execute("DROP TABLE IF EXISTS documents;")
57+
conn.commit()

bindings/python/examples/psycopg_copy_example.py

Lines changed: 0 additions & 117 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import os
2+
3+
import numpy as np
4+
import psycopg
5+
6+
from pgvecto_rs.psycopg import register_vector
7+
from pgvecto_rs.types import SparseVector
8+
9+
URL = "postgresql://{username}:{password}@{host}:{port}/{db_name}".format(
10+
port=os.getenv("DB_PORT", "5432"),
11+
host=os.getenv("DB_HOST", "localhost"),
12+
username=os.getenv("DB_USER", "postgres"),
13+
password=os.getenv("DB_PASS", "mysecretpassword"),
14+
db_name=os.getenv("DB_NAME", "postgres"),
15+
)
16+
17+
18+
# Connect to the DB and init things
19+
with psycopg.connect(URL) as conn:
20+
conn.execute("CREATE EXTENSION IF NOT EXISTS vectors;")
21+
register_vector(conn)
22+
conn.execute("DROP TABLE IF EXISTS documents;")
23+
conn.execute(
24+
"CREATE TABLE documents (id SERIAL PRIMARY KEY, embedding svector NOT NULL);",
25+
)
26+
conn.commit()
27+
try:
28+
with conn.cursor() as cursor, cursor.copy(
29+
"COPY documents (embedding) FROM STDIN (FORMAT BINARY)"
30+
) as copy:
31+
copy.write_row([SparseVector(3, [0, 2], [1.0, 3.0])])
32+
copy.write_row([SparseVector(3, np.array([0, 1, 2]), [1.0, 2.0, 3.0])])
33+
copy.write_row([SparseVector(3, np.array([1, 2]), np.array([2.0, 3.0]))])
34+
conn.pgconn.flush()
35+
conn.commit()
36+
37+
# Select the rows using binary format
38+
cur = conn.execute(
39+
"SELECT * FROM documents;",
40+
binary=True,
41+
)
42+
for row in cur.fetchall():
43+
print(row[0], ": ", row[1])
44+
45+
# output will be:
46+
# 1 : [1.0, 0.0, 3.0]
47+
# 2 : [1.0, 2.0, 3.0]
48+
# 3 : [0.0, 2.0, 3.0]
49+
finally:
50+
# Drop the table
51+
conn.execute("DROP TABLE IF EXISTS documents;")
52+
conn.commit()

0 commit comments

Comments
 (0)