pyvector support for vexdb
Project description
pyvector-vexdb
pyvector support for vexdb
Supports SQLAlchemy,Psycopg 2
Run:
pip install pyvector-vexdb
And follow the instructions for your database library:
SQLAlchemy
映射向量列
from vexdb.sqlalchemy import FloatVector
class Item(Base):
embedding = mapped_column(FloatVector(3))
插入向量
item = Item(embedding=[1, 2, 3])
session.add(item)
session.commit()
查询近似向量 使用欧几里得距离操作符 l2_distance(<->)
session.scalar(select(Item).order_by(Item.embedding.l2_distance([3, 1, 2])).limit(5))
还支持以下操作符
negative_inner_product(<#>)consine_distance(<=>add(+)sub(-)
查询向量的欧几里得距离
session.scalar(select(Item.embedding.l2_distance([3, 1, 2])))
查询近似向量(使用欧几里得距离)
session.scalar(select(Item).filter(Item.embedding.l2_distance([3, 1, 2]) < 5))
函数调用
session.scalar(session.query(floatvector_combine([1.0,2.0,3.0], [4,5,6])))
还支持以下函数(函数说明请参考使用手册)
floatvector_accumfloatvector_cmpfloatvector_gtfloatvector_gefloatvector_nefloatvector_eqfloatvector_lefloatvector_ltfloatvector_spherical_distancefloatvector_negative_inner_productfloatvector_l2_squared_distancefloatvector_avgfloatvector_subfloatvector_addfloatvector_normfloatvector_dimsl2_distanceinner_productcosine_distance
近似最近邻索引
index = Index(
'my_index',
Item.embedding,
postgresql_using='hnsw',
postgresql_with={'m': 16, 'ef_construction': 64},
postgresql_ops={'embedding': 'floatvector_l2_ops'}
)
# or
index = Index(
'my_index',
Item.embedding,
postgresql_using='ivfflat',
postgresql_with={'ivf_nlist': 100},
postgresql_ops={'embedding': 'floatvector_l2_ops'}
)
索引构建还支持以下操作符
floatvector_l2_ops计算向量的欧几里得距离floatvector_ip_ops计算向量的内积floatvector_consine_ops计算向量的余弦距离
Psycopg 2
注册向量类型到连接或者游标
from vexdb.psycopg2 import register_vector
register_vector(conn)
创建带有向量类型的表
cur.execute('CREATE TABLE items (id bigint PRIMARY KEY, embedding floatvector(3))')
插入向量字段
embedding = np.array([1, 2, 3])
cur.execute('INSERT INTO items (embedding) VALUES (%s)', (embedding,))
获取最近邻向量
cur.execute('SELECT * FROM items ORDER BY embedding <-> %s LIMIT 5', (embedding,))
cur.fetchall()
floatvector_combine
cur.execute('select flaotvector_combine(%s,%s)', (embedding1, embeeding2,))
cur.fetch()
创建向量索引
cur.execute('CREATE INDEX ON items USING hnsw (embedding floatvector_l2_ops)')
# or
cur.execute('CREATE INDEX ON items USING ivfflat (embedding floatvector_l2_ops) WITH (ivf_nlist = 100)')
索引构建还支持以下操作符
floatvector_l2_ops计算向量的欧几里得距离floatvector_ip_ops计算向量的内积floatvector_consine_ops计算向量的余弦距离
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 pyvector_vexdb-1.0.2.tar.gz.
File metadata
- Download URL: pyvector_vexdb-1.0.2.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7431661be40a232b59beacad0c66deb85f64a2b3ba91c5ae35f9ef821458bbd2
|
|
| MD5 |
5dfd6e232e7229c45b44539b4595ba4b
|
|
| BLAKE2b-256 |
1656719efd7e699246ae82c66516515df0b1cb942f3d88a30120fb0dd5f66b34
|
File details
Details for the file pyvector_vexdb-1.0.2-py3-none-any.whl.
File metadata
- Download URL: pyvector_vexdb-1.0.2-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
47ba88fa821cf6dd6f536c7fd6998599ee0dd5163582be595f0cc6316b128cfb
|
|
| MD5 |
b127989683e579e7fdf802e7ed24b219
|
|
| BLAKE2b-256 |
7b0cd856c66d3284ab94fac2ee006d0f07ebfd59198f12dbe427a99589b706c5
|