功能說明
預測查詢是指將文本、圖片、視頻通過向量檢索版內建的向量化模型產生向量資料,並通過文本、圖片或視頻進行檢索的查詢方式。
註:若您已有向量並直接將向量匯入向量檢索版執行個體中進行檢索,請參考向量查詢。
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種
| 無 | 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/mp4base64_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/jpegbase64_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 }