使用Python SDK V2調用QueryVectorsFusion介面對Fusion模式的向量索引進行檢索,支援向量檢索、標量過濾、全文檢索索引以及多路召回融合。
說明
Fusion Mode 目前處於邀測階段,僅在印尼(雅加達)地區提供。Standard 模式向量索引的檢索請參見 檢索Vectors(Python SDK V2)。
許可權說明
阿里雲帳號預設擁有全部許可權。阿里雲帳號下的RAM使用者或RAM角色預設沒有任何許可權,需要阿里雲帳號或帳號管理員通過RAM Policy概述或Bucket Policy授予操作許可權。
|
API |
Action |
說明 |
|
QueryVectors |
|
查詢向量資料。 |
方法定義
invoke_operation(op_input: OperationInput, **kwargs) → OperationOutput
請求參數列表
|
參數名 |
類型 |
說明 |
|
op_input |
OperationInput |
通用操作輸入。需設定 op_name(QueryVectorsFusion)、method(POST)、parameters(鍵為 queryVectorsFusion)、bucket、headers 以及 JSON 格式的 body。body 中各欄位的含義請參見 QueryVectorsFusion 介面文檔。 |
傳回值列表
|
類型 |
說明 |
|
OperationOutput |
通用操作傳回值,包含 status_code、headers、http_response 等資訊。檢索結果可從 http_response.content 中解析。 |
範例程式碼
import argparse
import base64
import hashlib
import json
from requests.structures import CaseInsensitiveDict
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.vectors as oss_vectors
from alibabacloud_oss_v2 import OperationInput
parser = argparse.ArgumentParser(description="query vectors fusion sample")
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--index_name', help='The name of the fusion vector index.', required=True)
parser.add_argument('--account_id', help='The account id.', required=True)
def main():
args = parser.parse_args()
# Loading credentials values from the environment variables
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Using the SDK's default configuration
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
cfg.account_id = args.account_id
if args.endpoint is not None:
cfg.endpoint = args.endpoint
client = oss_vectors.Client(cfg)
# Query vectors fusion
json_data = json.dumps({
'indexName': args.index_name,
'knn': [
{
'field': 'text_vector',
'queryVector': [0.1, 0.2, 0.3, 0.4],
'topK': 10,
'numCandidates': 100,
'filter': {
'year': {
'$gte': 2020,
},
},
},
],
'query': {
'title': {
'$textMatch': {
'value': 'vector search',
'boost': 2.0,
},
},
},
'returnMetadata': True,
'returnMetadataFields': ['title', 'category', 'year'],
'limit': 10,
'sort': [
{
'_score': {
'order': 'desc',
},
},
],
})
body = json_data.encode()
content_md5 = base64.b64encode(hashlib.md5(body).digest()).decode()
op_input = OperationInput(
op_name='QueryVectorsFusion',
method='POST',
headers=CaseInsensitiveDict({
'Content-Type': 'application/json',
'Content-MD5': content_md5,
}),
parameters={
'queryVectorsFusion': '',
},
bucket=args.bucket,
body=body,
)
op_output = client.invoke_operation(op_input)
print(f'QueryVectorsFusion status code: {op_output.status_code},'
f' request id: {op_output.headers.get("x-oss-request-id", "")},'
f' content: {op_output.http_response.content},')
if __name__ == "__main__":
main()