Skip to content

Commit 0d63396

Browse files
sararobcopybara-github
authored andcommitted
chore: GenAI SDK client - add more replay tests for eval
PiperOrigin-RevId: 782058943
1 parent 22fa1fe commit 0d63396

2 files changed

Lines changed: 134 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
17+
from tests.unit.vertexai.genai.replays import pytest_helper
18+
from vertexai._genai import types
19+
import pandas as pd
20+
21+
22+
def test_evaluation_result(client):
23+
"""Tests that evaluate() produces a correctly structured EvaluationResult."""
24+
prompts_df = pd.DataFrame({"prompt": ["What is Taylor Swift's most recent album?"]})
25+
26+
eval_dataset = client.evals.run_inference(
27+
model="gemini-2.5-flash",
28+
src=prompts_df,
29+
)
30+
31+
metrics_to_run = [
32+
types.PrebuiltMetric.TEXT_QUALITY,
33+
]
34+
35+
evaluation_result = client.evals.evaluate(
36+
dataset=eval_dataset,
37+
metrics=metrics_to_run,
38+
)
39+
40+
assert isinstance(evaluation_result, types.EvaluationResult)
41+
42+
assert evaluation_result.summary_metrics is not None
43+
assert len(evaluation_result.summary_metrics) > 0
44+
for summary in evaluation_result.summary_metrics:
45+
assert isinstance(summary, types.AggregatedMetricResult)
46+
assert summary.metric_name is not None
47+
assert summary.mean_score is not None
48+
49+
assert evaluation_result.eval_case_results is not None
50+
assert len(evaluation_result.eval_case_results) > 0
51+
for case_result in evaluation_result.eval_case_results:
52+
assert isinstance(case_result, types.EvalCaseResult)
53+
assert case_result.eval_case_index is not None
54+
assert case_result.response_candidate_results is not None
55+
56+
57+
pytestmark = pytest_helper.setup(
58+
file=__file__,
59+
globals_for_file=globals(),
60+
test_method="evals.evaluate",
61+
)

tests/unit/vertexai/genai/replays/test_evaluate_instances.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from tests.unit.vertexai.genai.replays import pytest_helper
1919
from vertexai._genai import types
2020
import pandas as pd
21+
import json
2122

2223

2324
def test_bleu_metric(client):
@@ -34,6 +35,78 @@ def test_bleu_metric(client):
3435
assert len(response.bleu_results.bleu_metric_values) == 1
3536

3637

38+
def test_exact_match_metric(client):
39+
"""Tests the _evaluate_instances method with ExactMatchInput."""
40+
test_exact_match_input = types.ExactMatchInput(
41+
instances=[
42+
types.ExactMatchInstance(
43+
prediction="The quick brown fox jumps over the lazy dog.",
44+
reference="The quick brown fox jumps over the lazy dog.",
45+
)
46+
],
47+
metric_spec=types.ExactMatchSpec(),
48+
)
49+
response = client.evals._evaluate_instances(
50+
exact_match_input=test_exact_match_input
51+
)
52+
assert len(response.exact_match_results.exact_match_metric_values) == 1
53+
54+
55+
def test_rouge_metric(client):
56+
"""Tests the _evaluate_instances method with RougeInput."""
57+
test_rouge_input = types.RougeInput(
58+
instances=[
59+
types.RougeInstance(
60+
prediction="A fast brown fox leaps over a lazy dog.",
61+
reference="The quick brown fox jumps over the lazy dog.",
62+
)
63+
],
64+
metric_spec=types.RougeSpec(rouge_type="rougeL"),
65+
)
66+
response = client.evals._evaluate_instances(rouge_input=test_rouge_input)
67+
assert len(response.rouge_results.rouge_metric_values) == 1
68+
69+
70+
def test_pointwise_metric(client):
71+
"""Tests the _evaluate_instances method with PointwiseMetricInput."""
72+
instance_dict = {"prompt": "What is the capital of France?", "response": "Paris"}
73+
json_instance = json.dumps(instance_dict)
74+
75+
test_input = types.PointwiseMetricInput(
76+
instance=types.PointwiseMetricInstance(json_instance=json_instance),
77+
metric_spec=types.PointwiseMetricSpec(
78+
metric_prompt_template="Evaluate if the response '{response}' correctly answers the prompt '{prompt}'."
79+
),
80+
)
81+
response = client.evals._evaluate_instances(pointwise_metric_input=test_input)
82+
assert response.pointwise_metric_result is not None
83+
assert response.pointwise_metric_result.score is not None
84+
85+
86+
def test_pairwise_metric_with_autorater(client):
87+
"""Tests the _evaluate_instances method with PairwiseMetricInput and AutoraterConfig."""
88+
89+
instance_dict = {
90+
"baseline_response": "Short summary.",
91+
"candidate_response": "A longer, more detailed summary.",
92+
}
93+
json_instance = json.dumps(instance_dict)
94+
95+
test_input = types.PairwiseMetricInput(
96+
instance=types.PairwiseMetricInstance(json_instance=json_instance),
97+
metric_spec=types.PairwiseMetricSpec(
98+
metric_prompt_template="Which response is a better summary? Baseline: '{baseline_response}' or Candidate: '{candidate_response}'"
99+
),
100+
)
101+
autorater_config = types.AutoraterConfig(sampling_count=2)
102+
103+
response = client.evals._evaluate_instances(
104+
pairwise_metric_input=test_input, autorater_config=autorater_config
105+
)
106+
assert response.pairwise_metric_result is not None
107+
assert response.pairwise_metric_result.pairwise_choice is not None
108+
109+
37110
def test_run_inference_with_string_model(client):
38111
test_df = pd.DataFrame({"prompt": ["test prompt"]})
39112

0 commit comments

Comments
 (0)