Skip to content

Commit 34db96b

Browse files
committed
Added pool example and test for asyncpg - closes pgvector#18
1 parent ccbb1a9 commit 34db96b

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,15 @@ from pgvector.asyncpg import register_vector
248248
await 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+
251260
Insert a vector
252261

253262
```python

tests/test_asyncpg.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)