GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — pgvector-python/examples/loading/example.py at master · pgvector/pgvector-python Expand file tree/ Copy pathexample.pyMore file actions 46 lines (37 loc) · 1.4 KB / Copy pathexample.pyFile metadata and controls46 lines (37 loc) · 1.4 KB 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 import numpy as np from pgvector import Vector 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([Vector(embedding)]) # show progress if i % 10000 == 0: print('.', end='', flush=True) print('\nSuccess!') # 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') |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |