Python bindings for pgvector.rs
Currently supports SQLAlchemy.
Usage
Install from PyPI:
pip install pgvecto_rs
See the usage examples:
SQLAlchemy
Install SQLAlchemy and psycopg3
pip install "psycopg[binary]" sqlalchemy
Then write your code. For example:
import numpy as np
from sqlalchemy import create_engine, select, insert, types
from sqlalchemy import Integer, String
from pgvector_rs.sqlalchemy import Vector
from sqlalchemy.orm import Session, DeclarativeBase, mapped_column, Mapped
URL = "postgresql+psycopg://<...>"
# Define the ORM model
class Base(DeclarativeBase):
pass
class Document(Base):
__tablename__ = "documents"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
text: Mapped[str] = mapped_column(String)
embedding: Mapped[np.ndarray] = mapped_column(Vector(3))
def __repr__(self) -> str:
return f"{self.text}: {self.embedding}"
# Connect to the DB and create the table
engine = create_engine(URL)
Document.metadata.create_all(engine)
with Session(engine) as session:
# Insert 3 rows into the table
t1 = insert(Document).values(text="hello world", embedding=[1, 2, 3])
t2 = insert(Document).values(text="hello postgres", embedding=[1, 2, 4])
t3 = insert(Document).values(text="hello pgvecto.rs", embedding=[1, 3, 4])
for t in [t1, t2, t3]:
session.execute(t)
session.commit()
# Select the row "hello pgvecto.rs"
stmt = select(Document).where(Document.text == "hello pgvecto.rs")
target = session.scalar(stmt)
# Select all the rows and sort them
# by the squared_euclidean_distance to "hello pgvecto.rs"
stmt = select(
Document.text,
Document.embedding.squared_euclidean_distance(target.embedding).label(
"distance"
),
).order_by("distance")
for doc in session.execute(stmt):
print(doc)
# Drop the table
Document.metadata.drop_all(engine)
The output will be:
('hello pgvecto.rs', 0.0)
('hello postgres', 1.0)
('hello world', 2.0)
All the operators include:
squared_euclidean_distancenegative_dot_product_distancenegative_cosine_distance
Development
This package is managed by PDM.
Set up things:
pdm venv create
pdm use # select the venv inside the project path
pdm sync
Run lint:
pdm run format
pdm run check
Run test in current environment:
pdm run test
Test
Tox is used to test the package locally.
Run test in all environment:
tox run
Metadata
Release files for pgvecto-rs 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pgvecto_rs-0.1.1.tar.gz | 8.8 kB | Without provenance | Details for pgvecto_rs-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pgvecto_rs-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for pgvecto_rs-0.1.1-py3-none-any.whl |
Total release size: 17.2 kB
Release files / pgvecto_rs-0.1.1.tar.gz
| Download URL | pgvecto_rs-0.1.1.tar.gz |
|---|---|
| Size | 8.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bcb4d66289cbda87e2a469101e56501b4f8fe08324a23fa5b4b86cf8ddc8cbd6
|
|
BLAKE2b-256 checksum How to use checksums |
b060a5e6ff43fd3aeab21467e4a460aa92aba16e09d0bc5f3ebd7ed9bd5a73dc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
pdm/2.9.3 CPython/3.10.12
|
Release files / pgvecto_rs-0.1.1-py3-none-any.whl
| Download URL | pgvecto_rs-0.1.1-py3-none-any.whl |
|---|---|
| Size | 8.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
73008eebd53b27683d754b00686a582c7cdad51b3275fd827a0865e820744558
|
|
BLAKE2b-256 checksum How to use checksums |
ad87161843c768e6a0e91cd9f64ebce88b8edb4f1b0f188e15af80da3c6f9e3f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
pdm/2.9.3 CPython/3.10.12
|
Security for pgvecto-rs 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.