# 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:
```python
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")
)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/Chr0nos/tortoise_vector",
"name": "tortoise-vector",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": null,
"author": "Sebastien Nicolet",
"author_email": "snicolet95@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/f1/6d/223d85c2f854371927f2ca6d76bbf82ba32a59a449c96bdd95e18c8739de/tortoise_vector-0.2.0.tar.gz",
"platform": null,
"description": "# Implementation of vector for Tortoise-ORM\nThis package adds the support of `pgvector` vectors to Tortoise-ORM as a new type of fields.\nThat way you can filter/order by cosine similarity distances for scementic search using embeddings.\nHere's an example for openai's embeddings but this will work with any kind of embeddings.\nUsage:\n\n```python\nfrom tortoise.models import Model, QuerySet\nfrom tortoise_vector.field import VectorField\nfrom tortoise_vector.expression import CosineSimilarity\n\n\nOPENAI_VECTOR_SIZE = 1536\n\n\nclass MyModel(Model):\n # vectors have a fixed size, openai uses 1536 dimensions\n embedding = VectorField(vector_size=OPENAI_VECTOR_SIZE)\n\n\n\nasync def get_embedding_from_text(string: str) -> list[float]:\n ...\n\n\nasync def get_nearst_models(text: str) -> QuerySet[MyModel]:\n embedding = await get_embedding_from_text(text)\n return (\n MyModel\n .all()\n .annotate(\n distance=CosineSimilarity(\n \"embedding\",\n embedding,\n OPENAI_VECTOR_SIZE\n )\n )\n .order_by(\"distance\")\n )\n```\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "pgvector implementation for Tortoise-ORM",
"version": "0.2.0",
"project_urls": {
"Documentation": "https://github.com/Chr0nos/tortoise_vector/blob/master/tortoise_vector/README.md",
"Homepage": "https://github.com/Chr0nos/tortoise_vector",
"Repository": "https://github.com/Chr0nos/tortoise_vector.git"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "eec64093934eb0baca06c3fd7c171aecc3b2c8692ec7ca403728d49481aad8b2",
"md5": "a822a391a4a9d755af6527d31daa4294",
"sha256": "38e9b89e2357312653cd8348f92f21771716473b3922d35a13720130fb9f2fee"
},
"downloads": -1,
"filename": "tortoise_vector-0.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "a822a391a4a9d755af6527d31daa4294",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 3940,
"upload_time": "2025-10-10T14:21:04",
"upload_time_iso_8601": "2025-10-10T14:21:04.387336Z",
"url": "https://files.pythonhosted.org/packages/ee/c6/4093934eb0baca06c3fd7c171aecc3b2c8692ec7ca403728d49481aad8b2/tortoise_vector-0.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "f16d223d85c2f854371927f2ca6d76bbf82ba32a59a449c96bdd95e18c8739de",
"md5": "d4df48590dafda92676c3789c8ae976c",
"sha256": "27692a5c8cd579809ac57f576c4400f53299403728e0d38ac62bc685c839c024"
},
"downloads": -1,
"filename": "tortoise_vector-0.2.0.tar.gz",
"has_sig": false,
"md5_digest": "d4df48590dafda92676c3789c8ae976c",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 2794,
"upload_time": "2025-10-10T14:21:05",
"upload_time_iso_8601": "2025-10-10T14:21:05.247222Z",
"url": "https://files.pythonhosted.org/packages/f1/6d/223d85c2f854371927f2ca6d76bbf82ba32a59a449c96bdd95e18c8739de/tortoise_vector-0.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-10-10 14:21:05",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Chr0nos",
"github_project": "tortoise_vector",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "tortoise-vector"
}