配置评判模型

借助基于模型的指标,您可以根据自己的条件和使用情形自定义评估指标的生成方式。本指南介绍了如何配置评判模型,并涵盖了以下主题:

  • 选择配置选项:比较可用于自定义评判模型的不同方法。
  • 系统指令:向评判模型提供高层级的持久性指令,以影响其行为。
  • 回答切换:通过交换基准模型和候选模型回答的位置来减少潜在的位置偏差。
  • 多次采样:通过多次调用评判模型来处理同一输入并汇总结果,从而提高一致性。
  • 调优后的评判模型:使用微调后的 LLM 作为评判模型,执行专门的评估任务。

如需了解基本的评估工作流,请参阅 Gen AI Evaluation Service 快速入门。高级评判模型自定义系列包含以下页面:

  1. 评估评判模型
  2. 为评判模型自定义撰写提示
  3. 配置评判模型(当前页面)

选择配置选项

您可以通过多种方式配置评判模型,以提高质量。下表对每种方法进行了概要比较。

选项 说明 使用场景
系统指令 向评判模型提供高阶的持久性指令,这些指令会影响模型对所有后续评估提示的行为。 当您需要在整个评估任务中为评判模型定义一致的角色、人设或输出格式时。
响应翻转 在一半的评估调用中,交换基准模型和候选模型回答的位置。 为了减少成对评估中可能出现的位置偏差,即评判模型可能偏好第一个或第二个位置的回答。
多重抽样 针对同一输入多次调用评判模型,并汇总结果。 通过减轻评判模型回答中的随机性影响,提高评估得分的一致性和可靠性。
调优后的评判模型 使用经过微调的 LLM 作为评估的评判模型。 对于需要细致理解或特定领域知识的专业评估任务,而通用模型缺乏这些能力。

系统指令

Gemini 模型可以接收系统指令,这是一组会影响模型处理提示方式的指令。在初始化模型或使用模型生成内容时,您可以使用系统指令来指定产品级行为,例如角色、角色设定、上下文信息以及说明风格和语气。评判模型通常会认为系统指令比输入提示更重要。

如需查看支持系统指令的模型列表,请参阅支持的模型

以下示例使用 Vertex AI SDK 在 PointwiseMetric 的指标级层添加了 system_instruction

system_instruction = "You are an expert evaluator."
linguistic_acceptability = PointwiseMetric(
    metric="linguistic_acceptability",
    metric_prompt_template=linguistic_acceptability_metric_prompt_template,
    system_instruction=system_instruction,
)

eval_result = EvalTask(
    dataset=EVAL_DATASET,
    metrics=[linguistic_acceptability]
).evaluate()

您可以为 PairwiseMetric 使用相同的方法。

回答切换

对于 PairwiseMetrics,Gen AI Evaluation Service 会使用基准模型和候选模型的回答。评判模型会评估哪个回答更符合 metric_prompt_template 中的标准。不过,在某些设置中,评判模型可能会偏向基准模型或候选模型。

为了减少评估结果中的偏差,您可以启用回答切换功能。此技术会针对一半的评判模型调用交换基准模型和候选模型回答。以下示例展示了如何使用 Vertex AI SDK 启用响应翻转:

from vertexai.preview.evaluation import AutoraterConfig

pairwise_relevance_prompt_template = """
    # Instruction


    ### Response A
    {baseline_model_response}

    ### Response B
    {candidate_model_response}
"""

my_pairwise_metric = PairwiseMetric(
    metric="my_pairwise_metric",
    metric_prompt_template=pairwise_relevance_prompt_template,
    candidate_response_field_name = "candidate_model_response",
    baseline_response_field_name = "baseline_model_response"
)


# Define an AutoraterConfig with flip_enabled
my_autorater_config = AutoraterConfig(flip_enabled=True)

# Define an EvalTask with autorater_config
flip_enabled_eval_result = EvalTask(
    dataset=EVAL_DATASET,
    metrics=[my_pairwise_metric],
    autorater_config=my_autorater_config,
).evaluate()

多次采样

在评估期间,评判模型可能会在回答中表现出随机性。为了减轻这种随机性的影响并获得更一致的结果,您可以进行额外的抽样。这种技术也称为多重抽样

不过,增加采样也会增加完成请求的延迟时间。您可以使用 AutoraterConfig 将采样次数值更新为介于 1 到 32 之间的整数。我们建议使用默认的 sampling_count 值 4,以平衡随机性和延迟。

使用 Vertex AI SDK,您可以指定针对每个请求执行的采样次数:

from vertexai.preview.evaluation import AutoraterConfig

# Define customized sampling count in AutoraterConfig
autorater_config = AutoraterConfig(sampling_count=6)

# Run evaluation with the sampling count.
eval_result = EvalTask(
    dataset=EVAL_DATASET,
    metrics=[METRICS],
    autorater_config=autorater_config
).evaluate()

调优后的评判模型

如果您有适合评估应用场景的调优数据,则可以使用 Vertex AI SDK 调优 Gemini 模型作为评判模型,并使用调优后的模型进行评估。您可以通过 AutoraterConfig 将调优后的模型指定为评判模型:

from vertexai.preview.evaluation import {
   AutoraterConfig,
   PairwiseMetric,
   tune_autorater,
   evaluate_autorater,
}

# Tune a model to be the judge model. The tune_autorater helper function returns an AutoraterConfig with the judge model set as the tuned model.
autorater_config: AutoRaterConfig = tune_autorater(
    base_model="gemini-2.0-flash",
    train_dataset=f"{BUCKET_URI}/train/sft_train_samples.jsonl",
    validation_dataset=f"{BUCKET_URI}/val/sft_val_samples.jsonl",
    tuned_model_display_name=tuned_model_display_name,
)

# Alternatively, you can set up the judge model with an existing tuned model endpoint
autorater_config = AutoraterConfig(autorater_model=TUNED_MODEL)

# Use the tuned judge model
eval_result = EvalTask(
    dataset=EVAL_DATASET,
    metrics=[METRICS],
    autorater_config=autorater_config,
).evaluate()

后续步骤