You need to enable JavaScript to run this app.
文档中心
对象存储

对象存储

复制全文
下载 pdf
向量
PutVectors(TOS Vectors Python SDK)
复制全文
下载 pdf
PutVectors(TOS Vectors Python SDK)
put_vectors 接口用于向指定 collection 写入向量数据,本文介绍如何通过 TOS Vectors Python SDK 的 put_vectors 接口完成批量向量写入。​
注意事项​
  • 为了防止密钥泄露,本文示例代码需要从环境变量中读取 Access Key ID 和 Secret Access Key。因此使用 SDK 前,您需要配置长期访问凭证,详细介绍,请参见 配置长期访问凭证。​
  • TOS Vectors Python SDK 支持使用临时访问凭证;也支持配置超时机制、重试策略等多种机制,您可以根据业务需求灵活设置,提升应用安全性,详细介绍,请参见 常见配置场景。​
  • TOS Vectors 底层支持向量 float32 精度,如果传入高于此精度的向量,可能会导致精度损失。​
权限说明​
主账号默认拥有全部权限,但是 IAM 用户默认没有任何权限。如果您需要使用 IAM 用户执行 put_vectors 操作,主账号需通过 IAM策略和桶策略为该 IAM 用户授予以下权限:tosvectors:PutVectors​
代码示例​
向量索引中批量插入向量​
​
# 向量索引中批量插入向量​
import os​
import tos​
​
# 从环境变量获取 AK 和 SK 信息。​
ak = os.getenv('TOS_ACCESS_KEY')​
sk = os.getenv('TOS_SECRET_KEY')​
# your endpoint 和 your region 填写Bucket 所在区域对应的Endpoint。# 以华北2(北京)为例,your endpoint 填写 https://tosvectors-cn-beijing.volces.com,your region 填写 cn-beijing。​
endpoint = 'Provide your endpoint'​
region = 'Provide your region'​
account_id = os.getenv('TOS_ACCOUNT_ID')​
vector_bucket_name = 'Provide your bucket name'​
​
try:​
# 创建 VectorClient 对象,对向量的操作都通过 VectorClient 实现​
client = tos.VectorClient(ak, sk, endpoint, region)​
​
# 准备向量数据 - 创建3个128维的float32向量​
vectors = []​
for i in range(3):​
# 创建向量数据,每个向量128维​
vector_data = tos.models2.VectorData(​
float32=[float(x) for x in range(128)] # 128维向量数据​
)​
​
# 创建向量对象​
vector = tos.models2.Vector(​
key=f'vector-{i}', # 向量唯一键​
data=vector_data # 向量数据​
)​
vectors.append(vector)​
​
# 指定索引名称​
index_name = 'test'​
​
# 批量插入向量到向量索引中​
result = client.put_vectors(​
vector_bucket_name=vector_bucket_name,​
account_id=account_id,​
index_name=index_name,​
vectors=vectors​
)​
​
# 打印操作结果​
print('批量插入向量成功')​
print('Request ID:', result.request_id)​
print('Status Code:', result.status_code)​
print('成功插入向量数量:', len(vectors))​
​
except tos.exceptions.TosClientError as e:​
# 操作失败,捕获客户端异常,一般情况为非法请求参数或网络异常​
print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))​
except tos.exceptions.TosServerError as e:​
# 操作失败,捕获服务端异常,可从返回信息中获取详细错误信息​
print('fail with server error, code: {}'.format(e.code))​
# request id 可定位具体问题,强烈建议日志中保存​
print('error with request id: {}'.format(e.request_id))​
print('error with message: {}'.format(e.message))​
print('error with http code: {}'.format(e.status_code))​
print('error with ec: {}'.format(e.ec))​
print('error with request url: {}'.format(e.request_url))​
except Exception as e:​
print('fail with unknown error: {}'.format(e))​
​
向量索引中批量插入带元数据的向量​
​
# 向量索引中批量插入带元数据的向量​
import os​
import tos​
import time​
​
# 从环境变量获取 AK 和 SK 信息。​
ak = os.getenv('TOS_ACCESS_KEY')​
sk = os.getenv('TOS_SECRET_KEY')​
# your endpoint 和 your region 填写Bucket 所在区域对应的Endpoint。# 以华北2(北京)为例,your endpoint 填写 https://tosvectors-cn-beijing.volces.com,your region 填写 cn-beijing。​
endpoint = 'Provide your endpoint'​
region = 'Provide your region'​
account_id = os.getenv('TOS_ACCOUNT_ID')​
vector_bucket_name = 'Provide your bucket name'​
​
try:​
# 创建 VectorClient 对象,对向量的操作都通过 VectorClient 实现​
client = tos.VectorClient(ak, sk, endpoint, region)​
​
# 准备向量数据 - 创建带元数据的向量​
vectors = []​
categories = ['electronics', 'clothing', 'books', 'sports', 'home']​
​
for i in range(5):​
# 创建向量数据,每个向量128维​
vector_data = tos.models2.VectorData(​
float32=[float(i * 0.1 + x * 0.01) for x in range(128)] # 128维向量数据​
)​
​
# 创建带元数据的向量对象​
vector = tos.models2.Vector(​
key=f'product-vector-{i}', # 向量唯一键​
data=vector_data, # 向量数据​
metadata={ # 向量元数据​
'product_id': f'prod_{i:06d}',​
'category': categories[i % len(categories)],​
'name': f'Product {i}',​
'price': round(10.0 + i * 5.5, 2),​
'timestamp': str(int(time.time() * 1000)),​
'description': f'This is product {i} in {categories[i % len(categories)]} category'​
}​
)​
vectors.append(vector)​
​
# 指定索引名称​
index_name = 'test'​
​
# 批量插入带元数据的向量到向量索引中​
result = client.put_vectors(​
vector_bucket_name=vector_bucket_name,​
account_id=account_id,​
index_name=index_name,​
vectors=vectors​
)​
​
# 打印操作结果​
print('批量插入带元数据的向量成功')​
print('Request ID:', result.request_id)​
print('Status Code:', result.status_code)​
print('成功插入向量数量:', len(vectors))​
print('\n插入的向量信息:')​
for i, vector in enumerate(vectors):​
print(f' 向量 {i+1}: key={vector.key}, category={vector.metadata["category"]}, price=${vector.metadata["price"]}')​
​
except tos.exceptions.TosClientError as e:​
# 操作失败,捕获客户端异常,一般情况为非法请求参数或网络异常​
print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))​
except tos.exceptions.TosServerError as e:​
# 操作失败,捕获服务端异常,可从返回信息中获取详细错误信息​
print('fail with server error, code: {}'.format(e.code))​
# request id 可定位具体问题,强烈建议日志中保存​
print('error with request id: {}'.format(e.request_id))​
print('error with message: {}'.format(e.message))​
print('error with http code: {}'.format(e.status_code))​
print('error with ec: {}'.format(e.ec))​
print('error with request url: {}'.format(e.request_url))​
except Exception as e:​
print('fail with unknown error: {}'.format(e))​
​
相关文档​
关于 PutVectors 的 API 文档,请参见 PutVectors。​
​
最近更新时间:2026.08.10 17:31:19
这个页面对您有帮助吗?
有用
有用
无用
无用