File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed
Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -248,6 +248,15 @@ from pgvector.asyncpg import register_vector
248248await register_vector(conn)
249249```
250250
251+ or your pool
252+
253+ ``` python
254+ async def init (conn ):
255+ await register_vector(conn)
256+
257+ pool = await asyncpg.create_pool(... , init = register_vector)
258+ ```
259+
251260Insert a vector
252261
253262``` python
Original file line number Diff line number Diff line change @@ -30,3 +30,25 @@ async def test_works(self):
3030 assert text_res [0 ]['embedding' ] == '[1.5,2,3]'
3131
3232 await conn .close ()
33+
34+ @pytest .mark .asyncio
35+ async def test_pool (self ):
36+ async def init (conn ):
37+ await register_vector (conn )
38+
39+ pool = await asyncpg .create_pool (database = 'pgvector_python_test' , init = init )
40+
41+ async with pool .acquire () as conn :
42+ await conn .execute ('CREATE EXTENSION IF NOT EXISTS vector' )
43+ await conn .execute ('DROP TABLE IF EXISTS item' )
44+ await conn .execute ('CREATE TABLE item (id bigserial primary key, embedding vector(3))' )
45+
46+ embedding = np .array ([1.5 , 2 , 3 ])
47+ await conn .execute ("INSERT INTO item (embedding) VALUES ($1), (NULL)" , embedding )
48+
49+ res = await conn .fetch ("SELECT * FROM item ORDER BY id" )
50+ assert res [0 ]['id' ] == 1
51+ assert res [1 ]['id' ] == 2
52+ assert np .array_equal (res [0 ]['embedding' ], embedding )
53+ assert res [0 ]['embedding' ].dtype == np .float32
54+ assert res [1 ]['embedding' ] is None
You can’t perform that action at this time.
0 commit comments