创建Fusion模式向量 Index(Python SDK V2)

更新时间:
复制 MD 格式

使用Python SDK V2调用PutVectorIndexFusion接口在向量Bucket中创建Fusion模式的向量索引。

说明

Fusion Mode 目前处于邀测阶段,仅在印度尼西亚(雅加达)地域提供。Standard 模式向量索引的创建请参见 创建向量 Index(Python SDK V2)。

权限说明

阿里云账号默认拥有全部权限。阿里云账号下的 RAM 用户或 RAM 角色默认没有任何权限,需要阿里云账号或账号管理员通过 RAM Policy 或 Bucket Policy 授予操作权限。

API

Action

说明

PutVectorIndexFusion

oss:PutVectorIndex

创建Fusion模式的向量索引(Index)。

方法定义

put_vector_index_fusion(request: PutVectorIndexFusionRequest, **kwargs) → PutVectorIndexFusionResult

请求参数列表

参数名

类型

说明

request

PutVectorIndexFusionRequest

PutVectorIndexFusionRequest 对象,包含 bucket、index_name、schema_configuration 等参数。各字段的含义请参见 PutVectorIndexFusion 接口文档。

返回值列表

类型

说明

PutVectorIndexFusionResult

PutVectorIndexFusionResult 对象,包含 status_code、request_id 等信息。

示例代码

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.vectors as oss_vectors

parser = argparse.ArgumentParser(description="put vector index 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()

    # 从环境变量中加载访问凭证
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # 使用 SDK 默认配置
    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

    vector_client = oss_vectors.Client(cfg)

    # Fusion 索引通过 Schema 描述所有字段,区别于 put_vector_index 仅配置单个向量字段。
    # 以下为典型的电商商品 Schema:两个向量字段用于文本和图片双路召回,
    # 标量字段用于过滤和排序,分词后的标题字段用于全文检索。
    #
    # Schema 使用 dict 描述,服务端支持的任何字段类型和属性均可透传。
    # 文本字段默认使用 "standard" 分词器(英文按词、中文按字分词)。
    # 需要明确表示 false 时务必写出,省略和设为 false 对于服务端含义不同。
    # mode 默认为 "fusion",此处省略。
    schema_configuration = {
        "fields": [
            {
                "name": "text_vector",
                "type": "vector",
                "dataType": "float32",
                "dimension": 768,
                "distanceMetric": "euclidean"
            },
            {
                "name": "image_vector",
                "type": "vector",
                "dataType": "float32",
                "dimension": 512,
                "distanceMetric": "euclidean"
            },
            {
                "name": "title",
                "type": "string",
                "exactMatch": True,
                "text": {
                    "enabled": True,
                    "analyzer": "standard"
                }
            },
            {
                "name": "brand",
                "type": "string"
            },
            {
                "name": "category",
                "type": "string",
                "isArray": True
            },
            {
                "name": "price",
                "type": "double"
            },
            {
                "name": "stock",
                "type": "long"
            },
            {
                "name": "on_sale",
                "type": "bool"
            }
        ]
    }

    result = vector_client.put_vector_index_fusion(oss_vectors.models.PutVectorIndexFusionRequest(
        bucket=args.bucket,
        index_name=args.index_name,
        schema_configuration=schema_configuration
    ))

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
    )

if __name__ == "__main__":
    main()