View a markdown version of this page

開始批次評估 - Amazon Bedrock AgentCore

開始批次評估

啟動批次評估,以針對多個代理程式工作階段執行評估程式。此服務會從 CloudWatch Logs 探索工作階段,針對每個工作階段執行每個評估器,並產生彙總結果。

程式碼範例

範例
AgentCore CLI

當您使用 時,CLI 會從專案組態中解析serviceNameslogGroupNames自動執行--runtime

agentcore run batch-evaluation \ --runtime MyAgent \ --evaluator Builtin.GoalSuccessRate Builtin.Helpfulness Builtin.Faithfulness

使用選用旗標:

# Custom name and lookback window agentcore run batch-evaluation \ --runtime MyAgent \ --evaluator Builtin.GoalSuccessRate \ --name my_baseline_eval \ --lookback-days 1 # Specific sessions agentcore run batch-evaluation \ --runtime MyAgent \ --evaluator Builtin.GoalSuccessRate \ --session-ids session-abc123 session-def456 # With ground truth agentcore run batch-evaluation \ --runtime MyAgent \ --evaluator Builtin.GoalSuccessRate Builtin.Correctness \ --ground-truth ground-truth.json

根據預設, 命令會啟動任務並立即傳回。傳遞 --wait 以封鎖,直到任務達到結束狀態 (COMPLETEDFAILEDSTOPPED),之後 CLI 會顯示每個評估器的平均分數,並將結果儲存至 .cli/jobs/batch-eval-results/

agentcore run batch-evaluation 也支援下列旗標:

  • --wait — 封鎖,直到任務達到結束狀態。

  • --json — 發出機器可讀取的 JSON 輸出。

  • --kms-key <arn> — 使用客戶管理的 KMS 金鑰加密批次評估結果。

  • --dataset <name> / --dataset-version <version> — 在批次評估之前,使用資料集案例叫用代理程式 (省略本機檔案的 版本,或使用 N/DRAFT)。

  • --endpoint <name> — 以特定執行時間端點為目標 (例如 PROMPT_V1);預設為AGENTCORE_RUNTIME_ENDPOINT環境變數,然後是 DEFAULT

  • --evaluator-arn <arns…​> — 依 ARN 參考評估者,而非 -e

    大多數旗標具有短別名: -r (--runtime)、 -e (--evaluator)、 -n(--name)、 -d (--lookback-days)、 -s (--session-ids) 和 -g()--ground-truth

    若要在任務啟動後管理任務,請執行 agentcore stop batch-evaluation -i <id> 以停止執行中的任務agentcore archive batch-evaluation -i <id>,並封存任務記錄。

AWS SDK (boto3)
import boto3 import uuid import time import json client = boto3.client("bedrock-agentcore", region_name="us-west-2") # All sessions in the log group response = client.start_batch_evaluation( batchEvaluationName=f"baseline_eval_{uuid.uuid4().hex[:8]}", evaluators=[ {"evaluatorId": "Builtin.GoalSuccessRate"}, {"evaluatorId": "Builtin.Helpfulness"}, {"evaluatorId": "Builtin.Faithfulness"}, ], dataSourceConfig={ "cloudWatchLogs": { "serviceNames": ["MyAgent.DEFAULT"], "logGroupNames": ["/aws/bedrock-agentcore/runtimes/MyAgent-abc123-DEFAULT"], } }, clientToken=str(uuid.uuid4()), ) batch_eval_id = response["batchEvaluationId"] print(f"Started: {batch_eval_id}") # Poll until complete while True: result = client.get_batch_evaluation(batchEvaluationId=batch_eval_id) status = result["status"] print(f"Status: {status}") if status in ("COMPLETED", "COMPLETED_WITH_ERRORS", "FAILED", "STOPPED"): break time.sleep(30) print(json.dumps(result, indent=4, default=str))

使用工作階段 ID 篩選:

response = client.start_batch_evaluation( batchEvaluationName=f"targeted-eval-{uuid.uuid4().hex[:8]}", evaluators=[ {"evaluatorId": "Builtin.GoalSuccessRate"}, ], dataSourceConfig={ "cloudWatchLogs": { "serviceNames": ["MyAgent.DEFAULT"], "logGroupNames": ["/aws/bedrock-agentcore/runtimes/MyAgent-abc123-DEFAULT"], "filterConfig": { "sessionIds": ["session-001", "session-002", "session-003"] }, } }, clientToken=str(uuid.uuid4()), )

使用時間範圍篩選:

from datetime import datetime, timedelta, timezone now = datetime.now(timezone.utc) response = client.start_batch_evaluation( batchEvaluationName=f"weekly-eval-{uuid.uuid4().hex[:8]}", evaluators=[ {"evaluatorId": "Builtin.GoalSuccessRate"}, ], dataSourceConfig={ "cloudWatchLogs": { "serviceNames": ["MyAgent.DEFAULT"], "logGroupNames": ["/aws/bedrock-agentcore/runtimes/MyAgent-abc123-DEFAULT"], "filterConfig": { "timeRange": { "startTime": (now - timedelta(days=7)).isoformat(), "endTime": now.isoformat(), } }, } }, clientToken=str(uuid.uuid4()), )

請求參數

參數 Type 必要 描述

batchEvaluationName

String

批次評估任務的名稱。模式:以字母、英數字元和底線開頭,最多 48 個字元。

dataSourceConfig

物件

尋找客服人員工作階段的位置。為您的代理程式指定具有日誌群組和服務名稱的cloudWatchLogs來源。請參閱以下 工作階段來源

evaluators

清單

評估者清單。每個項目都有一個 evaluatorId 欄位 (例如 Builtin.GoalSuccessRate)。最多 10 個評估者。

evaluationMetadata

物件

包含 sessionMetadata,每個工作階段的 Ground Truth 和中繼資料清單。最多 500 個項目。

clientToken

String

等冪性字符。如果您使用相同的用戶端字符重試請求,服務會傳回現有的任務,而不是建立新的任務。

工作階段來源

dataSourceConfig 參數指定服務探索代理程式工作階段的 CloudWatch Logs 位置。

必要欄位

欄位 Type 說明

cloudWatchLogs.serviceNames

字串清單 (確切為 1)

在 CloudWatch 中識別代理程式追蹤的服務名稱。慣例:{RuntimeName}.DEFAULT

cloudWatchLogs.logGroupNames

字串清單 (1–5)

存放代理程式遙測的 CloudWatch 日誌群組名稱。慣例:/aws/bedrock-agentcore/runtimes/{agentId}-DEFAULT

選填欄位

欄位 Type 說明

cloudWatchLogs.filterConfig.sessionIds

字串清單

僅評估這些特定的工作階段 IDs。省略時,服務會探索日誌群組中的所有工作階段。

cloudWatchLogs.filterConfig.timeRange.startTime

ISO 8601 日期時間

篩選在此時間之後建立的工作階段。

cloudWatchLogs.filterConfig.timeRange.endTime

ISO 8601 日期時間

篩選在此時間之前建立的工作階段。

回應

欄位 Type Description

batchEvaluationId

String

批次評估的唯一識別符。

batchEvaluationArn

String

批次評估的 ARN。

batchEvaluationName

String

您指定的名稱。

status

String

初始狀態。其中之一:PENDINGIN_PROGRESS

evaluators

清單

使用的評估者。

createdAt

時間戳記

建立任務時。

outputConfig

物件

每個工作階段結果的 CloudWatch Logs 目的地。

錯誤

錯誤 HTTP 狀態 說明

ValidationException

400

無效的請求參數。檢查欄位限制條件和必要欄位。

AccessDeniedException

403

許可不足。驗證 IAM 政策。

ConflictException

409

具有相同用戶端字符的批次評估已存在,具有不同的參數。

ThrottlingException

429

超過請求率。以指數退避重試。

InternalServerException

500

服務端錯誤。重試 請求。