类功能
功能描述
提供向量数据库。对同一个数据库的collection,add和add_sparse以及add_dense_and_sparse需独立使用,混用会导致失败。
函数原型
from mx_rag.storage.vectorstore import MilvusDB MilvusDB(client, collection_name, search_mode, auto_id, index_type, metric_type, auto_flush)
参数说明
参数名 |
数据类型 |
可选/必选 |
说明 |
---|---|---|---|
client |
MilvusClient |
必选 |
MilvusClient实例,具体说明请参考MilvusClient。 说明:
MilvusClient由用户控制传入,请使用安全的连接方式。 |
collection_name |
str |
可选 |
集合名称,不能为空,最大长度为1024,默认为“rag_sdk”。 |
search_mode |
SearchMode |
可选 |
检索模式,当前支持三种模式,包括稠密检索(DENSE),稀疏检索(SPARSE)和混合检索(HYBRID)。 类型介绍可参见SearchMode。 |
auto_id |
bool |
可选 |
主键是否自增,默认为False。 |
index_type |
str |
可选 |
向量检索类型,当前只支持FLAT, IVF_FLAT, IVF_PQ, HNSW,默认为FLAT,该字段稠密检索和混合检索模式时,针对稠密向量有效。稀疏向量检索类型固定为SPARSE_INVERTED_INDEX,不支持配置。 |
metric_type |
str |
可选 |
向量距离计算方式,支持IP,L2,COSINE,默认为L2,该字段稠密检索和混合检索模式时,针对稠密向量有效。稀疏向量距离计算方式固定为IP,不支持配置。 |
auto_flush |
bool |
可选 |
数据变更时是否自动刷新内存数据,默认为True。 |
返回类型
数据类型 |
说明 |
---|---|
MilvusDB |
MilvusDB对象。 |
调用示例
from pymilvus import MilvusClient from mx_rag.storage.vectorstore import MilvusDB import numpy as np client = MilvusClient("https://x.x.x.x:port", user="xxx", password=getpass.getpass(), token="xxx") vector_store = MilvusDB.create(client=client, x_dim=1024) vecs = np.random.randn(3, 1024) vector_store.add([0, 1, 2], vecs) print(vector_store.get_all_ids()) vector_store.delete([1]) vector_store.get_all_ids() print(vector_store.search(vecs[1:2, :].tolist())) vector_store.update([0], vecs[:1]) vector_store.drop_collection()
父主题: MilvusDB类