Skip to content

Commit 95ea580

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
feat: GenAI Client(evals) - add user-facing generate_loss_clusters with LRO polling and replay tests
PiperOrigin-RevId: 893874547
1 parent 5414089 commit 95ea580

File tree

7 files changed

+781
-15
lines changed

7 files changed

+781
-15
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 result.clusters[0].taxonomy_entry.l2_category == "Missing Tool Invocation"
41+
assert result.clusters[0].item_count == 3
42+
assert result.clusters[1].cluster_id == "cluster-2"
43+
assert result.clusters[1].taxonomy_entry.l1_category == "Hallucination"
44+
assert result.clusters[1].item_count == 2
45+
46+
47+
pytest_plugins = ("pytest_asyncio",)
48+
49+
50+
@pytest.mark.asyncio
51+
async def test_gen_loss_clusters_async(client):
52+
"""Tests that generate_loss_clusters() async returns GenerateLossClustersResponse."""
53+
eval_result = types.EvaluationResult()
54+
response = await client.aio.evals.generate_loss_clusters(
55+
eval_result=eval_result,
56+
config=types.LossAnalysisConfig(
57+
metric="multi_turn_task_success_v1",
58+
candidate="travel-agent",
59+
),
60+
)
61+
assert isinstance(response, types.GenerateLossClustersResponse)
62+
assert len(response.results) == 1
63+
result = response.results[0]
64+
assert result.config.metric == "multi_turn_task_success_v1"
65+
assert len(result.clusters) == 2
66+
assert result.clusters[0].cluster_id == "cluster-1"
67+
assert result.clusters[1].cluster_id == "cluster-2"
68+
69+
70+
pytestmark = pytest_helper.setup(
71+
file=__file__,
72+
globals_for_file=globals(),
73+
test_method="evals.generate_loss_clusters",
74+
)

0 commit comments

Comments
 (0)