Call PutVectorIndex using the Python SDK V2 to create a vector index in a vector bucket.
Permissions
An Alibaba Cloud account has all permissions by default. A Resource Access Management (RAM) user or RAM role has no permissions by default. The account owner or an administrator must grant permissions using a RAM policy or a bucket policy.
| API | Action | Description |
|---|---|---|
| PutVectorIndex | oss:PutVectorIndex | Creates a vector index. |
Prerequisites
Before you begin, ensure that you have:
A vector bucket
The
oss:PutVectorIndexpermission granted to your Alibaba Cloud account, RAM user, or RAM role
Method definition
put_vector_index(request: PutVectorIndexRequest, **kwargs) → PutVectorIndexResultRequest parameters
| Parameter | Type | Description |
|---|---|---|
request | PutVectorIndexRequest | Request parameters, including the vector bucket name and index configuration. See PutVectorIndexRequest. |
Return values
| Type | Description |
|---|---|
PutVectorIndexResult | The result object. See PutVectorIndexResult. |
For the complete method definition, see put_vector_index.
Sample code
The following example creates a vector index in a bucket. All configuration is passed through CLI arguments, and credentials are loaded from environment variables.
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()
# Load credentials from environment variables
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Use 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, # Set to match your embedding model's output dimension
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()References
For the complete sample code, see put_vector_index.py.