Vertex AI Gen AI 评估服务

vertex-eval 功能将 adk-eval 连接到 Vertex AI Gen AI 评估服务。基于模型的判断由该服务的自动评估器执行,而不是由本地 LLM 执行;工具轨迹则由该服务基于计算的轨迹指标进行评分。每次调用都是针对 projects.locations:evaluateInstances (v1beta1) 的单个 POST 请求。

设置

[dependencies]
adk-eval = { version = "2.1.0", features = ["vertex-eval"] }

身份验证使用应用默认凭据(gcloud auth application-default login,或已部署容器的工作负载身份)。调用方需要 aiplatform.endpoints.predict 权限(roles/aiplatform.user)。

环境变量用途
GOOGLE_CLOUD_PROJECTVertexEvalConfig::from_env 的 GCP 项目
GOOGLE_CLOUD_LOCATION区域,例如 us-central1

服务支持的评审器

VertexEvalJudge 镜像了 LlmJudge 的评估接口——方法名称相同, 结果类型相同——因此可以直接用于针对本地评审器编写的代码:

use adk_eval::{VertexEvalClient, VertexEvalConfig, VertexEvalJudge};
use adk_eval::criteria::{Rubric, RubricConfig};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = VertexEvalConfig::from_env()?;
    let judge = VertexEvalJudge::new(VertexEvalClient::new_with_adc(config)?);

    // Semantic equivalence (pointwiseMetricInput under the hood)
    let result = judge
        .semantic_match("The capital is Paris", "Paris is the capital of France", None)
        .await?;
    println!("score={} equivalent={}", result.score, result.equivalent);

    // Rubric-based quality, weight-normalized like LlmJudge
    let rubrics = RubricConfig {
        rubrics: vec![
            Rubric::new("Accuracy", "Response is factually correct").with_weight(2.0),
            Rubric::new("Clarity", "Response is easy to follow"),
        ],
    };
    let quality = judge.evaluate_rubrics("agent output", "task context", &rubrics).await?;
    println!("overall={}", quality.overall_score);

    // Safety and hallucination checks
    let safety = judge.evaluate_safety("agent output").await?;
    let hallucination = judge
        .detect_hallucinations("agent output", "provided context", Some("ground truth"))
        .await?;
    println!("safe={} grounded={}", safety.is_safe, hallucination.hallucination_free);
    Ok(())
}

LlmJudge 的差异都源于服务为每次评判返回一个 {score, explanation} 对:

  • 布尔判定(equivalentis_safehallucination_free)根据分数推导——分数达到 0.5 或更高即视为通过。
  • issues 将服务的解释作为单个条目携带,而不是解析后的列表。

轨迹指标

VertexEvalClient::evaluate_trajectory 将 adk-eval ToolUse 值映射为 传输层 Trajectory 结构(nametoolNameargs → JSON 编码的 toolInput),并返回分数:

TrajectoryMetric含义
ExactMatch如果轨迹完全匹配,则为 1;否则为 0
InOrderMatch如果所有参考工具调用均按顺序出现,则为 1;否则为 0
AnyOrderMatch如果所有参考工具调用以任意顺序出现,则为 1,否则为 0
Precision预测工具调用的平均精确率
Recall参考工具调用的平均召回率
use adk_eval::{TrajectoryMetric, VertexEvalClient, VertexEvalConfig};
use adk_eval::schema::ToolUse;
use serde_json::json;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = VertexEvalClient::new_with_adc(VertexEvalConfig::from_env()?)?;

    let predicted = vec![ToolUse::new("get_weather").with_args(json!({ "city": "Paris" }))];
    let reference = predicted.clone();

    let score = client
        .evaluate_trajectory(TrajectoryMetric::ExactMatch, &predicted, &reference)
        .await?;
    assert_eq!(score, 1.0);
    Ok(())
}

评判模型配置

AutoraterConfig 选择基于模型的指标所使用的评判模型和采样配置;对于基于计算的指标,服务器会忽略此配置:

use adk_eval::{AutoraterConfig, VertexEvalClient, VertexEvalConfig};

fn build() -> adk_core::Result<VertexEvalClient> {
    let client = VertexEvalClient::new_with_adc(VertexEvalConfig::from_env()?)?
        .with_autorater_config(
            AutoraterConfig::new()
                .with_autorater_model(
                    "projects/p/locations/us-central1/publishers/google/models/gemini-3.7-flash",
                )
                .with_sampling_count(1),
        );
    Ok(client)
}

自定义指标

evaluate_pointwise 接受任意 PointwiseMetricSpecmetricPromptTemplate 包含 {placeholder} 变量,这些变量由服务器根据实例对象进行渲染:

use adk_eval::{PointwiseMetricSpec, VertexEvalClient, VertexEvalConfig};
use serde_json::json;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = VertexEvalClient::new_with_adc(VertexEvalConfig::from_env()?)?;

    let spec = PointwiseMetricSpec::new(
        "Rate the politeness of the response from 0.0 to 1.0.\n\nResponse:\n{response}",
    );
    let result = client
        .evaluate_pointwise(&spec, &json!({ "response": "Thanks for asking!" }))
        .await?;
    println!("score={:?} explanation={:?}", result.score, result.explanation);
    Ok(())
}

evaluate_instances 是原始接口:它会 POST 任意 EvaluateInstancesRequest 请求体并返回原始响应,从而可以访问服务支持的所有其他指标(BLEU、ROUGE、成对比较指标、工具调用指标)。

错误处理

错误是结构化的 AdkError 值,包含组件 evaleval.vertex.* 代码(eval.vertex.rate_limitedeval.vertex.unauthorizedeval.vertex.invalid_response,……)。VertexEvalJudge 方法返回 crate 的 EvalError::JudgeError,与 LlmJudge 一致。

另请参阅