from tos.models2 import VectorData
ak = os.getenv('TOS_ACCESS_KEY')
sk = os.getenv('TOS_SECRET_KEY')
endpoint = 'Provide your endpoint'
region = 'Provide your region'
account_id = os.getenv('TOS_ACCOUNT_ID')
vector_bucket_name = 'Provide your bucket name'
index_name = "testindex"
client = tos.VectorClient(ak, sk, endpoint, region)
query_vector = VectorData(float32=[float(random.random()) for _ in range(128)])
result = client.query_vectors(
vector_bucket_name=vector_bucket_name,
query_vector=query_vector,
print('Request ID: {}'.format(result.request_id))
print('Status Code: {}'.format(result.status_code))
print('Found {} similar vectors'.format(len(result.vectors)))
for i, vector in enumerate(result.vectors):
print('\nTop {}:'.format(i + 1))
print(' Key: {}'.format(vector.key))
print(' Distance: {:.4f}'.format(vector.distance))
print(' Metadata: {}'.format(vector.metadata))
if vector.data and vector.data.float32:
print(' Vector dimension: {}'.format(len(vector.data.float32)))
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))
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))
print('fail with unknown error: {}'.format(e))