pymilvus执行多向量搜索

pymilvus执行多向量搜索

从 Milvus 2.4 开始,引入了多向量支持和混合搜索框架,单个collection可以支持10个向量字段。不同的向量字段可以表示不同的方面、不同的embedding模型甚至表征同一实体的不同数据模态。该功能在综合搜索场景中特别有用,例如根据图片、语音、指纹等各种属性来识别向量库中最相似的人。

多向量搜索支持在多个向量字段上执行搜索请求,并使用重排名策略(例如RRF和加权评分)组合结果。

客户端pymilvus需要2.4版本。

准备数据

集合hello_milvus有2个向量字段embeddings1、embeddings2

创建多个 AnnSearchRequest 实例

每个AnnSearchRequest代表指定向量字段上的单个搜索请求。

以下示例创建两个AnnSearchRequest实例对两个向量字段执行单独的相似性搜索。

go 复制代码
query_vector1 = [[0.9425935745239258,0.7893211245536804,0.6682707071304321,0.6769697070121765,0.9508556127548218]]

search_param_1 = {
    "data": query_vector1, # Query vector
    "anns_field": "embeddings1", # Vector field name
    "param": {
        "metric_type": "L2", # This parameter value must be identical to the one used in the collection schema
        "params": {"nprobe": 10}
    },
    "limit": 2 # Number of search results to return in this AnnSearchRequest
}
request_1 = AnnSearchRequest(**search_param_1)

query_vector2 = [[0.7958434224128723,0.18576304614543915,0.650543212890625,0.3026141822338104,0.7158203125]]
search_param_2 = {
    "data": query_vector2, # Query vector
    "anns_field": "embeddings2", # Vector field name
    "param": {
        "metric_type": "L2", # This parameter value must be identical to the one used in the collection schema
        "params": {"nprobe": 10}
    },
    "limit": 2 # Number of search results to return in this AnnSearchRequest
}
request_2 = AnnSearchRequest(**search_param_2)

reqs = [request_1, request_2]

配置重排名策略

创建AnnSearchRequest实例后,配置重排名策略以组合结果并重新排名。目前有两个选项:WeightedRanker和RRFRanker。

go 复制代码
from pymilvus import WeightedRanker
rerank = WeightedRanker(0.8, 0.2)

执行混合搜索

使用hybrid_search()方法执行多向量搜索。

go 复制代码
from pymilvus import (
    connections,
    Collection,
    AnnSearchRequest,
    WeightedRanker
)

collection_name = "hello_milvus"
host = "192.168.230.71"
port = 19530
username = ""
password = ""


query_vector1 = [[0.9425935745239258,0.7893211245536804,0.6682707071304321,0.6769697070121765,0.9508556127548218]]

search_param_1 = {
    "data": query_vector1, # Query vector
    "anns_field": "embeddings1", # Vector field name
    "param": {
        "metric_type": "L2", # This parameter value must be identical to the one used in the collection schema
        "params": {"nprobe": 10}
    },
    "limit": 2 # Number of search results to return in this AnnSearchRequest
}
request_1 = AnnSearchRequest(**search_param_1)

query_vector2 = [[0.7958434224128723,0.18576304614543915,0.650543212890625,0.3026141822338104,0.7158203125]]
search_param_2 = {
    "data": query_vector2, # Query vector
    "anns_field": "embeddings2", # Vector field name
    "param": {
        "metric_type": "L2", # This parameter value must be identical to the one used in the collection schema
        "params": {"nprobe": 10}
    },
    "limit": 2 # Number of search results to return in this AnnSearchRequest
}
request_2 = AnnSearchRequest(**search_param_2)

reqs = [request_1, request_2]

rerank = WeightedRanker(0.8, 0.2)

print("start connecting to Milvus")
connections.connect("default", host=host, port=port,user=username,password=password)
coll = Collection(collection_name, consistency_level="Bounded")

res = coll.hybrid_search(
    reqs, # List of AnnSearchRequests created in step 1
    rerank, # Reranking strategy specified in step 2
    output_fields=['pk'],
    limit=2 # Number of final search results to return
)

print(res)

返回结果:

shell 复制代码
['[
"id: 0, distance: 1.0, entity: {'pk': 0}", 
"id: 568, distance: 0.7705678939819336, entity: {'pk': 568}"
]']
相关推荐
蹦蹦跳跳真可爱5899 分钟前
Python----OpenCV(几何变换--图像平移、图像旋转、放射变换、图像缩放、透视变换)
开发语言·人工智能·python·opencv·计算机视觉
蹦蹦跳跳真可爱58913 分钟前
Python----循环神经网络(Transformer ----Layer-Normalization(层归一化))
人工智能·python·rnn·transformer
夜阳朔18 分钟前
Conda环境激活失效问题
人工智能·后端·python
小Lu的开源日常23 分钟前
AI模型太多太乱?用 OpenRouter,一个接口全搞定!
人工智能·llm·api
mit6.8242 小时前
[Meetily后端框架] Whisper转录服务器 | 后端服务管理脚本
c++·人工智能·后端·python
Baihai IDP2 小时前
AI 系统架构的演进:LLM → RAG → AI Workflow → AI Agent
人工智能·ai·系统架构·llm·agent·rag·白海科技
沫儿笙2 小时前
弧焊机器人气体全方位节能指南
网络·人工智能·机器人
LONGZETECH2 小时前
【龙泽科技】新能源汽车维护与动力蓄电池检测仿真教学软件【吉利几何G6】
人工智能·科技·汽车·汽车仿真教学软件·汽车教学软件
jndingxin3 小时前
OpenCV 图像哈希类cv::img_hash::AverageHash
人工智能·opencv·哈希算法
Jamence3 小时前
多模态大语言模型arxiv论文略读(153)
论文阅读·人工智能·语言模型·自然语言处理·论文笔记