View a markdown version of this page

开始批量评估 - Amazon Bedrock AgentCore

开始批量评估

启动批量评估,对多个代理会话运行评估器。该服务从 CloudWatch 日志中发现会话,针对每个会话运行每个评估器,并生成汇总结果。

代码示例

AgentCore CLI

当您使用--runtime以下内容时,CLI logGroupNames 会自动解析serviceNames并从项目配置中自动解析:

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到区块,直到作业达到终端状态(COMPLETEDFAILED、或STOPPED),之后 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

字符串

批量评估作业的名称。模式:以字母、字母数字和下划线开头,最多 48 个字符。

dataSourceConfig

对象

在哪里可以找到代理会话。为您的代理指定包含日志组和服务名称的cloudWatchLogs来源。请参阅下面的会话来源

evaluators

列表

评估者名单。每个条目都有一个evaluatorId字段(例如,Builtin.GoalSuccessRate)。最多 10 个赋值器。

evaluationMetadata

对象

包含sessionMetadata每个会话的基本事实和元数据的列表。最多 500 个参赛作品。

clientToken

字符串

等能令牌。如果您使用相同的客户端令牌重试请求,服务将返回现有任务,而不是创建新任务。

会话来源

dataSourceConfig参数指定服务发现代理会话的 CloudWatch 日志位置。

必填字段

字段 Type 说明

cloudWatchLogs.serviceNames

字符串列表(正好是 1)

用于识别您的代理跟踪的服务名称 CloudWatch。惯例:{RuntimeName}.DEFAULT

cloudWatchLogs.logGroupNames

字符串列表 (1—5)

CloudWatch 存储代理遥测数据的日志组名称。惯例:/aws/bedrock-agentcore/runtimes/{agentId}-DEFAULT

可选字段

字段 Type 说明

cloudWatchLogs.filterConfig.sessionIds

字符串列表

仅评估这些特定的会话 ID。省略时,服务会发现日志组中的所有会话。

cloudWatchLogs.filterConfig.timeRange.startTime

ISO 8601 日期时间

筛选在此时间之后创建的会话。

cloudWatchLogs.filterConfig.timeRange.endTime

ISO 8601 日期时间

筛选在此时间之前创建的会话。

响应

字段 Type 说明

batchEvaluationId

字符串

批次评估的唯一标识符。

batchEvaluationArn

字符串

批量评估的 ARN。

batchEvaluationName

字符串

您指定的名称。

status

字符串

初始状态。其中之一:PENDINGIN_PROGRESS

evaluators

列表

使用的评估者。

createdAt

Timestamp

创建作业的时间。

outputConfig

对象

CloudWatch 记录每个会话结果的目标。

错误

错误 HTTP 状态 说明

ValidationException

400

请求参数无效。检查字段限制和必填字段。

AccessDeniedException

403

权限不足。验证 IAM 策略。

ConflictException

409

已经存在使用相同客户端令牌的批量评估,参数不同。

ThrottlingException

429

超出请求速率。使用指数回退进行重试。

InternalServerException

500

Service-side 错误。重试请求。