预测查询

功能说明

预测查询是指将文本、图片、视频通过向量检索版内置的向量化模型生成向量数据,并通过文本、图片或视频进行检索的查询方式。

注:若您已有向量并直接将向量导入向量检索版实例中进行检索,请参考向量查询

URL

/vector-service/inference-query

  • 以上 URL 省略了请求Header参数及编码等因素。

  • 以上 URL 中省略了访问应用的 host 地址。

  • 以上URL 中拼接的所有查询参数,请查看下方“查询参数”的参数定义、使用方式及样例。

请求协议

HTTP

请求方式

POST

支持格式

JSON

签名机制

可用以下方法计算签名(authorization

参数

类型

描述

accessUserName

string

用户名,可在实例详情页>网络信息查看

accessPassWord

string

密码,可在实例详情页>网络信息修改

import com.aliyun.darabonba.encode.Encoder;
import com.aliyun.darabonbastring.Client;

public class GenerateAuthorization {
 public static void main(String[] args) throws Exception {
 String accessUserName = "username";
 String accessPassWord = "password";
 String realmStr = "" + accessUserName + ":" + accessPassWord + "";
 String authorization = Encoder.base64EncodeToString(Client.toBytes(realmStr, "UTF-8"));
 System.out.println(authorization);
 }
}

authorization正确返回格式:

cm9vdDp******mdhbA==

使用HTTP请求设置authorization参数需加上Basic前缀

示例:(在header中加入)

authorization: Basic cm9vdDp******mdhbA==

请求body参数

参数名称

描述

默认值

类型

是否必须

tableName

查询的表名

string

indexName

查询的索引名称

配置的第一个索引

string

content

需要预测的数据

string

contentType

视频预测数据类型:

text、image、video_uri、video_base64

string

modal

向量化模型的值有3

  • text:文本向量化就是以文搜文,图片向量化就是以文搜图

  • image:图片向量化就是以图搜图

  • video:视频向量化支持文本、图片、视频搜视频

string

videoFrameTopK

召回帧的数量

100

int

namespace

查询向量的空间

""

string

topK

返回个数

100

int

includeVector

是否返回文档中的向量信息

false

bool

outputFields

需要返回值的字段列表

[]

list[string]

order

排序顺序, ASC:升序 DESC: 降序

ASC

string

searchParams

查询参数

""

string

filter

过滤表达式

""

string

scoreThreshold

分数过滤, 使用欧式距离时,只返回小于scoreThreshold的结果。使用内积时,只返回大于scoreThreshold的结果

默认不过滤

float

返回参数

字段名称

描述

类型

result

结果列表

list[Item]

totalCount

result中的个数

int

totalTime

引擎处理耗时,单位ms

float

errorCode

错误码,有错误时才有该字段

int

errorMsg

错误信息,有错误时才有该字段

string

  • item定义

字段名称

描述

类型

score

距离分

float

fields

字段名称和对应的值

map<string, FieldType>

vector

向量值

list[float]

id

主键值,类型为所定义的字段类型

FieldType

namespace

向量的名称空间,如果设置了namespace会返回该字段

string

示例

文本向量化检索

  • 请求body:

    {
      "tableName": "gist",
      "indexName": "test",
      "content": "hello",
      "modal": "text",
      "topK": 3,
      "searchParams":"{\"qc.searcher.scan_ratio\":0.01}",
      "includeVector": true
    }
  • 返回参数:

    {
      "result":[
        {
          "id": 1,
          "score":1.0508723258972169,
          "vector": [0.1, 0.2, 0.3]
        },
        {
          "id": 2,
          "score":1.0329746007919312,
          "vector": [0.2, 0.2, 0.3]
        },
        {
          "id": 3,
          "score":0.980593204498291,
          "vector": [0.3, 0.2, 0.3]
        }
      ],
      "totalCount":3,
      "totalTime":2.943
    }

图片向量化

以文搜图

  • 请求body:

    {
      "tableName": "gist",
      "indexName": "test",
      "content": "自行车",
      "modal": "text",
      "topK": 3,
      "searchParams":"{\"qc.searcher.scan_ratio\":0.01}",
      "includeVector": true
    }
  • 返回参数:

    {
      "result":[
        {
          "id": 1,
          "score":1.0508723258972169,
          "vector": [0.1, 0.2, 0.3]
        },
        {
          "id": 2,
          "score":1.0329746007919312,
          "vector": [0.2, 0.2, 0.3]
        },
        {
          "id": 3,
          "score":0.980593204498291,
          "vector": [0.3, 0.2, 0.3]
        }
      ],
      "totalCount":3,
      "totalTime":2.943
    }

以图搜图

  • 请求body:

    {
      "tableName": "gist",
      "indexName": "test",
      "content": "base64编码的图片",
      "modal": "image",
      "topK": 3,
      "searchParams":"{\"qc.searcher.scan_ratio\":0.01}",
      "includeVector": true
    }
  • 返回参数:

    {
        "totalCount": 5,
        "result": [
            {
                "id": 5,
                "score": 1.103209137916565
            },
            {
                "id": 3,
                "score": 1.1278988122940064
            },
            {
                "id": 2,
                "score": 1.1326735019683838
            }
        ],
        "totalTime": 242.615
    }

主体识别

  • 请求body:

    未传入range:

    {
     "tableName": "gist",
     "indexName": "test",
     "content": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQ",
     "modal": "image",
     "searchParams": "{\"crop\": true}",
     "topK": 3,
     "includeVector": true
    }

    "crop":true表示使用主体进行查询,未传入range则会调用主体识别模型

    传入range:

    {
     "tableName": "gist",
     "indexName": "test",
     "content": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQ",
     "modal": "image",
     "searchParams": "{\"crop\": true, \"range\": \"100,100,60,70\"}",
     "topK": 3,
     "includeVector": true
    }

    "crop":true, "range":"100,100,60,70"表示使用主体进行查询,range表示主体在图片中的区域,四个数分别表示主体区域左上角点的(x, y)坐标,宽度width,高度height。

  • 返回参数:

    {
     "result":[
     {
     "id": 1,
     "score":1.0508723258972169,
     "vector": [0.1, 0.2, 0.3]
     }
     ],
     "__meta__": {
     "__range__": "100,100,60,70;",
     }
     "totalCount":1,
     "totalTime":2.943
    }

    • 主体识别中modal=image时,返回参数中才会有__range__ 相关主体识别的内容返回

    • __range__表示主体在图片中的区域,四个数分别表示主体区域左上角点的(x, y)坐标,宽度width,高度height。

    • 如果模型识别出了多个主体,__range__按照模型打分从高到低排列每个主题,且默认返回第一个主体的向量查询结果。

文本召回视频

  • 请求body:

    {
      "tableName": "video",
      "content": "hello",
      "modal": "video",
      "topK": 3,
      "videoFrameTopK":100,
      "contentType":"text",
      "searchParams":"{\"qc.searcher.scan_ratio\":0.01}"
    }
  • 返回参数:

    {
      "result":[
        {
          "videoId": 1,
          "videoUri": "oss://...",
          "fields" : {
            "tag" : "demo"
          },
          "clips": [{
              "queryStartTime": 5, //查询视频帧的时间点(单位秒)
              "startTime": 5, //匹配视频帧的时间点(单位秒)
              "duration": 5, // 匹配时长(单位秒)
              "queryStartFrameIndex": 150, //查询视频帧 开始index
              "queryEndFrameIndex": 300, //查询视频帧 结束index
              "startFrameIndex": 150, //匹配视频帧 开始index
              "endFrameIndex": 300, //匹配视频帧 结束index       
              "sim": 0.8 // 综合相似度
           }]
        }
      ],
      "totalCount":1,
      "totalTime":2.943
    }

视频召回视频

支持的视频格式有mp4、avi、mkv、mov、flv、webm。

  • 请求body:

    {
      "tableName": "video",
      "content": "oss://...",
      "modal": "video",
      "topK": 3,
      "videoFrameTopK":100,
      "contentType":"video_uri",
      "searchParams":"{\"qc.searcher.scan_ratio\":0.01}"
    }

    输入文件的OSS路径,例如oss://bucket-name/xxx/xxx.mp4:

    {
      "tableName": "video",
      "content": "data:video/mp4;base64,AAAAIGZ0eXBtcDQyAAABAGlxxxxxxx",
      "modal": "video",
      "topK": 3,
      "videoFrameTopK":100,
      "contentType":"video_encode",
      "searchParams":"{\"qc.searcher.scan_ratio\":0.01}"
    }

    格式为data:video/{format};base64,{base64_video},其中:

    • image/{format}:视频的格式,例如视频为mp4格式,则设置为video/mp4

    • base64_video:图像的BASE64数据

  • 返回参数:

    {
      "result":[
        {
          "videoId": 1,
          "videoUri": "oss://...",
          "fields" : {
            "tag" : "demo"
          },      
          "clips": [{
              "queryStartTime": 5, //查询视频帧的时间点(单位秒)
              "startTime": 5, //匹配视频帧的时间点(单位秒)
              "duration": 5, // 匹配时长(单位秒)
              "queryStartFrameIndex": 150, //查询视频帧 开始index
              "queryEndFrameIndex": 300, //查询视频帧 结束index
              "startFrameIndex": 150, //匹配视频帧 开始index
              "endFrameIndex": 300, //匹配视频帧 结束index
              "sim": 0.8 // 综合相似度
           }]
        }
      ],
      "totalCount":1,
      "totalTime":2.943
    }

图片召回视频

支持的图片格式有png、jpeg、jpg。

  • 请求body:

    {
      "tableName": "video",
      "content": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAxxxxxx",
      "modal": "video",
      "topK": 3,
      "videoFrameTopK":100,
      "contentType":"image_encode", 
      "searchParams":"{\"qc.searcher.scan_ratio\":0.01}"
    }

    图片使用base64数据,将编码后的base64数据传递给image参数,格式为data:image/{format};base64,{base64_image},其中:

    • image/{format}:本地图像的格式。请根据实际的图像格式设置参数,例如图片为jpg格式,则设置为image/jpeg

    • base64_image:图像的base64数据。

  • 返回参数:

    {
      "result":[
        {
          "videoId": 1,
          "videoUri": "oss://...",
          "fields" : {
            "tag" : "demo"
          },      
          "clips": [{
              "queryStartTime": 5, //查询视频帧的时间点(单位秒)
              "startTime": 5, //匹配视频帧的时间点(单位秒)
              "duration": 5, // 匹配时长(单位秒)
              "queryStartFrameIndex": 150, //查询视频帧 开始index
              "queryEndFrameIndex": 300, //查询视频帧 结束index
              "startFrameIndex": 150, //匹配视频帧 开始index
              "endFrameIndex": 300, //匹配视频帧 结束index
              "sim": 0.8 // 综合相似度
           }]
        }
      ],
      "totalCount":3,
      "totalTime":2.943
    }