Skip to main content

pgvector implementation for Tortoise-ORM

Project description

Implementation of vector for Tortoise-ORM

This package adds the support of pgvector vectors to Tortoise-ORM as a new type of fields. That way you can filter/order by cosine similarity distances for scementic search using embeddings. Here's an example for openai's embeddings but this will work with any kind of embeddings. Usage:

from tortoise.models import Model, QuerySet
from tortoise_vector.field import VectorField
from tortoise_vector.expression import CosineSimilarity


OPENAI_VECTOR_SIZE = 1536


class MyModel(Model):
    # vectors have a fixed size, openai uses 1536 dimensions
    embedding = VectorField(vector_size=OPENAI_VECTOR_SIZE)



async def get_embedding_from_text(string: str) -> list[float]:
    ...


async def get_nearst_models(text: str) -> QuerySet[MyModel]:
    embedding = await get_embedding_from_text(text)
    return (
        MyModel
        .all()
        .annotate(
            distance=CosineSimilarity(
                "embedding",
                embedding,
                OPENAI_VECTOR_SIZE
            )
        )
        .order_by("distance")
    )

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

tortoise_vector-0.2.0.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tortoise_vector-0.2.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file tortoise_vector-0.2.0.tar.gz.

File metadata

  • Download URL: tortoise_vector-0.2.0.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.13.7 Linux/6.16.3-arch1-1

File hashes

Hashes for tortoise_vector-0.2.0.tar.gz
Algorithm Hash digest
SHA256 27692a5c8cd579809ac57f576c4400f53299403728e0d38ac62bc685c839c024
MD5 d4df48590dafda92676c3789c8ae976c
BLAKE2b-256 f16d223d85c2f854371927f2ca6d76bbf82ba32a59a449c96bdd95e18c8739de

See more details on using hashes here.

File details

Details for the file tortoise_vector-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: tortoise_vector-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.13.7 Linux/6.16.3-arch1-1

File hashes

Hashes for tortoise_vector-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 38e9b89e2357312653cd8348f92f21771716473b3922d35a13720130fb9f2fee
MD5 a822a391a4a9d755af6527d31daa4294
BLAKE2b-256 eec64093934eb0baca06c3fd7c171aecc3b2c8692ec7ca403728d49481aad8b2

See more details on using hashes here.

Supported by

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