赞
踩
Milvus于2019年创建,其独特目标是存储、索引和管理由深度神经网络和其他机器学习(ML)模型生成的大规模嵌入向量。Milvus能够在万亿级别上索引向量。与现有的主要处理遵循预定义模式的结构化数据的关系数据库不同,Milvus从底层开始设计,以处理从非结构化数据转换而来的嵌入向量。
官方文档:https://milvus.io/docs
1.进入cmd命令行,安装wsl2
wsl --install or wsl --install -d Ubuntu
2.进入Docker Desktop官网,下载对应系统的安装包,并安装
https://www.docker.com/products/docker-desktop/
1.下载Milvus yaml配置文件
https://github.com/milvus-io/milvus/releases/download/v2.3.0/milvus-standalone-docker-compose.yml
2.将下载的文件重命名为 docker-compose.yml
3.选取硬盘路径,在该目录下创建五个文件夹分命名为:conf,db,logs,pic,volumes,wal
我的文件目录
milvus/
│
├───conf/
├───db/
├───logs/
├───pic/
├───volumes/
├───wal/
│
└───docker-compose.yml
4. Shift+右键打开该目录的Powershell,执行
docker compose up -d
5. 打开Docker Desktop即可管理Milvus环境
如下图所示
pip install pymilvus
from pymilvus import (
connections,
utility,
FieldSchema, CollectionSchema, DataType,
Collection,
)
#连接milvus,default是数据库名,host支持远程连接,port端口默认19530
connections.connect("default", host="localhost", port="19530")
#定义表格式
fields = [
FieldSchema(name="name", dtype=DataType.VARCHAR, is_primary=True, auto_id=False, max_length=30),
FieldSchema(name="sequence", dtype=DataType.VARCHAR, max_length=35220),
FieldSchema(name="embeddings", dtype=DataType.FLOAT_VECTOR, dim=dim)
]
#创建表
schema = CollectionSchema(fields, "create vector database")
db = Collection('db_name', schema, consistency_level="Strong")
Tips
1.name,seqs,embeddings均为List
2.embeddings中的向量只支持list,不支持np.array和torch.tensor
3.插入数据不支持一次插入过多,因此建议酌情分块插入
#插入数据
entities = [
name,
seqs,
embeddings
]
db.insert(entities)
db.flush()
我自己写的overview.py
能显示已建立的数据表以及该表的数据量
from pymilvus import list_collections
from pymilvus import (
connections,
Collection
)
def main():
connections.connect("default", host="localhost", port="19530")
collections = list_collections()
if len(collections) == 0:
print('Milvus is empty.')
else:
for collection_name in collections:
print(collection_name, end=':')
db = Collection(collection_name)
print(db.num_entities)
if __name__ == '__main__':
main()
from pymilvus import (
connections,
utility
)
utility.drop_collection('dbname')
Milvus在查询前需要建立索引,支持IP/L2/COSINE,查询的similarity需要跟索引一致
假设db是一个milvus向量数据库
db.load()前需要保证db有已建立的索引
删除索引前需要先db.release()释放该数据库
索引类型选择可以参考 https://blog.csdn.net/weixin_44839084/article/details/103471083
index = {
"index_type": "IVF_FLAT",
"metric_type": "L2",
"params": {"nlist": 128},
}
db.create_index("embeddings", index)
db.load()
db.release()
db.drop_index()
limit设置需要查几条
hit.distance是当前metric下的score
支持多探针查询
search_params = {
"metric_type": 'L2',
"params": {"nprobe": 10},
}
result = db.search([pooler_token], "embeddings", search_params, limit=300, output_fields=["name"])
name = []
valus = []
for hits in result:
for hit in hits:
name.append(hit.id)
valus.append(hit.distance)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。