创建评估数据集后,下一步是定义用于衡量模型性能的指标。生成式 AI 模型可以创建应用来执行各种任务,而 Gen AI Evaluation Service 使用测试驱动型框架,可将评估从主观评分转换为切实可行的客观结果。
以下是与评估指标相关的核心概念:
评分准则:有关如何对 LLM 模型或应用的回答进行评分的标准。
指标:根据评分准则衡量模型输出的得分。
Gen AI Evaluation Service 提供以下类别的指标:
基于评分准则的指标:将 LLM 纳入评估工作流。
自适应评分准则(推荐):系统会针对每个提示动态生成评分准则。系统会使用特定于提示的精细且可解释的通过或未通过反馈,对回答进行评估。
静态评分准则:明确定义评分准则,并对所有提示应用相同的评分准则。系统会使用同一组基于数值评分的评估器来评估回答。每个提示会获得单一的数值得分(例如 1-5)。这适用于需要针对非常具体的维度进行评估,或者需要对所有提示使用完全相同的评分准则的情况。
基于计算的指标:使用确定性算法评估回答(通常使用标准答案)。每个提示会获得一个数值得分(例如 0.0-1.0)。这适用于有标准答案可用且可以通过确定性方法进行匹配的情况。
自定义函数指标:通过 Python 函数定义您自己的指标。
基于评分准则的指标
基于评分准则的指标将大语言模型纳入工作流中,以评估模型回答的质量。基于评分准则的评估适用于各种任务,尤其是写作质量、安全性和指令遵从(这些方面通常难以使用确定性算法进行评估)。
自适应评分准则
自适应评分准则的功能类似于模型的单元测试。自适应评分准则会针对数据集中的每个提示动态生成一组独有的通过或未通过测试。评分准则可确保评估与所请求的任务相关,旨在提供客观、可解释且一致的结果。
以下示例展示了如何为一组提示生成自适应评分准则:
提示 | 自适应评分准则 |
---|---|
“Summarize the following article about the benefits of solar power in under 100 words…” |
|
“Write a short, friendly email inviting employees to the annual company picnic. Mention the date is September 15th and that vegetarian options will be available…” |
|
您可以通过 SDK 访问自适应评分准则。我们建议首先将 GENERAL_QUALITY
作为默认值。
常规质量指标
GENERAL_QUALITY
会根据输入提示生成一组涵盖各种任务(例如指令遵从、格式设置、语气、风格)的评分准则。您可以在以下代码行中将评分准则生成与验证相结合:
from vertexai import types
eval_result = client.evals.evaluate(
dataset=eval_dataset,
metrics=[
types.RubricMetric.GENERAL_QUALITY,
],
)
您可以先单独生成评分准则(以便在模型和代理之间查看或重复使用),然后再使用它们评估模型回答:
from vertexai import types
# Use GENERAL_QUALITY recipe to generate rubrics, and store them
# as a rubric group named "general_quality_rubrics".
data_with_rubrics = client.evals.generate_rubrics(
src=eval_dataset_df,
rubric_group_name="general_quality_rubrics",
predefined_spec_name=types.RubricMetric.GENERAL_QUALITY,
)
# Specify the group of rubrics to use for the evaluation.
eval_result = client.evals.evaluate(
dataset=data_with_rubrics,
metrics=[types.RubricMetric.GENERAL_QUALITY(
rubric_group_name="general_quality_rubrics",
)],
)
您还可以使用自然语言 guidelines
引导 GENERAL_QUALITY
,使系统重点根据对您而言最重要的标准来生成评分准则。然后,Gen AI Evaluation Service 会生成涵盖其默认任务和您指定的准则的评分准则。
from vertexai import types
eval_result = client.evals.evaluate(
dataset=eval_dataset,
metrics=[
types.RubricMetric.GENERAL_QUALITY(
metric_spec_parameters={
"guidelines": "The response must maintain a professional tone and must not provide financial advice."
}
)
],
)
有针对性的质量指标
如果您需要评估模型质量的更具针对性的方面,可以使用会生成侧重于特定领域的评分准则的指标。例如:
from vertexai import types
eval_result = client.evals.evaluate(
dataset=eval_dataset,
metrics=[
types.RubricMetric.TEXT_QUALITY,
types.RubricMetric.INSTRUCTION_FOLLOWING,
],
)
Gen AI Evaluation Service 提供以下类型的自适应评分准则:
INSTRUCTION_FOLLOWING
:衡量回答在多大程度上遵循了提示中的特定限制条件和指令。TEXT_QUALITY
:专门侧重于回答的语言质量,评估流畅度、连贯性和语法。
多轮对话
multi_turn_general_quality
:评估多轮对话中的整体对话质量。multi_turn_text_quality
:评估多轮对话中回答的文本质量。
智能体评估
final_response_reference_free
:评估代理最终答案的质量,而无需参考答案。
如需详细了解有针对性的自适应评分准则,请参阅自适应评分准则详细信息。
静态评分准则
静态评分准则会为数据集中的每个样本应用一组固定的评分准则。如果您需要针对所有提示,以一致的基准衡量性能,那么这种得分驱动型方法会很有用。
例如,以下静态评分准则以 1-5 分制对文本质量进行评分:
5: (Very good). Exceptionally clear, coherent, fluent, and concise. Fully adheres to instructions and stays grounded.
4: (Good). Well-written, coherent, and fluent. Mostly adheres to instructions and stays grounded. Minor room for improvement.
3: (Ok). Adequate writing with decent coherence and fluency. Partially fulfills instructions and may contain minor ungrounded information. Could be more concise.
2: (Bad). Poorly written, lacking coherence and fluency. Struggles to adhere to instructions and may include ungrounded information. Issues with conciseness.
1: (Very bad). Very poorly written, incoherent, and non-fluent. Fails to follow instructions and contains substantial ungrounded information. Severely lacking in conciseness.
Gen AI Evaluation Service 提供以下静态评分准则指标:
GROUNDING
:根据提供的源文本(标准答案)检查真实性和一致性。此指标对于 RAG 系统至关重要。SAFETY
:评估模型回答是否违反安全政策,例如是否包含仇恨言论或危险内容。
您还可以使用指标提示模板,例如 FLUENCY
。
from vertexai import types
eval_result = client.evals.evaluate(
dataset=eval_dataset,
metrics=[
types.RubricMetric.SAFETY,
types.RubricMetric.GROUNDING,
types.RubricMetric.FLUENCY,
],
)
自定义静态评分准则
对于高度专业化的需求,您可以创建自己的静态评分准则。此方法可提供最大的控制权,但需要您仔细设计评估提示,以确保获得一致且可靠的结果。我们建议您先使用包含 GENERAL_QUALITY
的准则,然后再自定义静态评分准则。
# Define a custom metric to evaluate language simplicity
simplicity_metric = types.LLMMetric(
name='language_simplicity',
prompt_template=types.MetricPromptBuilder(
instruction="Evaluate the story's simplicity for a 5-year-old.",
criteria={
"Vocabulary": "Uses simple words.",
"Sentences": "Uses short sentences.",
},
rating_scores={
"5": "Excellent: Very simple, ideal for a 5-year-old.",
"4": "Good: Mostly simple, with minor complex parts.",
"3": "Fair: Mix of simple and complex; may be challenging for a 5-year-old.",
"2": "Poor: Largely too complex, with difficult words/sentences.",
"1": "Very Poor: Very complex, unsuitable for a 5-year-old."
}
)
)
eval_result = client.evals.evaluate(
dataset=eval_dataset,
metrics=[
simplicity_metric
],
)
基于计算的指标
基于计算的指标使用确定性算法,通过将模型回答与参考答案进行比较来为模型回答评分。它们需要数据集中的标准答案,非常适合明确定义了“正确”答案的任务。
以召回率为导向的摘要评估研究(rouge_l、rouge_1):衡量模型回答与参考文本之间 n 元语法(连续的字词序列)的重叠程度。它通常用于评估文本摘要。
双语替换评测 (bleu):通过统计匹配的 n 元语法来衡量回答与高质量参考文本的相似程度。它是衡量翻译质量的标准指标,但也可用于其他文本生成任务。
完全匹配 (exact_match):衡量与参考答案完全相同的回答所占的百分比。这对于基于事实的问答或只有一种正确回答的任务非常有用。
from vertexai import types
eval_result = client.evals.evaluate(
dataset=eval_dataset,
metrics=[
types.Metric(name='bleu'),
types.Metric(name='rouge_l'),
types.Metric(name='exact_match')
],
)
自定义函数指标
您还可以通过向 custom_function
参数传递自定义 Python 函数来实现自定义评估逻辑。Gen AI Evaluation Service 会针对数据集的每一行执行此函数。
# Define a custom function to check for the presence of a keyword
def contains_keyword(instance: dict) -> dict:
keyword = "magic"
response_text = instance.get("response", "")
score = 1.0 if keyword in response_text.lower() else 0.0
return {"score": score}
keyword_metric = types.Metric(
name="keyword_check",
custom_function=contains_keyword
)
eval_result = client.evals.evaluate(
dataset=eval_dataset,
metrics=[keyword_metric]
)