Use get_vectors to retrieve vector data for specified keys from a vector index. Pass a list of keys and control whether to include vector values, metadata, or both in the response.
Permissions
An Alibaba Cloud account has all permissions by default. By default, Resource Access Management (RAM) users or RAM roles do not have any permissions. The Alibaba Cloud account or an administrator must grant permissions using a RAM policy or a bucket policy.
| API | Action | Description |
|---|---|---|
| GetVectors | oss:GetVectors | Gets vector data. |
Method definition
get_vectors(request: GetVectorsRequest, **kwargs) → GetVectorsResultRequest parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
bucket | str | Yes | The name of the bucket. |
index_name | str | Yes | The name of the vector index. |
keys | list[str] | Yes | A list of keys identifying the vectors to retrieve. |
return_data | bool | No | Whether to include vector values in the response. Set to True only when you need raw vector values; omitting it reduces data transfer when you only need metadata. |
return_metadata | bool | No | Whether to include metadata in the response. Set to True to retrieve the metadata associated with each vector. |
For the complete GetVectorsRequest definition, see GetVectorsRequest.
Return values
| Type | Description |
|---|---|
GetVectorsResult | Contains the queried vector data. The vectors field holds the list of returned vectors; each item exposes the vector values and metadata based on the flags you set. |
For the complete GetVectorsResult definition, see GetVectorsResult.
For the complete get_vectors method definition, see get_vectors.
Sample code
The following example retrieves two vectors by key, including both vector values and metadata. The example loads credentials from environment variables.
import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.vectors as oss_vectors
parser = argparse.ArgumentParser(description="vector get 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()
# 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)
keys = ['key1', 'key2']
result = vector_client.get_vectors(oss_vectors.models.GetVectorsRequest(
bucket=args.bucket,
index_name=args.index_name,
keys=keys,
return_data=True,
return_metadata=True
))
print(f'status code: {result.status_code},'
f' request id: {result.request_id}')
if result.vectors:
for vector in result.vectors:
print(f'vector id: {vector}')
if __name__ == "__main__":
main()References
For the complete sample code, see get_vectors.py.