通過Python SDK V2調用ListVectors介面列出指定向量索引中的所有向量,支援分頁遍曆大量向量資料。
許可權說明
阿里雲帳號預設擁有全部許可權。阿里雲帳號下的RAM使用者或RAM角色預設沒有任何許可權,需要阿里雲帳號或帳號管理員通過RAM Policy或Bucket Policy授予操作許可權。
API | Action | 說明 |
ListVectors |
| 列舉向量資料。 |
方法定義
Python SDK V2提供了兩種方式列舉向量資料:
list_vectors():直接調用介面,需要手動處理分頁。list_vectors_paginator():使用分頁器,SDK會自動處理分頁邏輯,推薦使用。
list_vectors(request: ListVectorsRequest, **kwargs) → ListVectorsResult[source]請求參數列表
參數名 | 類型 | 說明 |
request | ListVectorsRequest | 佈建要求參數,具體請參見ListVectorsRequest |
傳回值列表
類型 | 說明 |
ListVectorsResult | 傳回值,具體請參見ListVectorsResult |
關於列舉向量方法的完整定義,請參見list_vectors。
# 使用分頁器
list_vectors_paginator(**kwargs) → ListVectorsPaginator[source]傳回值列表
類型 | 說明 |
ListVectorsPaginator | 傳回值,具體請參見ListVectorsPaginator |
關於使用分頁器列舉向量方法的完整定義,請參見list_vectors_paginator。
範例程式碼
import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.vectors as oss_vectors
parser = argparse.ArgumentParser(description="list vectors sample")
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--account_id', help='The account id.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--index_name', help='The name of the vector index.', required=True)
def main():
args = parser.parse_args()
# Loading credentials values from the environment variables
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Using 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
client = oss_vectors.Client(cfg)
# Create the Paginator for the ListVectors operation
paginator = client.list_vectors_paginator()
# Create request with bucket and index name
request = oss_vectors.models.ListVectorsRequest(
bucket=args.bucket,
index_name=args.index_name
)
# Iterate through the vectors pages
for page in paginator.iter_page(request):
for o in page.vectors:
print(f'Vector: {o}')
if __name__ == "__main__":
main()相關文檔
關於列出向量的完整範例程式碼,請參見list_vectors.py。