-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
99 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -326,3 +326,4 @@ cython_debug/ | |
#.idea/ | ||
|
||
/arxiv_abstracts.zip | ||
*.parquet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import numpy as np | ||
from pgvector.psycopg import register_vector | ||
import psycopg | ||
|
||
# generate random data | ||
rows = 100000 | ||
dimensions = 128 | ||
embeddings = np.random.rand(rows, dimensions) | ||
categories = np.random.randint(100, size=rows).tolist() | ||
queries = np.random.rand(10, dimensions) | ||
|
||
DB_NAME= 'pgvector_citus' | ||
|
||
#Make the table | ||
conn = psycopg.connect("host=localhost user=postgres password='letmein'", autocommit=True) | ||
cursor = conn.cursor() | ||
|
||
cursor.execute("SELECT datname FROM pg_database;") | ||
|
||
list_database = cursor.fetchall() | ||
|
||
if ('pgvector_citus',) in list_database: | ||
cursor.execute(("DROP database "+ DB_NAME +" with (FORCE);")) | ||
cursor.execute("create database " + DB_NAME + ";"); | ||
else: | ||
cursor.execute("create database " + DB_NAME + ";"); | ||
|
||
#Now close the connection and switch DB | ||
conn.close() | ||
|
||
|
||
# enable extensions | ||
conn = psycopg.connect("host=localhost user=postgres password='letmein' dbname='pgvector_citus'", autocommit=True) | ||
conn.execute('CREATE EXTENSION IF NOT EXISTS vector') | ||
|
||
# GUC variables set on the session do not propagate to Citus workers | ||
# https://github.com/citusdata/citus/issues/462 | ||
# you can either: | ||
# 1. set them on the system, user, or database and reconnect | ||
# 2. set them for a transaction with SET LOCAL | ||
# S | ||
conn.close() | ||
|
||
# reconnect for updated GUC variables to take effect | ||
conn = psycopg.connect("host=localhost user=postgres password='letmein' dbname='pgvector_citus'", autocommit=True) | ||
register_vector(conn) | ||
|
||
print('Creating distributed table') | ||
conn.execute('DROP TABLE IF EXISTS items') | ||
conn.execute('CREATE TABLE items (id bigserial, embedding vector(%d), category_id bigint, PRIMARY KEY (id, category_id))' % dimensions) | ||
#conn.execute('SET citus.shard_count = 4') | ||
#conn.execute("SELECT create_distributed_table('items', 'category_id')") | ||
|
||
print('Loading data in parallel') | ||
with conn.cursor().copy('COPY items (embedding, category_id) FROM STDIN WITH (FORMAT BINARY)') as copy: | ||
copy.set_types(['vector', 'bigint']) | ||
|
||
for i in range(rows): | ||
copy.write_row([embeddings[i], categories[i]]) | ||
|
||
conn.close() | ||
|
||
# print('Creating index in parallel') | ||
# conn.execute('CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)') | ||
# | ||
# print('Running distributed queries') | ||
# for query in queries: | ||
# items = conn.execute('SELECT id FROM items ORDER BY embedding <-> %s LIMIT 10', (query,)).fetchall() | ||
# print([r[0] for r in items]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
numpy | ||
requests>=2.26.0 | ||
scikit_learn>=1.0.2 | ||
scipy | ||
sentence_transformers>=2.2.0 | ||
torch | ||
tqdm | ||
psycopg | ||
pyarrow | ||
pgvector |