[proxy]
github.com
—
← back
|
site home
|
direct (HTTPS) ↗
|
proxy home
|
◑ dark
◐ light
pgvector
/
pgvector-python
Public
Notifications
You must be signed in to change notification settings
Fork
89
Star
1.4k
Files
Expand file tree
master
/
example.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
45 lines (36 loc) · 1.37 KB
master
/
example.py
Top
File metadata and controls
Code
Blame
45 lines (36 loc) · 1.37 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
numpy
as
np
from
pgvector
.
psycopg
import
register_vector
import
psycopg
# generate random data
rows
=
1000000
dimensions
=
128
embeddings
=
np
.
random
.
rand
(
rows
,
dimensions
)
# enable extension
conn
=
psycopg
.
connect
(
dbname
=
'pgvector_example'
,
autocommit
=
True
)
conn
.
execute
(
'CREATE EXTENSION IF NOT EXISTS vector'
)
register_vector
(
conn
)
# create table
conn
.
execute
(
'DROP TABLE IF EXISTS items'
)
conn
.
execute
(
f'CREATE TABLE items (id bigserial, embedding vector(
{
dimensions
}
))'
)
# load data
print
(
f'Loading
{
len
(
embeddings
)
}
rows'
)
cur
=
conn
.
cursor
()
with
cur
.
copy
(
'COPY items (embedding) FROM STDIN WITH (FORMAT BINARY)'
)
as
copy
:
# use set_types for binary copy
# https://www.psycopg.org/psycopg3/docs/basic/copy.html#binary-copy
copy
.
set_types
([
'vector'
])
for
i
,
embedding
in
enumerate
(
embeddings
):
copy
.
write_row
([
embedding
])
# show progress
if
i
%
10000
==
0
:
print
(
'.'
,
end
=
''
,
flush
=
True
)
print
(
'
\n
Success!'
)
# create any indexes *after* loading initial data (skipping for this example)
create_index
=
False
if
create_index
:
print
(
'Creating index'
)
conn
.
execute
(
"SET maintenance_work_mem = '8GB'"
)
conn
.
execute
(
'SET max_parallel_maintenance_workers = 7'
)
conn
.
execute
(
'CREATE INDEX ON items USING hnsw (embedding vector_cosine_ops)'
)
# update planner statistics for good measure
conn
.
execute
(
'ANALYZE items'
)