|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Copyright 2026 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +"""Tests that `vertexai.types` metrics resolve in the `vertexai` evals client. |
| 18 | +
|
| 19 | +`vertexai.types` resolves to the agentplatform classes, so these objects reach |
| 20 | +`vertexai._genai._evals_common` as agentplatform instances. |
| 21 | +""" |
| 22 | + |
| 23 | +from unittest import mock |
| 24 | + |
| 25 | +import vertexai |
| 26 | +from vertexai._genai import _evals_common |
| 27 | +from vertexai._genai import _evals_metric_loaders |
| 28 | +from vertexai._genai import types as legacy_types |
| 29 | +from google.genai import types as genai_types |
| 30 | +import pytest |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture(autouse=True) |
| 34 | +def gcs_utils(): |
| 35 | + with mock.patch.object(_evals_metric_loaders._gcs_utils, "GcsUtils") as gcs: |
| 36 | + yield gcs |
| 37 | + |
| 38 | + |
| 39 | +def test_rubric_metric_resolves_to_predefined_spec_in_evaluation_run(gcs_utils): |
| 40 | + resolved = _evals_common._resolve_evaluation_run_metrics( |
| 41 | + [vertexai.types.RubricMetric.TOOL_USE_QUALITY(version="v2")], mock.Mock() |
| 42 | + ) |
| 43 | + |
| 44 | + assert len(resolved) == 1 |
| 45 | + assert isinstance(resolved[0], legacy_types.EvaluationRunMetric) |
| 46 | + assert resolved[0].metric == "tool_use_quality_v2" |
| 47 | + assert ( |
| 48 | + resolved[0].metric_config.predefined_metric_spec.metric_spec_name |
| 49 | + == "tool_use_quality_v2" |
| 50 | + ) |
| 51 | + gcs_utils.assert_not_called() |
| 52 | + |
| 53 | + |
| 54 | +def test_rubric_metric_keeps_requested_version(gcs_utils): |
| 55 | + resolved = _evals_common._resolve_evaluation_run_metrics( |
| 56 | + [vertexai.types.RubricMetric.TOOL_USE_QUALITY(version="v1")], mock.Mock() |
| 57 | + ) |
| 58 | + |
| 59 | + assert resolved[0].metric == "tool_use_quality_v1" |
| 60 | + gcs_utils.assert_not_called() |
| 61 | + |
| 62 | + |
| 63 | +def test_rubric_metric_resolves_in_evaluate(gcs_utils): |
| 64 | + resolved = _evals_common._resolve_metrics( |
| 65 | + [vertexai.types.RubricMetric.TOOL_USE_QUALITY(version="v2")], mock.Mock() |
| 66 | + ) |
| 67 | + |
| 68 | + assert len(resolved) == 1 |
| 69 | + assert isinstance(resolved[0], legacy_types.Metric) |
| 70 | + assert resolved[0].name == "tool_use_quality_v2" |
| 71 | + gcs_utils.assert_not_called() |
| 72 | + |
| 73 | + |
| 74 | +def test_llm_metric_resolves_in_evaluation_run(): |
| 75 | + resolved = _evals_common._resolve_evaluation_run_metrics( |
| 76 | + [vertexai.types.LLMMetric(name="my_metric", prompt_template="Rate {response}")], |
| 77 | + mock.Mock(), |
| 78 | + ) |
| 79 | + |
| 80 | + assert resolved[0].metric == "my_metric" |
| 81 | + assert resolved[0].metric_config is not None |
| 82 | + |
| 83 | + |
| 84 | +def test_llm_metric_converts_to_legacy_class_in_evaluate(): |
| 85 | + metric = vertexai.types.LLMMetric( |
| 86 | + name="my_metric", prompt_template="Rate {response}" |
| 87 | + ) |
| 88 | + |
| 89 | + resolved = _evals_common._resolve_metrics([metric], mock.Mock()) |
| 90 | + |
| 91 | + assert type(resolved[0]) is legacy_types.LLMMetric |
| 92 | + assert resolved[0].name == "my_metric" |
| 93 | + assert resolved[0].prompt_template == "Rate {response}" |
| 94 | + |
| 95 | + |
| 96 | +def test_evaluation_run_metric_passes_through(): |
| 97 | + metric = vertexai.types.EvaluationRunMetric( |
| 98 | + metric="my_metric", |
| 99 | + metric_config=vertexai.types.UnifiedMetric( |
| 100 | + predefined_metric_spec=genai_types.PredefinedMetricSpec( |
| 101 | + metric_spec_name="my_metric" |
| 102 | + ) |
| 103 | + ), |
| 104 | + ) |
| 105 | + |
| 106 | + resolved = _evals_common._resolve_evaluation_run_metrics([metric], mock.Mock()) |
| 107 | + |
| 108 | + assert type(resolved[0]) is legacy_types.EvaluationRunMetric |
| 109 | + assert resolved[0].metric == "my_metric" |
| 110 | + |
| 111 | + |
| 112 | +@pytest.mark.parametrize( |
| 113 | + "resolve", |
| 114 | + [ |
| 115 | + _evals_common._resolve_evaluation_run_metrics, |
| 116 | + _evals_common._resolve_metrics, |
| 117 | + ], |
| 118 | +) |
| 119 | +def test_unsupported_metric_type_raises_type_error(resolve, gcs_utils): |
| 120 | + with pytest.raises(TypeError, match="Unsupported metric type"): |
| 121 | + resolve([object()], mock.Mock()) |
| 122 | + gcs_utils.assert_not_called() |
0 commit comments