|
| 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 | +) |
0 commit comments