|
| 1 | +# Copyright 2026 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 | + |
| 17 | +from tests.unit.vertexai.genai.replays import pytest_helper |
| 18 | +from vertexai import types |
| 19 | +import pytest |
| 20 | + |
| 21 | + |
| 22 | +def test_gen_loss_clusters(client): |
| 23 | + """Tests that generate_loss_clusters() returns GenerateLossClustersResponse.""" |
| 24 | + eval_result = types.EvaluationResult() |
| 25 | + response = client.evals.generate_loss_clusters( |
| 26 | + eval_result=eval_result, |
| 27 | + config=types.LossAnalysisConfig( |
| 28 | + metric="multi_turn_task_success_v1", |
| 29 | + candidate="travel-agent", |
| 30 | + ), |
| 31 | + ) |
| 32 | + assert isinstance(response, types.GenerateLossClustersResponse) |
| 33 | + assert len(response.results) == 1 |
| 34 | + result = response.results[0] |
| 35 | + assert result.config.metric == "multi_turn_task_success_v1" |
| 36 | + assert result.config.candidate == "travel-agent" |
| 37 | + assert len(result.clusters) == 2 |
| 38 | + assert result.clusters[0].cluster_id == "cluster-1" |
| 39 | + assert result.clusters[0].taxonomy_entry.l1_category == "Tool Calling" |
| 40 | + assert ( |
| 41 | + result.clusters[0].taxonomy_entry.l2_category == "Missing Tool Invocation" |
| 42 | + ) |
| 43 | + assert result.clusters[0].item_count == 3 |
| 44 | + assert result.clusters[1].cluster_id == "cluster-2" |
| 45 | + assert result.clusters[1].taxonomy_entry.l1_category == "Hallucination" |
| 46 | + assert result.clusters[1].item_count == 2 |
| 47 | + |
| 48 | + |
| 49 | +pytest_plugins = ("pytest_asyncio",) |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.asyncio |
| 53 | +async def test_gen_loss_clusters_async(client): |
| 54 | + """Tests that generate_loss_clusters() async returns GenerateLossClustersResponse.""" |
| 55 | + eval_result = types.EvaluationResult() |
| 56 | + response = await client.aio.evals.generate_loss_clusters( |
| 57 | + eval_result=eval_result, |
| 58 | + config=types.LossAnalysisConfig( |
| 59 | + metric="multi_turn_task_success_v1", |
| 60 | + candidate="travel-agent", |
| 61 | + ), |
| 62 | + ) |
| 63 | + assert isinstance(response, types.GenerateLossClustersResponse) |
| 64 | + assert len(response.results) == 1 |
| 65 | + result = response.results[0] |
| 66 | + assert result.config.metric == "multi_turn_task_success_v1" |
| 67 | + assert len(result.clusters) == 2 |
| 68 | + assert result.clusters[0].cluster_id == "cluster-1" |
| 69 | + assert result.clusters[1].cluster_id == "cluster-2" |
| 70 | + |
| 71 | + |
| 72 | +pytestmark = pytest_helper.setup( |
| 73 | + file=__file__, |
| 74 | + globals_for_file=globals(), |
| 75 | + test_method="evals.generate_loss_clusters", |
| 76 | +) |
0 commit comments