Skip to main content

Adds pgvector support to TortoiseORM.

Project description

Tortoise Embeddings

Tortoise Embeddings adds pgvector support to TortoiseORM, enabling efficient vector storage and similarity search in PostgreSQL.

Features

  • Four Vector Field Types:
    • VectorField: Supports vector type.
    • HalfVectorField: Supports halfvec type.
    • BinaryVector: Supports bit type.
    • SparseVector: Supports sparsevec type.
  • Aerich Migration Support: Automatically includes vector-based types from your models into the migration scripts.
  • Similarity Operations:
    • Custom Functions: L2Distance, CosineDistance, InnerProduct, L1Distance, HammingDistance, JaccardDistance.
    • Custom Filters: Convenient filter suffixes - __l2, __cosine, __inner, __l1, __hamming, and __jaccard.
  • Works with numpy arrays and standard Python types.

Installation

pip install tortoise-embeddings

Make sure you have pgvector installed in your PostgreSQL database.

Usage

Define Your Models

from tortoise.models import Model
from tortoise.fields import IntField
from tortoise_embeddings import VectorField, HalfVectorField, BinaryVectorField, SparseVectorField

class Item(Model):
    id = IntField(primary_key=True)
    # Define a vector field with 3 dimensions
    embedding = VectorField(dimensions=3)
    # Support for other pgvector types
    half_embedding = HalfVectorField(dimensions=3)
    binary_embedding = BinaryVectorField(dimensions=4)
    sparse_embedding = SparseVectorField(dimensions=3)

Initialize with Custom Client

To enable binary codecs and migration support, use the provided AsyncpgDBClient:

from tortoise import Tortoise
import tortoise.backends.asyncpg
from tortoise_embeddings import VectorAsyncpgDBClient

# Override the client class for the asyncpg backend
tortoise.backends.asyncpg.client_class = VectorAsyncpgDBClient

async def init():
    await Tortoise.init(
        db_url='postgres://user:pass@host:port/db',
        modules={'models': ['your_models_module']}
    )
    await Tortoise.generate_schemas()

Similarity Search

Using Custom Filters (Recommended)

import numpy

target_vector = numpy.array([1.0, 0.0, 0.0])

# Find items where L2 distance is less than 0.5
items = await Item.filter(embedding__l2=(target_vector, 0.5))

# Find items where Cosine distance is less than 0.1
items = await Item.filter(embedding__cosine=(target_vector, 0.1))

Using Custom Functions

from tortoise_embeddings.similarity import L2Distance

target_vector = [1.0, 0.0, 0.0]

# Annotate results with distance and order by it
items = await Item.all().annotate(
    dist=L2Distance(Item._meta.fields_map['embedding'], target_vector, alias="dist")
).order_by("dist")

for item in items:
    print(f"Item {item.id} has distance {item.dist}")

Aerich Migrations

If you use aerich, simply use the AerichVectorPostgresDDL to ensure the vector extension is created:

# In your project configuration
from tortoise_embeddings import AerichVectorPostgresDDL
# This is usually handled automatically if you use the VectorAsyncpgDBClient

Development setup

Set the following environment variables:

Variable Description How to get?
PSQL_CONNECTION_STRING A connection string to a PostgreSQL database. postgres://user:password@host:port/database
GEMINI_API_KEY An API key to Google GenAI. (optional) Go to aistudio.google.com and create an API key.

Dependencies:

  • tortoise-orm>=0.25.0,<1.0.0
  • aerich>=0.9.0
  • asyncpg>=0.31.0
  • pgvector>=0.4.0
  • numpy>=2.4.0

Running Tests

Tests require a PostgreSQL database with the pgvector extension.

export PSQL_CONNECTION_STRING="postgres://user:password@host:port/database"
export GEMINI_API_KEY="your_api_key" # Optional for Gemini integration tests
pytest tests/

License

Copyright 2026 Kotz

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

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_embeddings-0.0.1.tar.gz (17.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_embeddings-0.0.1-py3-none-any.whl (19.6 kB view details)

Uploaded Python 3

File details

Details for the file tortoise_embeddings-0.0.1.tar.gz.

File metadata

  • Download URL: tortoise_embeddings-0.0.1.tar.gz
  • Upload date:
  • Size: 17.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tortoise_embeddings-0.0.1.tar.gz
Algorithm Hash digest
SHA256 dee1e036ba8fe4c365a67c530bf6f6a09faaa8541dc74c15e099d2e5d8b8ab8d
MD5 9d740631b858e8239e8cc1203e3ddd32
BLAKE2b-256 13a921e0c28a1d983c42033010b14b5ee86b2948232528ee18a8cb95c4e4e1c7

See more details on using hashes here.

Provenance

The following attestation bundles were made for tortoise_embeddings-0.0.1.tar.gz:

Publisher: release.yml on Kaoticz/tortoise-embeddings

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tortoise_embeddings-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for tortoise_embeddings-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1e01ad5cf9f9800cfc337647af0bb8bb3a9a4edceee635cd11bc61e7bef95cf4
MD5 3279e3cbeed54b2a8b573b9a61e39acf
BLAKE2b-256 a771f0413745feb57461438dac173a0b0620fdd79ae7f9e318d4b34a3ae9e605

See more details on using hashes here.

Provenance

The following attestation bundles were made for tortoise_embeddings-0.0.1-py3-none-any.whl:

Publisher: release.yml on Kaoticz/tortoise-embeddings

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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