多模态数据检索是指向量数据库支持直接通过图文等多模态数据类型进行检索,且支持模态的组合,如文搜图,图搜图,图搜文+图等。
本页面主要介绍如何基于/index/search 接口实现多模态数据检索。
说明
请求向量数据库 VikingDB 的 OpenAPI 接口时,需要构造签名进行鉴权,详细的 OpenAPI 签名调用方法请参见 API签名调用指南。
URI | /api/index/search | 统一资源标识符 |
---|---|---|
请求方法 | POST | 客户端对向量数据库服务器请求的操作类型 |
请求头 | Content-Type: application/json | 请求消息类型 |
Authorization: HMAC-SHA256 *** | 鉴权 |
最简洁的多模态检索可通过配置search接口下的 order_by_raw
和 need_instruction
子参数实现,输入文本或图片tos链接,检索出与之最相似的数据。请求参数如下:
参数 | 子参数 | 类型 | 是否必选 | 默认值 | 参数说明 |
---|---|---|---|---|---|
collection_name/collection_alias | string | 是 | 指定检索的 Index 所属的 Collection 名称/别名。
| ||
index_name | string | 是 | 指定检索的 Index 名称。
| ||
resource_id | string | 否 | 资源ID | ||
search | order_by_raw | map | 是 | 多模态数据检索,当前支持文本、图片检索,字段如下:
| |
need_instruction | bool | 否 | 由模型默认值决定,豆包系列模型默认为true,其他模型默认为false | 用于多模态检索场景,对 query 做向量化时是否在 query 前增加 instruction。 |
在下面的示例中,假设多模态数据集下存放了如下 10 条数据。每条数据都有 id, picture,introduction,name,city, salary这几个字段,其中picture为image类型的字段,introduction为text类型的字段。
说明
关于多模态数据集的创建,参考创建数据集
{"id": "1", "name": "Alice", "city": "New York", "salary": 85000, "picture": "tos://example-bucket/pic1.jpg", "introduction": "Alice 是一位居住在 New York 的专业人士,年薪为 $85,000。"} {"id": "2", "name": "Bob", "city": "San Francisco", "salary": 120000, "picture": "tos://example-bucket/pic2.jpg", "introduction": "Bob 是一位居住在 San Francisco 的专业人士,年薪为 $120,000。"} {"id": "3", "name": "Charlie", "city": "Chicago", "salary": 78000, "picture": "tos://example-bucket/pic3.jpg", "introduction": "Charlie 是一位居住在 Chicago 的专业人士,年薪为 $78,000。"} {"id": "4", "name": "Diana", "city": "London", "salary": 95000, "picture": "tos://example-bucket/pic4.jpg", "introduction": "Diana 是一位居住在 London 的专业人士,年薪为 $95,000。"} {"id": "5", "name": "Ethan", "city": "Toronto", "salary": 91000, "picture": "tos://example-bucket/pic5.jpg", "introduction": "Ethan 是一位居住在 Toronto 的专业人士,年薪为 $91,000。"} {"id": "6", "name": "Fiona", "city": "Sydney", "salary": 104000, "picture": "tos://example-bucket/pic6.jpg", "introduction": "Fiona 是一位居住在 Sydney 的专业人士,年薪为 $104,000。"} {"id": "7", "name": "George", "city": "Berlin", "salary": 83000, "picture": "tos://example-bucket/pic7.jpg", "introduction": "George 是一位居住在 Berlin 的专业人士,年薪为 $83,000。"} {"id": "8", "name": "Hannah", "city": "Paris", "salary": 98000, "picture": "tos://example-bucket/pic8.jpg", "introduction": "Hannah 是一位居住在 Paris 的专业人士,年薪为 $98,000。"} {"id": "9", "name": "Ian", "city": "Tokyo", "salary": 89000, "picture": "tos://example-bucket/pic9.jpg", "introduction": "Ian 是一位居住在 Tokyo 的专业人士,年薪为 $89,000。"} {"id": "10", "name": "Julia", "city": "Singapore", "salary": 115000, "picture": "tos://example-bucket/pic10.jpg", "introduction": "Julia 是一位居住在 Singapore 的专业人士,年薪为 $115,000。"}
curl -i -X POST \ -H 'Content-Type: application/json' \ -H 'Authorization: HMAC-SHA256 ***' \ https://api-vikingdb.volces.com/api/index/search \ -d '{ "collection_name": "test_name", //用于检索的集合的名称 "index_name": "index_test", //用于检索的索引名称 "search": { "order_by_raw":{ "text":"hello" } } }'
curl -i -X POST \ -H 'Content-Type: application/json' \ -H 'Authorization: HMAC-SHA256 ***' \ https://api-vikingdb.volces.com/api/index/search \ -d '{ "collection_name": "test_name", //用于检索的集合的名称 "index_name": "index_test", //用于检索的索引名称 "search": { "order_by_raw":{ "image":"tos://best-practice-pic-search-tos/pic_search_1000_images/003bd60fa9.jpg}" } } }'
curl -i -X POST \ -H 'Content-Type: application/json' \ -H 'Authorization: HMAC-SHA256 ***' \ https://api-vikingdb.volces.com/api/index/search \ -d '{ "collection_name": "test_name", //用于检索的集合的名称 "index_name": "index_test", //用于检索的索引名称 "search": { "order_by_raw":{ "text":"hello" , "image":"tos://best-practice-pic-search-tos/pic_search_1000_images/003bd60fa9.jpg}" } } }'
执行成功返回示例:
{ "code": 0, "data": [ [ { "fields": { "city": "New York", "id": 1, "name": "Alice", "salary": 85000, "picture": "tos://example-bucket/pic1.jpg", "introduction": "Alice 是一位居住在 New York 的专业人士,年薪为 $85,000。" }, "id": 1, "score": 1.0002950429916382 }, { "fields": { "city": "San Francisco", "id": 2, "name": "Bob", "salary": 120000, "picture": "tos://example-bucket/pic2.jpg", "introduction": "Bob 是一位居住在 San Francisco 的专业人士,年薪为 $120,000。" }, "id": 2, "score": -0.017884135246276855 } ], [ { "fields": { "city": "San Francisco", "id": 2, "name": "Bob", "salary": 120000, "picture": "tos://example-bucket/pic2.jpg", "introduction": "Bob 是一位居住在 San Francisco 的专业人士,年薪为 $120,000。" }, "id": 2, "score": 0.9990343451499939 }, { "fields": { "city": "New York", "id": 1, "name": "Alice", "salary": 85000, "picture": "tos://example-bucket/pic1.jpg", "introduction": "Alice 是一位居住在 New York 的专业人士,年薪为 $85,000。" }, "id": 1, "score": -0.018528103828430176 } ] ], "message": "success", "request_id": "02174781958394600000000000000000000ffff0a00744bebcae7" }
如果在创建索引时划分了子索引 partition,并且查询目标可以具体到其中的一个 partition,可以在检索时指定在特定partiton中进行检索,从而减少扫描的数据量,提高检索速度。
在基础向量检索的基础上,通过配置search接口下的partition
参数可以实现对特定子索引的检索:
参数 | 子参数 | 类型 | 是否必选 | 默认值 | 参数说明 |
---|---|---|---|---|---|
search | partition | string/int | 否 | "default" | 子索引名称,类型与 partition_by 的 field_type 一致,字段值对应 partition_by 的 field_value。
|
假设在创建索引时设置了city字段为子索引字段,根据city字段的取值将数据集划分成了不同的子数据集,现在想对名为London的partiton进行检索,可以通过如下方式实现:
curl -i -X POST \ -H 'Content-Type: application/json' \ -H 'Authorization: HMAC-SHA256 ***' \ https://api-vikingdb.volces.com/api/index/search \ -d '{ "collection_name": "test_name", //用于检索的集合的名称 "index_name": "index_test", //用于检索的索引名称 "search": { "order_by_raw":{ "text":"hello" , "image":"tos://best-practice-pic-search-tos/pic_search_1000_images/003bd60fa9.jpg}" }, "partiton": "London" } }'
检索过滤是指在向量相似度检索过程中,结合结构化的元数据(如数值、布尔值、字符串等)对候选数据进行筛选的过程。这种过滤可以提高检索结果的相关性和精度。
检索过滤为可选能力,需要配合基础的向量检索能力使用,可以配置一种或多种检索过滤方式。
标量过滤检索是指在向量数据库中,同时使用向量检索和标量检索两种方法进行检索。在标量过滤检索中,使用向量检索来匹配向量的相似度,同时使用标量检索来匹配向量的标量值。
在基础向量检索的基础上,可以通过配置search接口下的filter
参数来实现对检索结果的标量过滤,详细的使用方法参考标量过滤。
主键过滤是指在向量检索时,仅对指定主键值数据执行近邻搜索,从而有效缩小检索范围并提升性能与准确度
在基础向量检索的基础上,可以通过配置search接口下的 primary_key_in
/primary_key_not_in
子参数对特定主键值的数据进行检索,详细的使用方法参考主键过滤。
后置过滤是 VikingDB 提供的一种检索后处理能力,用于在向量召回之后,对候选数据进行进一步的过滤和优化,确保最终返回的搜索结果更加精准、高效。VikingDB 提供了以下几种后置过滤能力:
在基础向量检索的基础上,可以通过配置search接口下的 post_process_ops
和post_process_input_limit
配置后置过滤能力,详细的使用方法详见后置处理算子。
通过配置search接口下的 output_fields
子参数可以限制检索结果只返回指定的字段:
参数 | 子参数 | 类型 | 是否必选 | 默认值 | 参数说明 |
---|---|---|---|---|---|
search | output_fields | list<string> | 否 | 过滤字段,指定要返回的标量或向量字段列表。
如果索引的距离方式为cosine,向量字段返回的向量是归一化后的向量。 |
例如希望在返回结果中只保留“name”字段:
curl -i -X POST \ -H 'Content-Type: application/json' \ -H 'Authorization: HMAC-SHA256 ***' \ https://api-vikingdb.volces.com/api/index/search \ -d '{ "collection_name": "test_name", //用于检索的集合的名称 "index_name": "index_test", //用于检索的索引名称 "search": { "order_by_raw":{ "text":"hello" , "image":"tos://best-practice-pic-search-tos/pic_search_1000_images/003bd60fa9.jpg}" }, "output_fields": ['name'] } }'
分页能力通过在检索请求中指定 offset
和 limit
参数,将结果分批返回,在降低单次响应延迟的同时便于前端交互和资源管理
参数 | 子参数 | 类型 | 是否必选 | 默认值 | 参数说明 |
---|---|---|---|---|---|
search | limit | int | 否 | 10 | 检索结果数量,最大5000个。 |
offset | int | 否 | 0 | 偏移量。仅分页场景下使用,不建议设置过大值,否则有深分页影响。默认值为0。设置值至少为0,语义和mysql的offset相同。 |
例如指定返回10个结果:
curl -i -X POST \ -H 'Content-Type: application/json' \ -H 'Authorization: HMAC-SHA256 ***' \ https://api-vikingdb.volces.com/api/index/search \ -d '{ "collection_name": "test_name", //用于检索的集合的名称 "index_name": "index_test", //用于检索的索引名称 "search": { "order_by_raw":{ "text":"hello" , "image":"tos://best-practice-pic-search-tos/pic_search_1000_images/003bd60fa9.jpg}" }, "limit": 2, # 每页返回 2 条结果 "offset": 4 # 跳过前 4 条结果,获取第三页结果 } }'
参数 | 参数说明 |
---|---|
code | 状态码 |
message | 返回信息 |
request_id | 标识每个请求的唯一标识符 |
data | 检索结果,向量检索会返回检索到的主键、score、fields。 |
状态码 | http状态码 | 返回信息 | 状态码说明 |
---|---|---|---|
0 | 200 | drop index success | 向量检索成功。 |
1000008 | 400 | index not exist | 指定的 Index 不存在。 |
1000003 | 400 | invalid request | 非法参数:
|
1000001 | 401 | unauthorized | 请求头中缺乏鉴权信息。 |
1000002 | 403 | no permission | 权限不足。 |
1000016 | 400 | invalid vectors for index_recall | 输入的向量格式不合法。 |
1000029 | 429 | 请求已达上限, 请调整CPU核数 | 需要调大 cpu_quota |
curl -i -X POST \ -H 'Content-Type: application/json' \ -H 'Authorization: HMAC-SHA256 ***' \ https://api-vikingdb.volces.com/api/index/search \ -d '{ "collection_name": "test_name", //用于检索的集合的名称 "index_name": "index_test", //用于检索的索引名称 "search": { "order_by_raw":{ "text":"hello" , "image":"tos://best-practice-pic-search-tos/pic_search_1000_images/003bd60fa9.jpg}" }, "partiton": "London",//指定partition, "filter": { "op": "range", "field": "salary", "gte": 10000 }, "post_process_ops": { "op": "string_contain", "field": "name", "pattern": "s" }, "output_fields": ['name'], "limit": 2, # 每页返回 2 条结果 "offset": 4 # 跳过前 4 条结果,获取第三页结果 } } }'
执行成功返回:
HTTP/1.1 200 OK Content-Length: 43 Content-Type: application/json { "code":0, "msg":"search success", "request_id":"021695029537650fd001de666660000000000000000000230da93", "data": [ [ { "id": 1, "score": 0.99, "fields": { "name": "Alice" } }, { "id": 2, "score": 0.98, "fields": { "name": "Fiona" } } ], [ { "id": 9, "score": 0.95, "fields": { "name": "Charlie" } }, { "id": 8, "score": 0.84, "fields": { "name": "Ethan" } } ] ] }
执行失败返回:
HTTP/1.1 400 OK Content-Length: 43 Content-Type: application/json {"code":1000008, "msg":"index not exist","request_id":"021695029537650fd001de666660000000000000000000230da93"}