Integrate MariaDB's VECTOR type with SQL Alchemy and SQL Model
Project description
MariaDB Vector
Integrate MariaDB's VECTOR type with SQL Alchemy and SQL Model
Installation
pip install mariadb-vector
Usage
Add a vector column
from sqlmodel import SQLModel, Field, Column
from mariadb_vector import Vector
class Item(SQLModel, table=True):
embedding: list[float] = Field(sa_column=Column(Vector(3)))
Insert a vector
item = Item(embedding=[0.1, 0.2, 0.3])
session.add(item)
session.commit()
Vector Search
The following functions are available for calculating vector distances:
vec_from_seq(array: Sequence)
- Converts a Python list or NumPy array into a database-compatible vector function.
vec_distance(v1, v2)
- Calculates a general distance between two vectors.
vec_distance_euclidean(v1, v2)
- Calculates the Euclidean distance between two vectors.
vec_distance_cosine(v1, v2)
- Calculates the cosine distance between two vectors.
# Assume `engine` is already created and connected to your database.
with Session(engine) as session:
target_vector = [0.1, 0.2, 0.3]
query = (
select(Item.id)
.order_by(vec_distance_euclidean(Item.embedding, target_vector))
.limit(2)
)
result = session.exec(query).all()
print(result)
Contributing
- This library is inspired by the pgvector-python and is built to bring similar functionality to MariaDB.
- Any contributions, bug reports, or improvements are welcome!
- Feel free to open issues or submit pull requests.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mariadb_vector-0.3.0.tar.gz.
File metadata
- Download URL: mariadb_vector-0.3.0.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.23.0 CPython/3.13.2 Linux/6.8.0-1021-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61ac4da85f51d41674053c670cf884b378ce12c5f7ad88da014ca62fc70a65ab
|
|
| MD5 |
91de827bb8b7967865596d4a72d4eec5
|
|
| BLAKE2b-256 |
6daa466a5cd7c884df6e43d6f9722544fe16e42f00aafaea25e54cca07c2b6cf
|
File details
Details for the file mariadb_vector-0.3.0-py3-none-any.whl.
File metadata
- Download URL: mariadb_vector-0.3.0-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.23.0 CPython/3.13.2 Linux/6.8.0-1021-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b278f07210dbbdb048cb4eafe50e7e06d8e65586a6289679e75d5fa3559af0b5
|
|
| MD5 |
c6c53fda70da27c9cc4e67158c4ff21d
|
|
| BLAKE2b-256 |
5cd698b4067d7e3608780b5cbd558351b827f06095de8d73637b29a07dd0f131
|