asyncpg-vector 0.1.0__tar.gz → 0.1.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 Levente Hunyadi
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2025-2026 Levente Hunyadi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -1,132 +1,137 @@
1
- Metadata-Version: 2.4
2
- Name: asyncpg_vector
3
- Version: 0.1.0
4
- Summary: PostgreSQL vector support for asyncpg
5
- Author-email: Levente Hunyadi <hunyadi@gmail.com>
6
- Maintainer-email: Levente Hunyadi <hunyadi@gmail.com>
7
- License-Expression: MIT
8
- Project-URL: Homepage, https://github.com/hunyadi/asyncpg_vector
9
- Project-URL: Source, https://github.com/hunyadi/asyncpg_vector
10
- Keywords: asyncpg,postgresql-extension,vector,vector-search
11
- Classifier: Development Status :: 5 - Production/Stable
12
- Classifier: Framework :: AsyncIO
13
- Classifier: Intended Audience :: Developers
14
- Classifier: Operating System :: OS Independent
15
- Classifier: Programming Language :: Python :: 3
16
- Classifier: Programming Language :: Python :: 3.10
17
- Classifier: Programming Language :: Python :: 3.11
18
- Classifier: Programming Language :: Python :: 3.12
19
- Classifier: Programming Language :: Python :: 3.13
20
- Classifier: Programming Language :: Python :: 3.14
21
- Classifier: Programming Language :: Python :: 3 :: Only
22
- Classifier: Topic :: Database :: Front-Ends
23
- Classifier: Topic :: Software Development :: Libraries
24
- Classifier: Typing :: Typed
25
- Requires-Python: >=3.10
26
- Description-Content-Type: text/markdown
27
- License-File: LICENSE
28
- Requires-Dist: asyncpg>=0.30
29
- Requires-Dist: typing-extensions>=4.15; python_version < "3.11"
30
- Provides-Extra: dev
31
- Requires-Dist: asyncpg-stubs>=0.30; extra == "dev"
32
- Requires-Dist: build>=1.3; extra == "dev"
33
- Requires-Dist: mypy>=1.18; extra == "dev"
34
- Requires-Dist: ruff>=0.14; extra == "dev"
35
- Dynamic: license-file
36
-
37
- # PostgreSQL vector support for asyncpg
38
-
39
- Adds PostgreSQL [vector](https://github.com/pgvector/pgvector) support for Python.
40
-
41
- Registers data types `vector` and `halfvec` from the PostgreSQL extension `vector` to the asynchronous PostgreSQL client `asyncpg`, and marshals vector data to and from PostgreSQL database tables.
42
-
43
- Internally, the data is packed into a Python `bytes` object, with single-precision float vectors stored on 4 bytes per item (for class `Vector`) and half-precision float vectors stored on 2 bytes per item (for class `HalfVector`). Data is (un)packed using `struct` from the standard library as necessary.
44
-
45
- This module provides functionality similar to [pgvector-python](https://github.com/pgvector/pgvector-python) but imports minimum dependencies (e.g. no dependency on `numpy`).
46
-
47
- ## Setup
48
-
49
- #### Install the package
50
-
51
- ```sh
52
- pip install asyncpg_vector
53
- ```
54
-
55
- #### Initialize
56
-
57
- Register vector types with your database connection or connection pool:
58
-
59
- **Connection**:
60
-
61
- ```python
62
- from asyncpg_vector import register_vector
63
-
64
- async def main() -> None:
65
- ...
66
-
67
- await conn.execute("CREATE EXTENSION IF NOT EXISTS vector")
68
- await register_vector(conn)
69
- ```
70
-
71
- **Pool**:
72
-
73
- ```python
74
- from asyncpg_vector import register_vector
75
-
76
- async def init_connection(conn: asyncpg.Connection) -> None:
77
- await conn.execute("CREATE EXTENSION IF NOT EXISTS vector")
78
- await register_vector(conn)
79
-
80
- async def main() -> None:
81
- ...
82
-
83
- pool = await asyncpg.create_pool(..., init=init_connection)
84
- ```
85
-
86
- #### Perform similarity search
87
-
88
- First, create a table and an index:
89
-
90
- ```python
91
- async def create(conn: asyncpg.Connection) -> None:
92
- await conn.execute("""
93
- CREATE TABLE IF NOT EXISTS items
94
- (
95
- id bigint NOT NULL GENERATED ALWAYS AS IDENTITY,
96
- content text NOT NULL,
97
- embedding halfvec(1536) NOT NULL,
98
- CONSTRAINT pk_items PRIMARY KEY (id)
99
- );
100
-
101
- CREATE INDEX IF NOT EXISTS embedding_index ON items
102
- USING hnsw (embedding halfvec_cosine_ops);
103
- """)
104
- ```
105
-
106
- Next, find documents in a knowledge base that match a search phrase using vector similarity with approximate nearest neighbor semantics:
107
-
108
- ```python
109
- from asyncpg_vector import HalfVector
110
-
111
- async def search(conn: asyncpg.Connection, phrase: str) -> list[str]:
112
- ...
113
-
114
- embedding_response = await ai_client.embeddings.create(
115
- input=phrase,
116
- model="text-embedding-3-small",
117
- encoding_format="base64"
118
- )
119
- embedding = HalfVector.from_float_base64(embedding_response.data[0].embedding)
120
- query = """
121
- SELECT
122
- id,
123
- content,
124
- embedding <=> $1 AS distance
125
- FROM items
126
- ORDER BY distance
127
- LIMIT 5
128
- """
129
- rows = await conn.fetch(query, embedding)
130
-
131
- ...
132
- ```
1
+ Metadata-Version: 2.4
2
+ Name: asyncpg_vector
3
+ Version: 0.1.2
4
+ Summary: PostgreSQL vector support for asyncpg
5
+ Author-email: Levente Hunyadi <hunyadi@gmail.com>
6
+ Maintainer-email: Levente Hunyadi <hunyadi@gmail.com>
7
+ License-Expression: MIT
8
+ Project-URL: Homepage, https://github.com/hunyadi/asyncpg_vector
9
+ Project-URL: Source, https://github.com/hunyadi/asyncpg_vector
10
+ Keywords: asyncpg,postgresql-extension,vector,vector-search
11
+ Classifier: Development Status :: 5 - Production/Stable
12
+ Classifier: Framework :: AsyncIO
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Programming Language :: Python :: 3.13
20
+ Classifier: Programming Language :: Python :: 3.14
21
+ Classifier: Programming Language :: Python :: 3.15
22
+ Classifier: Programming Language :: Python :: 3 :: Only
23
+ Classifier: Topic :: Database :: Front-Ends
24
+ Classifier: Topic :: Software Development :: Libraries
25
+ Classifier: Typing :: Typed
26
+ Requires-Python: >=3.10
27
+ Description-Content-Type: text/markdown
28
+ License-File: LICENSE
29
+ Requires-Dist: asyncpg>=0.32
30
+ Requires-Dist: typing-extensions>=4.16; python_version < "3.11"
31
+ Provides-Extra: dev
32
+ Requires-Dist: asyncpg-stubs>=0.32; extra == "dev"
33
+ Requires-Dist: build>=1.6; extra == "dev"
34
+ Requires-Dist: mypy>=2.4; extra == "dev"
35
+ Requires-Dist: ruff>=0.17; extra == "dev"
36
+ Dynamic: license-file
37
+
38
+ # PostgreSQL vector support for asyncpg
39
+
40
+ Adds PostgreSQL [vector](https://github.com/pgvector/pgvector) support for Python.
41
+
42
+ Registers data types `vector` and `halfvec` from the PostgreSQL extension `vector` to the asynchronous PostgreSQL client `asyncpg`, and marshals vector data to and from PostgreSQL database tables.
43
+
44
+ Internally, the data is packed into a Python `bytes` object, with single-precision float vectors stored on 4 bytes per item (for class `Vector`) and half-precision float vectors stored on 2 bytes per item (for class `HalfVector`). Data is (un)packed using `struct` from the standard library as necessary.
45
+
46
+ This module provides functionality similar to [pgvector-python](https://github.com/pgvector/pgvector-python) but imports minimum dependencies (e.g. no dependency on `numpy`).
47
+
48
+ ## Setup
49
+
50
+ #### Install the package
51
+
52
+ ```sh
53
+ pip install asyncpg_vector
54
+ ```
55
+
56
+ #### Initialize
57
+
58
+ Register vector types with your database connection or connection pool:
59
+
60
+ **Connection**:
61
+
62
+ ```python
63
+ from asyncpg_vector import register_vector
64
+
65
+
66
+ async def main() -> None:
67
+ ...
68
+
69
+ await conn.execute("CREATE EXTENSION IF NOT EXISTS vector")
70
+ await register_vector(conn)
71
+ ```
72
+
73
+ **Pool**:
74
+
75
+ ```python
76
+ from asyncpg_vector import register_vector
77
+
78
+
79
+ async def init_connection(conn: asyncpg.Connection) -> None:
80
+ await conn.execute("CREATE EXTENSION IF NOT EXISTS vector")
81
+ await register_vector(conn)
82
+
83
+
84
+ async def main() -> None:
85
+ ...
86
+
87
+ pool = await asyncpg.create_pool(..., init=init_connection)
88
+ ```
89
+
90
+ #### Perform similarity search
91
+
92
+ First, create a table and an index:
93
+
94
+ ```python
95
+ async def create(conn: asyncpg.Connection) -> None:
96
+ await conn.execute("""
97
+ CREATE TABLE IF NOT EXISTS items
98
+ (
99
+ id bigint NOT NULL GENERATED ALWAYS AS IDENTITY,
100
+ content text NOT NULL,
101
+ embedding halfvec(1536) NOT NULL,
102
+ CONSTRAINT pk_items PRIMARY KEY (id)
103
+ );
104
+
105
+ CREATE INDEX IF NOT EXISTS embedding_index ON items
106
+ USING hnsw (embedding halfvec_cosine_ops);
107
+ """)
108
+ ```
109
+
110
+ Next, find documents in a knowledge base that match a search phrase using vector similarity with approximate nearest neighbor semantics:
111
+
112
+ ```python
113
+ from asyncpg_vector import HalfVector
114
+
115
+
116
+ async def search(conn: asyncpg.Connection, phrase: str) -> list[str]:
117
+ ...
118
+
119
+ embedding_response = await ai_client.embeddings.create(
120
+ input=phrase,
121
+ model="text-embedding-3-small", # aligned with `halfvec` size
122
+ encoding_format="base64", # transmits data efficiently
123
+ )
124
+ embedding = HalfVector.from_float_base64(embedding_response.data[0].embedding)
125
+ query = """
126
+ SELECT
127
+ id,
128
+ content,
129
+ embedding <=> $1 AS distance
130
+ FROM items
131
+ ORDER BY distance
132
+ LIMIT 5
133
+ """
134
+ rows = await conn.fetch(query, embedding)
135
+
136
+ ...
137
+ ```
@@ -1,96 +1,100 @@
1
- # PostgreSQL vector support for asyncpg
2
-
3
- Adds PostgreSQL [vector](https://github.com/pgvector/pgvector) support for Python.
4
-
5
- Registers data types `vector` and `halfvec` from the PostgreSQL extension `vector` to the asynchronous PostgreSQL client `asyncpg`, and marshals vector data to and from PostgreSQL database tables.
6
-
7
- Internally, the data is packed into a Python `bytes` object, with single-precision float vectors stored on 4 bytes per item (for class `Vector`) and half-precision float vectors stored on 2 bytes per item (for class `HalfVector`). Data is (un)packed using `struct` from the standard library as necessary.
8
-
9
- This module provides functionality similar to [pgvector-python](https://github.com/pgvector/pgvector-python) but imports minimum dependencies (e.g. no dependency on `numpy`).
10
-
11
- ## Setup
12
-
13
- #### Install the package
14
-
15
- ```sh
16
- pip install asyncpg_vector
17
- ```
18
-
19
- #### Initialize
20
-
21
- Register vector types with your database connection or connection pool:
22
-
23
- **Connection**:
24
-
25
- ```python
26
- from asyncpg_vector import register_vector
27
-
28
- async def main() -> None:
29
- ...
30
-
31
- await conn.execute("CREATE EXTENSION IF NOT EXISTS vector")
32
- await register_vector(conn)
33
- ```
34
-
35
- **Pool**:
36
-
37
- ```python
38
- from asyncpg_vector import register_vector
39
-
40
- async def init_connection(conn: asyncpg.Connection) -> None:
41
- await conn.execute("CREATE EXTENSION IF NOT EXISTS vector")
42
- await register_vector(conn)
43
-
44
- async def main() -> None:
45
- ...
46
-
47
- pool = await asyncpg.create_pool(..., init=init_connection)
48
- ```
49
-
50
- #### Perform similarity search
51
-
52
- First, create a table and an index:
53
-
54
- ```python
55
- async def create(conn: asyncpg.Connection) -> None:
56
- await conn.execute("""
57
- CREATE TABLE IF NOT EXISTS items
58
- (
59
- id bigint NOT NULL GENERATED ALWAYS AS IDENTITY,
60
- content text NOT NULL,
61
- embedding halfvec(1536) NOT NULL,
62
- CONSTRAINT pk_items PRIMARY KEY (id)
63
- );
64
-
65
- CREATE INDEX IF NOT EXISTS embedding_index ON items
66
- USING hnsw (embedding halfvec_cosine_ops);
67
- """)
68
- ```
69
-
70
- Next, find documents in a knowledge base that match a search phrase using vector similarity with approximate nearest neighbor semantics:
71
-
72
- ```python
73
- from asyncpg_vector import HalfVector
74
-
75
- async def search(conn: asyncpg.Connection, phrase: str) -> list[str]:
76
- ...
77
-
78
- embedding_response = await ai_client.embeddings.create(
79
- input=phrase,
80
- model="text-embedding-3-small",
81
- encoding_format="base64"
82
- )
83
- embedding = HalfVector.from_float_base64(embedding_response.data[0].embedding)
84
- query = """
85
- SELECT
86
- id,
87
- content,
88
- embedding <=> $1 AS distance
89
- FROM items
90
- ORDER BY distance
91
- LIMIT 5
92
- """
93
- rows = await conn.fetch(query, embedding)
94
-
95
- ...
96
- ```
1
+ # PostgreSQL vector support for asyncpg
2
+
3
+ Adds PostgreSQL [vector](https://github.com/pgvector/pgvector) support for Python.
4
+
5
+ Registers data types `vector` and `halfvec` from the PostgreSQL extension `vector` to the asynchronous PostgreSQL client `asyncpg`, and marshals vector data to and from PostgreSQL database tables.
6
+
7
+ Internally, the data is packed into a Python `bytes` object, with single-precision float vectors stored on 4 bytes per item (for class `Vector`) and half-precision float vectors stored on 2 bytes per item (for class `HalfVector`). Data is (un)packed using `struct` from the standard library as necessary.
8
+
9
+ This module provides functionality similar to [pgvector-python](https://github.com/pgvector/pgvector-python) but imports minimum dependencies (e.g. no dependency on `numpy`).
10
+
11
+ ## Setup
12
+
13
+ #### Install the package
14
+
15
+ ```sh
16
+ pip install asyncpg_vector
17
+ ```
18
+
19
+ #### Initialize
20
+
21
+ Register vector types with your database connection or connection pool:
22
+
23
+ **Connection**:
24
+
25
+ ```python
26
+ from asyncpg_vector import register_vector
27
+
28
+
29
+ async def main() -> None:
30
+ ...
31
+
32
+ await conn.execute("CREATE EXTENSION IF NOT EXISTS vector")
33
+ await register_vector(conn)
34
+ ```
35
+
36
+ **Pool**:
37
+
38
+ ```python
39
+ from asyncpg_vector import register_vector
40
+
41
+
42
+ async def init_connection(conn: asyncpg.Connection) -> None:
43
+ await conn.execute("CREATE EXTENSION IF NOT EXISTS vector")
44
+ await register_vector(conn)
45
+
46
+
47
+ async def main() -> None:
48
+ ...
49
+
50
+ pool = await asyncpg.create_pool(..., init=init_connection)
51
+ ```
52
+
53
+ #### Perform similarity search
54
+
55
+ First, create a table and an index:
56
+
57
+ ```python
58
+ async def create(conn: asyncpg.Connection) -> None:
59
+ await conn.execute("""
60
+ CREATE TABLE IF NOT EXISTS items
61
+ (
62
+ id bigint NOT NULL GENERATED ALWAYS AS IDENTITY,
63
+ content text NOT NULL,
64
+ embedding halfvec(1536) NOT NULL,
65
+ CONSTRAINT pk_items PRIMARY KEY (id)
66
+ );
67
+
68
+ CREATE INDEX IF NOT EXISTS embedding_index ON items
69
+ USING hnsw (embedding halfvec_cosine_ops);
70
+ """)
71
+ ```
72
+
73
+ Next, find documents in a knowledge base that match a search phrase using vector similarity with approximate nearest neighbor semantics:
74
+
75
+ ```python
76
+ from asyncpg_vector import HalfVector
77
+
78
+
79
+ async def search(conn: asyncpg.Connection, phrase: str) -> list[str]:
80
+ ...
81
+
82
+ embedding_response = await ai_client.embeddings.create(
83
+ input=phrase,
84
+ model="text-embedding-3-small", # aligned with `halfvec` size
85
+ encoding_format="base64", # transmits data efficiently
86
+ )
87
+ embedding = HalfVector.from_float_base64(embedding_response.data[0].embedding)
88
+ query = """
89
+ SELECT
90
+ id,
91
+ content,
92
+ embedding <=> $1 AS distance
93
+ FROM items
94
+ ORDER BY distance
95
+ LIMIT 5
96
+ """
97
+ rows = await conn.fetch(query, embedding)
98
+
99
+ ...
100
+ ```