Skip to main content

Python binding for pgvecto.rs

Project description

PGVecto.rs support for Python

discord invitation link trackgit-views trackgit-views trackgit-views

Usage

Install from PyPI:

pip install pgvecto_rs

See the usage of SDK

Or use it as an extension of postgres clients:

Requirements

To initialize a pgvecto.rs instance, you can run our official image by Quick start:

You can get the latest tags from the Release page. For example, it might be:

docker run \
  --name pgvecto-rs-demo \
  -e POSTGRES_PASSWORD=mysecretpassword \
  -p 5432:5432 \
  -d tensorchord/pgvecto-rs:pg16-v0.2.1

SDK

Our SDK is designed to use the pgvecto.rs out-of-box. You can exploit the power of pgvecto.rs to do similarity search or retrieve with filters, without writing any SQL code.

Install dependencies:

pip install "pgvecto_rs[sdk]"

A minimal example:

from pgvecto_rs.sdk import PGVectoRs, Record

# Create a client
client = PGVectoRs(
    db_url="postgresql+psycopg://postgres:mysecretpassword@localhost:5432/postgres",
    table_name="example",
    dimension=3,
)

try:
    # Add some records
    client.add_records(
        [
            Record.from_text("hello 1", [1, 2, 3]),
            Record.from_text("hello 2", [1, 2, 4]),
        ]
    )

    # Search with default operator (sqrt_euclid).
    # The results is sorted by distance
    for rec, dis in client.search([1, 2, 5]):
        print(rec.text)
        print(dis)
finally:
    # Clean up (i.e. drop the table)
    client.drop()

Output:

hello 2
1.0
hello 1
4.0

See examples/sdk_example.py and tests/test_sdk.py for more examples.

SQLAlchemy

Install dependencies:

pip install "pgvecto_rs[sqlalchemy]"

Then write your code. See examples/sqlalchemy_example.py and tests/test_sqlalchemy.py for example.

All the operators include:

  • squared_euclidean_distance
  • negative_dot_product_distance
  • negative_cosine_distance

psycopg3

Install dependencies:

pip install "pgvecto_rs[psycopg3]"

Then write your code. See examples/psycopg_example.py and tests/test_psycopg.py for example.

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 fix
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

eb2c25d (init: first commit)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pgvecto_rs-0.1.5.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

pgvecto_rs-0.1.5-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file pgvecto_rs-0.1.5.tar.gz.

File metadata

  • Download URL: pgvecto_rs-0.1.5.tar.gz
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.15.4 CPython/3.10.12 Linux/6.5.0-1021-azure

File hashes

Hashes for pgvecto_rs-0.1.5.tar.gz
Algorithm Hash digest
SHA256 dba50b052f4c45256834c2c4867794e5c1f79a97edfe715f0369d48819b7fa9f
MD5 9ff7ecccce3640e238506f4942e1419b
BLAKE2b-256 cfec6f3bb158eef11ec9dcc39a93e9717741a05af7b3b3b79accc9f8aaf5266e

See more details on using hashes here.

File details

Details for the file pgvecto_rs-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: pgvecto_rs-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 14.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.15.4 CPython/3.10.12 Linux/6.5.0-1021-azure

File hashes

Hashes for pgvecto_rs-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 44d56fc73b54f35eae9139a84130fbc60723a661b20e9a098ad82ad7634468d2
MD5 32cd6b8537055292107049b0dc010212
BLAKE2b-256 3cec703369dec42e686c38200d3ee0cf441b3e2e347a475173d7aca5ce5b088a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page