使用Python SDK V2调用PutVectorIndex接口在向量Bucket中创建向量索引。
权限说明
阿里云账号默认拥有全部权限。阿里云账号下的RAM用户或RAM角色默认没有任何权限,需要阿里云账号或账号管理员通过RAM Policy或Bucket Policy授予操作权限。
API  | Action  | 说明  | 
PutVectorIndex  | 
  | 创建向量索引。  | 
方法定义
put_vector_index(request: PutVectorIndexRequest, **kwargs) → PutVectorIndexResult请求参数列表
参数名  | 类型  | 说明  | 
request  | PutVectorIndexRequest  | 设置请求参数,例如设置向量存储桶的名称等,具体请参见PutVectorIndexRequest。  | 
返回值列表
类型  | 说明  | 
PutVectorIndexResult  | 返回值,具体请参见PutVectorIndexResult。  | 
关于创建向量Index方法的完整定义,请参见put_vector_Index。
示例代码
import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.vectors as oss_vectors
parser = argparse.ArgumentParser(description="vector put vector index 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 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
    vector_client = oss_vectors.Client(cfg)
    result = vector_client.put_vector_index(oss_vectors.models.PutVectorIndexRequest(
        bucket=args.bucket,
        index_name=args.index_name,
        dimension=512,
        data_type='float32',
        distance_metric='euclidean',
        metadata={"nonFilterableMetadataKeys": ["key1", "key2"]}
    ))
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
    )
if __name__ == "__main__":
    main()相关文档
关于创建向量Bucket的完整示例代码,请参见put_vector_index.py。
该文章对您有帮助吗?