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_vector.field import VectorField
from tortoise_vector.expressions import CosineSimilarity
from tortoise import Model
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(str: 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
Release history Release notifications | RSS feed
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.1.1.tar.gz
(2.6 kB
view hashes)
Built Distribution
Close
Hashes for tortoise_vector-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 84abd98d9d765dcd9aab0794fba80218d8df39950b8e1c5227b95b46b4eab828 |
|
MD5 | b6bcd11334099032f8133ba20c53e1f9 |
|
BLAKE2b-256 | 953b914bbb3917974f1130a330088cbcaf5a94bb388a9820b40f703fac086c14 |