通过Python SDK V2调用PutVectors接口将向量数据上传到指定的向量索引中。
权限说明
阿里云账号默认拥有全部权限。阿里云账号下的RAM用户或RAM角色默认没有任何权限,需要阿里云账号或账号管理员通过RAM Policy或Bucket Policy授予操作权限。
API | Action | 说明 |
PutVectors |
| 写入向量数据。 |
方法定义
put_vectors(request: PutVectorsRequest, **kwargs) → PutVectorsResult
请求参数列表
参数名 | 类型 | 说明 |
request | PutVectorsRequest | 设置请求参数,包括存储桶名称、索引名称、向量数据等,具体请参见PutVectorsRequest |
返回值列表
类型 | 说明 |
PutVectorsResult | 返回值,具体请参见PutVectorsResult |
关于上传Vectors方法的完整定义,请参见put_vectors。
示例代码
import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.vectors as oss_vectors
parser = argparse.ArgumentParser(description="vector put vectors 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)
vectors = [
{
"data": {"float32": [0.1] * 128},
"key": "key1",
"metadata": {"metadata1": "value1", "metadata2": "value2"}
},
{
"data": {"float32": [0.2] * 128},
"key": "key2",
"metadata": {"metadata3": "value3", "metadata4": "value4"}
}
]
result = vector_client.put_vectors(oss_vectors.models.PutVectorsRequest(
bucket=args.bucket,
index_name=args.index_name,
vectors=vectors,
))
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
)
if __name__ == "__main__":
main()
相关文档
关于上传向量数据的完整示例代码,请参见put_vectors.py。
该文章对您有帮助吗?