You need to enable JavaScript to run this app.
Torch Object Storage

Torch Object Storage

Copy page
Download PDF
Vector index
CreateIndex(TOS Vectors Python SDK)
Copy page
Download PDF
CreateIndex(TOS Vectors Python SDK)
The create_index interface is used to create vector indexes. This article explains how to create vector indexes using the create_index interface of the TOS Vectors Python SDK. The interface supports specifying the index name, dimension, distance type, and description.​
Notes​
  • To prevent key leakage, the sample code reads the Access Key ID and Secret Access Key from environment variables. Therefore, before using the SDK, you need to configure long-term access credentials. For details, see Configuring Long-term Access Credentials.​
  • The TOS Vectors Python SDK supports temporary access credentials and provides timeout mechanisms, retry strategies, and other features. You can configure these flexibly based on your business needs to enhance application security. For details, see Common Configuration Scenarios.​
  • Vector index names must be unique within the vector bucket.​
Permissions required​
The primary account has full permissions by default, but IAM users have no permissions by default. If you need a sub-account to perform the create_index operation, the primary account must grant the following permissions to the sub-account through IAM policies and bucket policies:​
tosvectors:CreateIndex​
Code example​
Basic usage for creating a vector index​
​
# Basic usage of creating vector indexes​
import os​
import tos​
​
# Retrieve AK and SK information from environment variables.​
ak = os.getenv("TOS_ACCESS_KEY")​
sk = os.getenv("TOS_SECRET_KEY")​
# Fill in your endpoint and your region with the endpoint corresponding to the bucket's region. # For example, for China (Beijing), set your endpoint to https://tosvectors-cn-beijing.volces.com and your region to cn-beijing.​
endpoint = os.getenv("TOS_ENDPOINT")​
region = os.getenv("TOS_REGION")​
account_id = os.getenv("TOS_ACCOUNT_ID")​
vector_bucket_name = os.getenv("TOS_BUCKET")​
​
try:​
# Create a VectorClient object; all vector operations are handled through VectorClient.​
client = tos.VectorClient(ak, sk, endpoint, region)​
​
# Call create_index to create a vector index.​
response = client.create_index(​
account_id=account_id,​
vector_bucket_name=vector_bucket_name,​
index_name="examplevectorindex",​
data_type=tos.DataType.DataTypeFloat32,​
dimension=128,​
distance_metric=tos.DistanceMetricType.DistanceMetricCosine,​
)​
​
# Print the return result.​
print("Create the vector index successfully.")​
print("request_id: {}".format(response.request_id))​
print("status_code: {}".format(response.status_code))​
​
except tos.exceptions.TosClientError as e:​
# The operation failed and a client exception was caught. This is usually due to invalid request parameters or a network exception.​
print("fail with client error, message:{}, cause: {}".format(e.message, e.cause))​
except tos.exceptions.TosServerError as e:​
# The operation failed and a server exception occurred. See the response for the error details.​
print("fail with server error, code: {}".format(e.code))​
# Use the request ID for troubleshooting purposes. We strongly recommend including the request ID in the logs.​
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))​
​
​
Related documentation​
For the API documentation on CreateIndex, see CreateIndex.​
Last updated: 2026.02.06 18:01:46