View a markdown version of this page

管理資料集 - Amazon Bedrock AgentCore

管理資料集

本主題涵蓋建立、擷取、列出、更新和刪除資料集。

建立資料集

CreateDataset API 會建立新的評估資料集。這是非同步操作 (HTTP 202) — CREATING ACTIVE 擷取完成後,資料集會從 轉換為 。

必要參數: datasetName(僅限英數和底線、^[a-zA-Z][a-zA-Z0-9_]{0,47}$)schemaType、 和 source(內嵌範例或 S3 URI)。

選用參數:descriptionkmsKeyArn(客戶受管加密金鑰,建立後不可變 — 請參閱資料集加密)、tags

下列範例示範如何建立資料集:

範例
AgentCore CLI
  1. # Add a dataset to your project agentcore add dataset --name my_eval_dataset \ --schema-type AGENTCORE_EVALUATION_PREDEFINED_V1 # Edit the generated JSONL file with your scenarios # File location: agentcore/datasets/my_eval_dataset.jsonl # Deploy to create the dataset in your AWS account agentcore deploy

    這會建立本機 JSONL 檔案,並在專案組態中註冊資料集。執行 agentcore deploy以建立資料集資源,並將範例同步至 服務。

    注意

    從 AgentCore 專案目錄 (使用 建立) 內執行此操作agentcore create

AgentCore SDK
  1. from bedrock_agentcore.evaluation import DatasetClient client = DatasetClient(region_name="us-west-2") # Create with inline examples (polls until ACTIVE) ds = client.create_dataset_and_wait( datasetName="customer_support_scenarios", schemaType="AGENTCORE_EVALUATION_PREDEFINED_V1", source={ "inlineExamples": { "examples": [ { "scenario_id": "TC-01", "turns": [{"input": "What is my balance?", "expected_response": "Your balance is $50."}], "assertions": ["Response includes a dollar amount"], } ] } }, ) print(f"Dataset ID: {ds['datasetId']}, Status: {ds['status']}") # Create with S3 source ds = client.create_dataset_and_wait( datasetName="my_s3_dataset", schemaType="AGENTCORE_EVALUATION_PREDEFINED_V1", source={"s3Source": {"s3Uri": "s3://my-bucket/scenarios.jsonl"}}, )
    注意

    對於 S3 擷取,JSONL 檔案中的每一行都必須包含 exampleId 欄位。必須使用發起人的登入資料來存取 S3 儲存貯體。

AWS SDK
  1. import boto3 import time client = boto3.client('bedrock-agentcore-control') response = client.create_dataset( datasetName='customer_support_scenarios', schemaType='AGENTCORE_EVALUATION_PREDEFINED_V1', source={ 'inlineExamples': { 'examples': [ { 'scenario_id': 'TC-01', 'turns': [{'input': 'What is my balance?', 'expected_response': 'Your balance is $50.'}], 'assertions': ['Response includes a dollar amount'], } ] } } ) dataset_id = response['datasetId'] # Create with S3 source response = client.create_dataset( datasetName='my_s3_dataset', schemaType='AGENTCORE_EVALUATION_PREDEFINED_V1', source={ 's3Source': {'s3Uri': 's3://my-bucket/scenarios.jsonl'} } ) # Poll until ACTIVE while True: ds = client.get_dataset(datasetId=dataset_id) if ds['status'] in ('ACTIVE', 'CREATE_FAILED'): break time.sleep(2)
AWS CLI
  1. # Create with inline examples aws bedrock-agentcore-control create-dataset \ --dataset-name "customer_support_scenarios" \ --schema-type AGENTCORE_EVALUATION_PREDEFINED_V1 \ --source '{"inlineExamples": {"examples": [{"scenario_id": "TC-01", "turns": [{"input": "What is my balance?", "expected_response": "Your balance is $50."}], "assertions": ["Response includes a dollar amount"]}]}}' # Create with S3 source aws bedrock-agentcore-control create-dataset \ --dataset-name "my_s3_dataset" \ --schema-type AGENTCORE_EVALUATION_PREDEFINED_V1 \ --source '{"s3Source": {"s3Uri": "s3://my-bucket/scenarios.jsonl"}}' # Poll until ACTIVE aws bedrock-agentcore-control get-dataset \ --dataset-id my-dataset-id

取得資料集

GetDataset API 會擷取資料集內容的資料集中繼資料、狀態、範例計數和預先簽章的下載 URL。根據預設, 會讀取 草稿;datasetVersion為已發佈的版本指定 。

downloadUrl 是完整dataset.jsonl檔案的預先簽章 S3 URL。您可以使用不含身分驗證標頭的純 HTTP GET 請求進行下載。

下列範例示範如何取得資料集:

範例
AgentCore CLI
  1. # Show dataset deployment status and metadata agentcore status --type dataset # Download dataset content to your local JSONL file (default: Draft) agentcore dataset download --name my_eval_dataset # Download a specific published version agentcore dataset download --name my_eval_dataset --version 1
AgentCore SDK
  1. from bedrock_agentcore.evaluation import DatasetClient client = DatasetClient(region_name="us-west-2") # Get dataset (default: Draft) ds = client.get_dataset(datasetId="my-dataset-id") print(f"Status: {ds['status']}, Examples: {ds['exampleCount']}") print(f"Download URL: {ds['downloadUrl']}") # Get a specific published version ds_v1 = client.get_dataset(datasetId="my-dataset-id", datasetVersion="1")
AWS SDK
  1. import boto3 client = boto3.client('bedrock-agentcore-control') response = client.get_dataset(datasetId='my-dataset-id') print(f"Status: {response['status']}, Examples: {response['exampleCount']}") # Download the dataset content via presigned URL if 'downloadUrl' in response: import requests data = requests.get(response['downloadUrl']) print(data.text) # Get a specific published version response = client.get_dataset(datasetId='my-dataset-id', datasetVersion='1')
AWS CLI
  1. # Get dataset (default: Draft) aws bedrock-agentcore-control get-dataset \ --dataset-id my-dataset-id # Get a specific published version aws bedrock-agentcore-control get-dataset \ --dataset-id my-dataset-id \ --dataset-version 1

列出資料集

ListDatasets API 會傳回您帳戶和區域中資料集的分頁清單。

下列範例示範如何列出資料集:

範例
AgentCore CLI
  1. agentcore status --type dataset
AgentCore SDK
  1. from bedrock_agentcore.evaluation import DatasetClient client = DatasetClient(region_name="us-west-2") response = client.list_datasets() for dataset in response["datasets"]: print(f" {dataset['datasetName']} ({dataset['status']})")
AWS SDK
  1. import boto3 client = boto3.client('bedrock-agentcore-control') response = client.list_datasets() for dataset in response['datasets']: print(f" {dataset['datasetName']} ({dataset['status']})")
AWS CLI
  1. aws bedrock-agentcore-control list-datasets

更新資料集

UpdateDataset API 會更新資料集中繼資料。這是同步操作 (HTTP 200)。只有 descriptiontags 可以更新。schemaType建立後 datasetName、 和 kmsKeyArn是不可變的。

資料集必須處於 ACTIVEUPDATE_FAILEDCREATE_FAILED 狀態。

下列範例示範如何更新資料集中繼資料:

範例
AgentCore CLI
  1. 若要使用 AgentCore CLI 更新資料集,請直接編輯agentcore.json檔案中的資料集組態,然後重新部署:

    agentcore deploy

    開啟 agentcore.json,在datasets陣列中尋找資料集,修改其 description,然後執行 agentcore deploy。變更會在部署後生效。

    注意

    從 AgentCore 專案目錄 (使用 建立) 內執行此操作agentcore create

AgentCore SDK
  1. from bedrock_agentcore.evaluation import DatasetClient client = DatasetClient(region_name="us-west-2") client.update_dataset(datasetId="my-dataset-id", description="Updated description")
AWS SDK
  1. import boto3 client = boto3.client('bedrock-agentcore-control') client.update_dataset(datasetId='my-dataset-id', description='Updated description')
AWS CLI
  1. aws bedrock-agentcore-control update-dataset \ --dataset-id my-dataset-id \ --description "Updated description"

刪除資料集

DeleteDataset API 會刪除資料集。這是非同步操作 (HTTP 202)。

  • 完全刪除 (省略 datasetVersion):刪除所有版本、草稿和資料集記錄。

  • 版本特定刪除 (指定datasetVersion為整數):僅刪除該已發佈的版本。

資料集必須處於 ACTIVEUPDATE_FAILEDCREATE_FAILEDDELETE_FAILED 狀態。

注意

僅接受整數版本號碼進行版本特定刪除。

下列範例示範如何刪除資料集:

範例
AgentCore CLI
  1. # Delete a specific published version agentcore dataset remove-version 1 --name my_eval_dataset # Delete entire dataset agentcore remove dataset --name my_eval_dataset agentcore deploy
AgentCore SDK
  1. from bedrock_agentcore.evaluation import DatasetClient client = DatasetClient(region_name="us-west-2") # Delete a specific published version client.delete_dataset_and_wait(datasetId="my-dataset-id", datasetVersion="1") # Delete entire dataset (polls until complete) client.delete_dataset_and_wait(datasetId="my-dataset-id")
AWS SDK
  1. import boto3 client = boto3.client('bedrock-agentcore-control') # Delete a specific published version client.delete_dataset(datasetId='my-dataset-id', datasetVersion='1') # Delete entire dataset client.delete_dataset(datasetId='my-dataset-id')
AWS CLI
  1. # Delete a specific published version aws bedrock-agentcore-control delete-dataset \ --dataset-id my-dataset-id \ --dataset-version 1 # Delete entire dataset aws bedrock-agentcore-control delete-dataset \ --dataset-id my-dataset-id