Skip to content

Commit de017de

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
feat: Add EvaluationMetric Create, Get and List methods to Vertex SDK GenAI evals
PiperOrigin-RevId: 882635735
1 parent 72942a4 commit de017de

5 files changed

Lines changed: 1352 additions & 473 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# pylint: disable=protected-access,bad-continuation,missing-function-docstring
16+
import re
17+
18+
from tests.unit.vertexai.genai.replays import pytest_helper
19+
from vertexai._genai import types
20+
21+
_TEST_PROJECT = "977012026409"
22+
_TEST_LOCATION = "us-central1"
23+
24+
25+
def test_create_and_get_evaluation_metric(client):
26+
client._api_client._http_options.api_version = "v1beta1"
27+
client._api_client._http_options.base_url = (
28+
"https://us-central1-staging-aiplatform.sandbox.googleapis.com/"
29+
)
30+
result = client.evals.create_evaluation_metric(
31+
display_name="test_metric",
32+
description="test_description",
33+
metric=types.RubricMetric.GENERAL_QUALITY,
34+
)
35+
assert isinstance(result, str)
36+
assert re.match(
37+
r"^projects/[^/]+/locations/[^/]+/evaluationMetrics/[^/]+$",
38+
result,
39+
)
40+
metric = client.evals.get_evaluation_metric(metric_resource_name=result)
41+
assert isinstance(metric, types.EvaluationMetric)
42+
assert metric.display_name == "test_metric"
43+
44+
45+
def test_list_evaluation_metrics(client):
46+
client._api_client._http_options.api_version = "v1beta1"
47+
client._api_client._http_options.base_url = (
48+
"https://us-central1-staging-aiplatform.sandbox.googleapis.com/"
49+
)
50+
response = client.evals.list_evaluation_metrics()
51+
assert isinstance(response, types.ListEvaluationMetricsResponse)
52+
assert len(response.evaluation_metrics) >= 0
53+
54+
55+
# The setup function registers the module and method for the recorder
56+
pytestmark = pytest_helper.setup(
57+
file=__file__,
58+
globals_for_file=globals(),
59+
test_method="evals.create_evaluation_metric",
60+
)

vertexai/_genai/_transformers.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,15 @@
2525
_METRIC_RES_NAME_RE = r"^projects/[^/]+/locations/[^/]+/evaluationMetrics/[^/]+$"
2626

2727

28+
def t_metric(
29+
metric: "types.MetricSubclass",
30+
) -> dict[str, Any]:
31+
"""Prepares the metric payload for a single metric."""
32+
return t_metrics([metric])[0]
33+
34+
2835
def t_metrics(
29-
metrics: list["types.MetricSubclass"],
36+
metrics: "list[types.MetricSubclass]",
3037
set_default_aggregation_metrics: bool = False,
3138
) -> list[dict[str, Any]]:
3239
"""Prepares the metric payload for the evaluation request.

0 commit comments

Comments
 (0)