全部產品
Search
文件中心

OpenSearch:預測查詢

更新時間:Jul 13, 2024

功能說明

預測查詢是指使用者將文本、圖片通過向量檢索版內建的向量化模型產生向量,並通過文本或圖片進行檢索的查詢方式。

註:若您已有向量並直接將向量匯入向量檢索版執行個體中進行檢索,請參考向量查詢

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

content

需要預測的資料

string

modal

向量化模型的值有2種

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

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

string

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",
	"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",
	"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",
	"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",
 "content": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQ",
 "modal": "image",
 "searchParams": "{\"crop\": true}",
 "topK": 3,
 "includeVector": true
}

"crop":true表示使用主體進行查詢,未傳入range則會調用主體識別模型

傳入range:

{
 "tableName": "gist",
	"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__按照模型打分從高到低排列每個主題,且預設返回第一個主體的向量查詢結果。