View a markdown version of this page

为自定义指标创建提示 - Amazon Bedrock

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

为自定义指标创建提示

借助 RAG 评测作业,您可以创建自己的指标,以更好地描述特定业务案例的模型性能。您最多可以为单个评测作业定义 10 个自定义指标。

要创建您自己的自定义评测指标,需要提供以下信息:

  • 包含供评判模型使用的详细指令的提示

  • 要用于自定义指标的评测器模型

您还可以指定评分系统(输出架构),评测器模型可以使用该系统对 RAG 系统的输出进行评分。

以下各部分介绍在使用自定义指标(包括最佳实践)时如何为评测器模型创建提示。这些章节还定义了使用 JSON 文件创建指标时要使用的架构。

要了解如何使用提示创建包含自定义指标的评测作业,请参阅使用自定义指标创建仅限检索 RAG 评测作业使用自定义指标创建“检索并生成”RAG 评测作业

提示构造和最佳实践

为自定义指标创建提示时,应使用以下元素来构建提示:

  1. 角色定义(可选)– 指示评测器模型采用特定的身份或角色

  2. 任务描述 – 提供有关评测任务的详细说明

  3. 标准和规定(可选)– 为评测提供详细的评分准则和规定

  4. 输入变量 – 定义要评测的变量,例如提示和响应

请按列表中给出的顺序将这些元素包含在提示中。以下部分详细介绍了其中的每个元素。

角色定义

可以选择是否提供角色定义,但提供角色定义有助于制定评测框架。例如,如果要创建一个指标来评测 RAG 系统最终输出的散文风格,可以考虑使用像“写作导师”这样的角色。“事实准确性检查员”或“主题专家”之类的角色可能也适合,具体取决于您的评测目标。

如果您选择包含角色定义,则它应该是提示的第一部分。以下内容显示了角色定义示例。

例角色定义
You are a professional editor who is familiar with the requirements of commonly-used style manuals.

任务定义

任务定义是提示中最重要的部分,它定义了您希望评测器模型执行的任务。任务定义应提供有关评测任务的详细说明(建议至少使用 15 个字),并具体说明要重点关注哪些方面以及如何构建评测。

如果您选择包含任务定义,则应直接加在角色定义之后。

有关更多如何构建 LLM 提示的一般指导,请参阅设计提示

以下示例显示了一个指标的任务定义,该指标侧重于 RAG 系统响应是否符合特定的样式指南。

例任务定义
You are provided a prompt and a response from a RAG system. The prompt asks the RAG system to follow the Chicago Manual of Style when generating its responses. Your task is to assess how closely the text in the response adheres to the style guide. Focus in particular on grammar, prose style, and citation requirements.

标准和规定

此部分是可选的,可用于为指标定义多个评测规定或提供详细的评分准则。此部分应在任务定义和输入变量之间添加。以下示例显示了在所提供的任务定义示例中,给出的任务的评测规定和评分准则。您可以在提示的这一部分中包含其中一种或两种类型的信息。

例评测规定
When evaluating the response quality, consider the following: - Grammar: Does the grammar in the response follow the requirements of the style guide - Style consistency: Does the response maintain consistent capitalization, punctuation, and paragraph formatting - Citations: Does the response use the correct citation style for in-text citations and endnotes
例评分准则
Please rate the quality of the response on the following scale: - Poor: Response includes errors in citation, grammar, or usage - Acceptable: Response includes only minor formatting errors - Good: Response meets all requirements of the style guide

创建使用 LLM as a judge 的评测作业时,无论是在 AWS 管理控制台中,还是使用 AWS CLI 或其中一个 AWS SDK,都可以为 Amazon Bedrock 指定输出架构,以对来自生成器模型的响应进行评分。确保您为输出架构指定的任何定义都与您在提示中定义的评分准则一致。例如,在前面的示例中,您可以将输出架构的定义指定为“差”、“可接受”和“好”。有关定义输出架构的更多指导,请参阅指定输出架构(评分量表)

输入变量

提示的最后一部分指定了评测器模型执行评测所需的变量。请注意,您必须最后指定输入变量;如果您在输入变量后面的提示中提供进一步的说明,评测器模型可能无法正确评测您的指标。

您需要指定的最少输入变量取决于您正在创建的评测作业的类型;对于“仅检索”类型的作业,您必须提供 {{context}};对于“检索并生成”类型的作业,则必须提供 {{prediction}}。在这两种情况下,我们都建议您同时添加为生成器模型提供的提示({{prompt}})。下表定义了可以在提示中使用的变量,以及它们与提示数据集内的属性的对应关系。

输入变量 定义 必需(仅检索) 必需(检索并生成) 提示数据集属性
{{prompt}} 向 RAG 系统提供的提示 "prompt"
{{prediction}} 响应生成器模型提供的响应(仅适用于“检索并生成”类型的作业) "output" -> "text"(仅适用于那些由您提供自己的推理数据的作业)
{{ground_truth}} 提示的参考答案 "referenceResponses" -> "content" -> "text"
{{context}} 生成响应时收到的 RAG 段落 "retrievalResults" -> "content" -> "text"
{{reference_contexts}} 期望检索的 Ground Truth 段落 referenceContexts -> "content" -> "text"

以下示例说明了如何在提示中指定输入变量。

例输入变量定义
Here is the actual task: Prompt: {{prompt}} Response: {{prediction}}

为了完整起见,以下示例提供了此使用案例的完整提示。完整提示最多可包含 5000 个字符。

例自定义指标提示
You are a professional editor who is familiar with the requirements of commonly-used style manuals. You are provided a prompt and a response from a RAG system. The prompt asks the RAG system to follow the Chicago manual of style when generating its responses. Your task is to assess how closely the text in the response adheres to the style guide. Focus in particular on grammar, prose style, and citation requirements. When evaluating the response quality, consider the following: - Grammar: Does the grammar in the response follow the requirements of the style guide - Style consistency: Does the response maintain consistent capitalization, punctuation, and paragraph formatting - Citations: Does the response use the correct citation style for in-text citations and endnotes Please rate the quality of the response on the following scale: - Poor: Response includes errors in citation, grammar, or usage - Acceptable: Response includes only minor formatting errors - Good: Response meets all requirements of the style guide Here is the actual task: Prompt: {{prompt}} Response: {{prediction}}

指定输出架构(评分量表)

除了提示中的评测准则外,当您使用自定义指标创建评测作业时,还可以通过指定输出架构来定义该指标的评分量表。该架构由量表值及其相应的定义组成。量表值可以是数值或字符串,但不能是两者的组合。

我们强烈建议您定义评分量表。如果您不这样做,Amazon Bedrock 可能无法正确解析评测器模型的输出,以便在控制台中以图形方式显示结果,也无法计算出平均分的计算结果。您可在以下两种情况下定义评分量表:使用控制台创建指标时;如果是使用 AWS SDK 或 AWS CLI 创建指标,则使用 ratingScale 属性来定义评分量表。

当您定义评分量表时,Amazon Bedrock 会将结构化输出说明添加到您的评测器模型提示中。由于不同评测器模型的结构化输出格式不同,因此请务必单独定义评分量表,不要将其作为主提示的一部分。如果您在控制台中创建指标并定义评分量表,则可在预览文本区域看到最终提示,其中包含结构化输出说明。

为每个量表值提供定义时,请确保在输出架构定义中使用的定义与在提示中定义评分准则的方式完全匹配。这些定义最多应包含五个单词,并且限制在 100 个字符以内。避免在定义中使用冠词(“a”和“the”)。您可以使用提示中的解释性文字,为评测器模型提供有关如何对响应进行评级的更详细的定义。

下表说明了如何在提示中描述评分准则,以及如何在输出架构中定义相应的量表定义。

提示评分准则 架构量表值 架构量表定义
- Poor: The response fails to meet the basic requirements of the prompt and contains significant errors and omissions 0 Poor
- Good: The response adequately meets the basic requirements of the prompt but may contain minor errors or omissions 1 Good
- Excellent: The response fully meets the prompt with comprehensive, accurate, and valuable information 2 Excellent

此示例使用整数值作为量表,您也可以使用字符串。

要了解如何在创建评测作业时指定架构,请参阅使用自定义指标创建模型评测作业

创建 JSON 文件以创建自定义指标

当您使用控制台自定义指标创建评测作业时,您可以选择从本地计算机上传定义指标的 JSON 文件。如果您选择使用 JSON 文件通过控制台创建自定义指标,则无法使用控制台用户界面的文本区域来定义评分量表,因此必须将其包含在 JSON 定义中。我们建议您在创建自定义指标时始终定义评分量表。

以下示例显示了用于创建自定义指标的 JSON 文件的格式,其中的指标可能采用数字或字符串评分量表,也可能不采用评分量表。在每种情况下,都要将完整的提示作为字符串添加到 "instructions" 属性中。

当您创建具有一个或多个自定义指标的评测作业时,Amazon Bedrock 会将您的指标定义以 JSON 文件的形式存储在您指定的输出 S3 存储桶中。您可以通过导航到 s3://S3-output-bucket-name/job-name/job-uuid/custom_metrics 来访问这些文件。

Definition with numerical scale
{ "customMetricDefinition": { "metricName": "my_custom_metric", "instructions": "Your complete custom metric prompt including at least one {{input variable}}", "ratingScale": [ { "definition": "first rating definition", "value": { "floatValue": 3 } }, { "definition": "second rating definition", "value": { "floatValue": 2 } }, { "definition": "third rating definition", "value": { "floatValue": 1 } } ] } }
Definition with string scale
{ "customMetricDefinition": { "metricName": "my_custom_metric", "instructions": "Your complete custom metric prompt including at least one {{input variable}}", "ratingScale": [ { "definition": "first rating definition", "value": { "stringValue": "first value" } }, { "definition": "second rating definition", "value": { "stringValue": "second value" } }, { "definition": "third rating definition", "value": { "stringValue": "third value" } } ] } }
Definition with no scale
{ "customMetricDefinition": { "metricName": "my_custom_metric", "instructions": "Your complete custom metric prompt including at least one {{input variable}}" } }