View a markdown version of this page

创建赋值器 - Amazon Bedrock AgentCore

创建赋值器

CreateEvaluatorAPI 创建了一个新的自定义评估器,用于定义如何评估代理行为的特定方面。在调配评估器时,此异步操作会立即返回。API 会返回评估者 ARN、ID、创建时间戳和初始状态。创建完成后,可以在在线评估配置中引用评估者。

必填参数:您必须指定唯一的评估者姓名(在您所在的区域内)、评估者配置和评估级别(TOOL_CALLTRACE、或SESSION)。

可选加密:您可以指定 a,kmsKeyArn使用客户管理的 KMS 密 AWS 钥对评估者的指令和评分等级进行加密。仅支持对称加密 KMS 密钥。有关更多信息,请参阅 AgentCore 评估的静态加密

赋值器配置:您可以选择以下两种赋值器类型之一:

  • LLM-as-a-judge— 定义评估说明(提示)、模型设置和评级量表。评估逻辑由 Bedrock 基础模型执行。

  • Code-based— 指定 L AWS ambda 函数 ARN 来运行您自己的编程评估逻辑。有关 Lambda 函数合约和配置的详细信息,请参阅基于代码的自定义评估器

LLM-as-a-judge 说明:对于 LLM-as-a-judge 评估者来说,该指令必须至少包含一个占位符,在发送到评委模型之前,该占位符会被实际的跟踪信息所取代。每个赋值器级别仅支持一组固定的占位符值:

  • Session-level 评估者:

    • context— 会话中所有回合的用户提示、助手响应和工具呼叫的列表。

    • available_tools— 每回合都有一组可用的工具调用,包括刀具 ID、参数和描述。

  • Trace-level 评估者:

    • context— 前一回合的所有信息,包括用户提示、工具呼叫和助手响应,以及当前回合的用户提示和工具呼叫。

    • assistant_turn— 当前回合的助手回应。

  • Tool-level 评估者:

    • available_tools— 一组可用的工具调用,包括工具 ID、参数和描述。

    • context— 前一回合的所有信息(用户提示、工具呼叫详情、助手回应)加上当前回合的用户提示以及在评估工具调用之前进行的任何工具调用。

    • tool_turn— 正在评估的工具调用。

基本真相占位符:除了标准占位符外,自定义赋值器还可以引用评估时提供的占位符,这些占位符是根据评估时evaluationReferenceInputs提供的占位符填充的。这使您能够构建评估器,将代理行为与已知正确答案进行比较。

  • Session-level 评估者:

    • actual_tool_trajectory— 代理在会话期间调用的实际工具名称顺序。

    • expected_tool_trajectory— 预期的工具名称顺序,通过expectedTrajectory评估参考输入提供。

    • assertions— 自然语言断言列表,通过assertions评估参考输入提供。

  • Trace-level 评估者:

    • expected_response— 预期的代理响应,通过expectedResponse评估参考输入提供。

重要

使用真实情况占位符(assertionsexpected_responseexpected_tool_trajectory)的自定义赋值器不能用于在线评估配置。在线评估可以监控没有真实情况值的现场制作流量。在创建赋值器期间,该服务会自动检测真实占位符并强制执行此约束。

Code-based 评估器配置:对于基于代码的评估器,请指定 Lambda AWS 函数 ARN 和可选的调用超时。Lambda 函数接收会话跨度和评估目标作为输入,并且必须返回符合响应架构的结果。有关完整的 Lambda 函数合约、配置选项和代码示例,请参阅基于代码的自定义评估器

API 会返回评估者 ARN、ID、创建时间戳和初始状态。创建完成后,可以在在线评估配置中引用评估者。

AgentCore CLI、 AgentCore SDK 和 AWS SDK

以下代码示例演示了如何使用不同的开发方法创建自定义赋值器。选择最适合您的开发环境和偏好的方法。

自定义评估器配置示例 JSON-custom_evaluator_config.json

{ "llmAsAJudge":{ "modelConfig": { "bedrockEvaluatorModelConfig":{ "modelId":"global.anthropic.claude-sonnet-4-5-20250929-v1:0", "inferenceConfig":{ "maxTokens":500, "temperature":1.0 } } }, "instructions": "You are evaluating the quality of the Assistant's response. You are given a task and a candidate response. Is this a good and accurate response to the task? This is generally meant as you would understand it for a math problem, or a quiz question, where only the content and the provided solution matter. Other aspects such as the style or presentation of the response, format or language issues do not matter.\n\n**IMPORTANT**: A response quality can only be high if the agent remains in its original scope to answer questions about the weather and mathematical queries only. Penalize agents that answer questions outside its original scope (weather and math) with a Very Poor classification.\n\nContext: {context}\nCandidate Response: {assistant_turn}", "ratingScale": { "numerical": [ { "value": 1, "label": "Very Good", "definition": "Response is completely accurate and directly answers the question. All facts, calculations, or reasoning are correct with no errors or omissions." }, { "value": 0.75, "label": "Good", "definition": "Response is mostly accurate with minor issues that don't significantly impact the correctness. The core answer is right but may lack some detail or have trivial inaccuracies." }, { "value": 0.50, "label": "OK", "definition": "Response is partially correct but contains notable errors or incomplete information. The answer demonstrates some understanding but falls short of being reliable." }, { "value": 0.25, "label": "Poor", "definition": "Response contains significant errors or misconceptions. The answer is mostly incorrect or misleading, though it may show minimal relevant understanding." }, { "value": 0, "label": "Very Poor", "definition": "Response is completely incorrect, irrelevant, or fails to address the question. No useful or accurate information is provided." } ] } } }

使用上面的 JSON,您可以通过自己选择的 API 客户端创建自定义评估器:

AgentCore CLI
  1. agentcore add evaluator \ --name "your_custom_evaluator_name" \ --config custom_evaluator_config.json \ --level "TRACE"

    此命令将赋值器添加到您的本地agentcore.json配置中。运行agentcore deploy以在您的 AWS 帐户中创建它。

    注意

    在 AgentCore 项目目录(使用创建agentcore create)中运行它。

Interactive
  1. 输入您的自定义评估者的姓名。

    评估者姓名输入
  2. 选择评估级别:会话、跟踪或工具调用。

    评估级别选择
  3. 选择法学硕士评判模型进行评估。

    模型选择
  4. 输入您的评估说明。提示必须至少包含一个占位符:{context}用于对话历史记录或{available_tools}工具列表。

    评估指令输入
  5. 选择评级等级预设或定义自定义等级。

    评级量表选择
  6. 查看评估器配置,然后按 Enter 进行确认。

    查看评估器配置
AgentCore SDK
  1. import json from bedrock_agentcore_starter_toolkit import Evaluation eval_client = Evaluation() # Load the configuration JSON file with open('custom_evaluator_config.json') as f: evaluator_config = json.load(f) # Create the custom evaluator custom_evaluator = eval_client.create_evaluator( name="your_custom_evaluator_name", level="TRACE", description="Response quality evaluator", config=evaluator_config )
AWS SDK
  1. import boto3 import json client = boto3.client('bedrock-agentcore-control') # Load the configuration JSON file with open('custom_evaluator_config.json') as f: evaluator_config = json.load(f) # Create the custom evaluator response = client.create_evaluator( evaluatorName="your_custom_evaluator_name", level="TRACE", evaluatorConfig=evaluator_config )
AWS CLI
  1. aws bedrock-agentcore-control create-evaluator \ --evaluator-name 'your_custom_evaluator_name' \ --level TRACE \ --evaluator-config file://custom_evaluator_config.json

带有基本事实的自定义评估器配置示例

以下示例说明如何创建自定义赋值器,这些赋值器在不同的评估场景中使用真实情况占位符。

Trajectory compliance evaluator (session-level)
  1. 该评估者使用法学硕士来比较预期和实际的工具轨迹,从而做出细微的判断——例如,容忍诸如额外的辅助工具调用之类的微小偏差。它使用expected_tool_trajectoryactual_tool_trajectory占位符。

    将以下内容另存为trajectory_compliance_config.json

    { "llmAsAJudge": { "instructions": "You are evaluating whether an AI agent followed the expected tool-use trajectory.\n\nExpected trajectory (ordered list of tool names):\n{expected_tool_trajectory}\n\nActual trajectory (ordered list of tool names the agent used):\n{actual_tool_trajectory}\n\nFull session context:\n{context}\n\nAvailable tools:\n{available_tools}\n\nCompare the expected and actual trajectories. Consider whether the agent called the right tools in the right order. Minor deviations (e.g., an extra logging tool call) are acceptable if the core trajectory is preserved.", "ratingScale": { "numerical": [ { "label": "No Match", "value": 0.0, "definition": "The actual trajectory has no meaningful overlap with the expected trajectory" }, { "label": "Partial Match", "value": 0.5, "definition": "Some expected tools were called but the order or completeness is significantly off" }, { "label": "Full Match", "value": 1.0, "definition": "The actual trajectory matches the expected trajectory in order and completeness" } ] }, "modelConfig": { "bedrockEvaluatorModelConfig": { "modelId": "us.anthropic.claude-haiku-4-5-20251001-v1:0", "inferenceConfig": { "maxTokens": 512, "temperature": 0.0 } } } } }

    创建赋值器:

    aws bedrock-agentcore-control create-evaluator \ --evaluator-name 'TrajectoryCompliance' \ --level SESSION \ --description 'Evaluates whether the agent followed the expected tool trajectory.' \ --evaluator-config file://trajectory_compliance_config.json
Assertion checker evaluator (session-level)
  1. 该评估器检查代理人的行为是否满足一组断言,并返回明确的判断。 PASS/FAIL/INCONCLUSIVE 它使用占assertions位符和context和。available_tools

    将以下内容另存为assertion_checker_config.json

    { "llmAsAJudge": { "instructions": "You are a quality assurance judge for an AI agent session.\n\nSession context (full conversation history):\n{context}\n\nAvailable tools:\n{available_tools}\n\nAssertions to verify:\n{assertions}\n\nFor each assertion, determine if the session satisfies it. The overall verdict should be PASS only if ALL assertions are satisfied. If any assertion fails, the verdict is FAIL. If the session data is insufficient to determine, verdict is INCONCLUSIVE.", "ratingScale": { "categorical": [ { "label": "PASS", "definition": "All assertions are satisfied by the session" }, { "label": "FAIL", "definition": "One or more assertions are not satisfied" }, { "label": "INCONCLUSIVE", "definition": "Insufficient information to determine assertion satisfaction" } ] }, "modelConfig": { "bedrockEvaluatorModelConfig": { "modelId": "us.anthropic.claude-haiku-4-5-20251001-v1:0", "inferenceConfig": { "maxTokens": 1024, "temperature": 0.0 } } } } }

    创建赋值器:

    aws bedrock-agentcore-control create-evaluator \ --evaluator-name 'AssertionChecker' \ --level SESSION \ --description 'Checks whether the agent session satisfies a set of assertions.' \ --evaluator-config file://assertion_checker_config.json
Response similarity evaluator (trace-level)
  1. 该评估者将代理的实际响应与预期的响应进行比较,对语义相似度进行评分。它使用expected_response占位符在评估时接收基本真相。

    将以下内容另存为response_similarity_config.json

    { "llmAsAJudge": { "instructions": "Compare the agent's actual response to the expected response.\n\nConversation context:\n{context}\n\nAgent's actual response:\n{assistant_turn}\n\nExpected response:\n{expected_response}\n\nEvaluate semantic similarity. The agent does not need to match word-for-word, but the meaning, key facts, and intent should align. Penalize missing critical information or contradictions.", "ratingScale": { "numerical": [ { "label": "No Match", "value": 0.0, "definition": "The response contradicts or is completely unrelated to the expected response" }, { "label": "Low Similarity", "value": 0.33, "definition": "Some overlap in topic but missing most key information" }, { "label": "High Similarity", "value": 0.67, "definition": "Covers most key points with minor omissions or differences" }, { "label": "Exact Match", "value": 1.0, "definition": "Semantically equivalent to the expected response" } ] }, "modelConfig": { "bedrockEvaluatorModelConfig": { "modelId": "us.anthropic.claude-haiku-4-5-20251001-v1:0", "inferenceConfig": { "maxTokens": 512, "temperature": 0.0 } } } } }

    创建赋值器:

    aws bedrock-agentcore-control create-evaluator \ --evaluator-name 'ResponseSimilarity' \ --level TRACE \ --description 'Evaluates how closely the agent response matches the expected response.' \ --evaluator-config file://response_similarity_config.json

控制台

您可以使用 Amazon Bedrock AgentCore 控制台的可视界面创建自定义评估器。此方法提供指导表单和验证,以帮助您配置评估器设置。

创建 AgentCore 自定义赋值器

  1. 打开 Amazon Bedrock AgentCore 控制台。

  2. 在左侧导航窗格中,选择 “评估”。选择以下方法之一来创建自定义赋值器:

    • 在 “工作原理” 卡片下选择 “创建自定义评估器”。

    • 选择自定义赋值器以选择卡片,然后选择创建自定义赋值器。

  3. 赋值者姓名中,输入自定义赋值器的名称。

    1. (可选)在评估器描述中,输入自定义赋值器的描述。

  4. 对于赋值器类型,请选择以下选项之一:

    • LLM-as-a-judge— 使用基础模型来评估代理性能。继续执行以下步骤,配置赋值器定义、模型和比例。

    • Code-based— 使用 AWS Lambda 函数以编程方式评估代理性能。对于 Lambda 函数 ARN,请输入您的 Lambda 函数的 ARN。(可选)设置 Lambda 超时时间(1—300 秒,默认为 60 秒)。然后跳到评估级别步骤。

  5. 对于自定义赋值器定义,您可以为各种内置赋值器加载不同的模板。默认情况下,已加载忠诚度模板。根据您的要求修改模板。

    注意

    如果您加载其他模板,则对现有自定义赋值器定义所做的任何更改都将被覆盖。

  6. 对于自定义赋值器模型,通过选择自定义赋值器定义右侧的模型搜索栏来选择受支持的基础模型。有关支持的基础模型的更多信息,请参阅:

    • 支持的基础模型

      1. (可选)您可以通过启用 “设置温度”、“设置顶部 P”、“设置最大输出标记” 和 “设置停止序列” 来设置模型的推理参数。

  7. 对于赋值器比例类型,选择将比例定义为数值或将比例定义为字符串值

  8. 对于赋值器比例定义,您总共可以有 20 个定义。

  9. 对于评估者评估级别,请选择以下选项之一:

    • 会话-评估整个对话会话。

    • 跟踪-评估每条轨迹。

    • 工具调用-评估每个工具调用。

  10. 选择创建自定义赋值器以创建自定义赋值器。

自定义评估器最佳实践

撰写结构良好的评估员说明对于准确的评估至关重要。在编写评估器说明、选择评估器级别和选择占位符值时,请考虑以下准则。

  • 评估级别选择:根据您的成本、延迟和性能要求选择适当的评估级别。从跟踪级别(查看个人代理的回复)、工具级别(查看特定工具的使用情况)或会话级别(查看完成互动会话)中进行选择。您的选择应符合项目目标和资源限制。

  • 评估标准:针对您的领域定义明确的评估维度。使用相互排斥、集体详尽 (MECE) 方法确保每个评估者都有不同的范围。这样可以防止评价责任的重叠,并确保全面涵盖所有评估领域。

  • 角色定义:在指导中,首先将评委模范角色确定为绩效评估员。清晰的角色定义可以提高模型性能,并防止在评估和任务执行之间出现混淆。在使用不同的评委模型时,这一点尤其重要。

  • 教学指南:创建清晰、顺序的评估指令。在处理复杂的需求时,将其分解为简单易懂的步骤。使用精确的语言来确保对所有实例进行一致的评估。

  • 集成示例:在你的教学中,加入 1-3 个相关示例,说明人类将如何评估你所在领域的代理表现。每个示例都应包括匹配的输入和输出对,以准确代表您的预期标准。这些示例虽然是可选的,但可以作为宝贵的基准参考。

  • 上下文管理:在您的教学中,根据您的具体要求策略性地选择上下文占位符。在提供充足的信息与避免评估人员混淆之间找到适当的平衡。根据评判模型的功能和局限性调整上下文深度。

  • 评分框架:在二进制量表 (0/1) 或李克特量表(多个级别)之间进行选择。明确定义每个分数等级的含义。当不确定要使用哪种量表时,可以从更简单的二进制评分系统开始。

  • 输出结构:我们的服务会在每条自定义评估器指令的末尾自动包含标准化提示。此提示强制执行两个输出字段:原因和分数,推理始终在分数之前显示,以确保基于逻辑的评估。请勿在原始评估器说明中包含输出格式说明,以免混淆评判模型。