配置评判模型

基于模型的指标提供了多种自定义方法,可帮助您根据自己的标准和应用场景生成评估指标。本页面介绍了如何针对理想的应用场景配置评判模型。

如需了解基本的评估工作流,请参阅 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 使用相同的方法。

评判模型配置

您可以通过 autorater_config 进一步自定义评判模型:

  • 回答切换:切换基准模型和候选模型的回答,以减少评估期间的评判模型偏差。

  • 多次采样:调整调用评判模型以获取评估得分的次数,从而提高一致性。

  • 调优后的评判模型:使用调优后的 LLM 作为评判模型。

回答切换

对于 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()

后续步骤