Skip to content

Commit 13b64c1

Browse files
authored
refactor(generative_ai/evaluation): migrate vertexai.generative_models to genai SDK (#14430)
* refactor(generative_ai/evaluation): migrate vertexai.generative_models to genai SDK - Migrated evaluation samples to use the new `google.genai` SDK instead of `vertexai.generative_models`. - Removed unnecessary dependencies from requirements after testing confirmed they are no longer needed. * - Migrated sample to new folder under genai. - Refactored sample to use newer genai SDK. - Cleaned requirements files from unneccessary dependencies. * modified wrong file, undoing. * linting issues fixed. * Changed model attributes to use constants. * Remove sample that is out of scope. * Removed main method, and updated year on files. Addressed comments on PR.
1 parent 3a55196 commit 13b64c1

5 files changed

Lines changed: 185 additions & 0 deletions

File tree

genai/evaluation/noxfile_config.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
# Default TEST_CONFIG_OVERRIDE for python repos.
16+
17+
# You can copy this file into your directory, then it will be imported from
18+
# the noxfile.py.
19+
20+
# The source of truth:
21+
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py
22+
23+
TEST_CONFIG_OVERRIDE = {
24+
# You can opt out from the test for specific Python versions.
25+
"ignored_versions": ["3.8", "3.9", "3.10", "3.12", "3.13"],
26+
# Old samples are opted out of enforcing Python type hints
27+
# All new samples should feature them
28+
"enforce_type_hints": True,
29+
# An envvar key for determining the project id to use. Change it
30+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
31+
# build specific Cloud project. You can also use your own string
32+
# to use your own Cloud project.
33+
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
34+
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
35+
# If you need to use a specific version of pip,
36+
# change pip_version_override to the string representation
37+
# of the version number, for example, "20.2.4"
38+
"pip_version_override": None,
39+
# A dictionary you want to inject into your test. Don't put any
40+
# secrets here. These values will override predefined values.
41+
"envs": {},
42+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
# https://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+
# [START aiplatform_genai_evaluation_pairwise_summarization_quality]
16+
17+
import os
18+
19+
from google import genai
20+
from google.genai import types
21+
22+
import pandas as pd
23+
24+
from vertexai.evaluation import (
25+
EvalTask,
26+
MetricPromptTemplateExamples,
27+
PairwiseMetric,
28+
)
29+
from vertexai.preview.evaluation import EvalResult
30+
31+
# TODO (developer) set GOOGLE_CLOUD_PROJECT and REGION_ID
32+
# environment variables before running.
33+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
34+
LOCATION = os.getenv("REGION_ID")
35+
BASELINE_MODEL = os.getenv("BASELINE_MODEL", "gemini-2.5-flash")
36+
CANDIDATE_MODEL = os.getenv("CANDIDATE_MODEL", "gemini-2.5-pro")
37+
38+
PROMPT = """
39+
Summarize the text such that a five-year-old can understand.
40+
41+
# Text
42+
43+
As part of a comprehensive initiative to tackle urban congestion and foster
44+
sustainable urban living, a major city has revealed ambitious plans for an
45+
extensive overhaul of its public transportation system. The project aims not
46+
only to improve the efficiency and reliability of public transit but also to
47+
reduce the city\'s carbon footprint and promote eco-friendly commuting options.
48+
City officials anticipate that this strategic investment will enhance
49+
accessibility for residents and visitors alike, ushering in a new era of
50+
efficient, environmentally conscious urban transportation.
51+
"""
52+
53+
54+
def evaluate_output() -> EvalResult:
55+
"""
56+
Evaluates a candidate model's summarization quality
57+
against a baseline model using Vertex AI.
58+
"""
59+
60+
baseline_responses = []
61+
candidate_responses = []
62+
63+
genai_client = genai.Client(vertexai=True, project=PROJECT_ID, location=LOCATION)
64+
65+
baseline_resp = genai_client.models.generate_content(
66+
model=BASELINE_MODEL,
67+
contents=PROMPT,
68+
config=types.GenerateContentConfig(temperature=0.4),
69+
)
70+
baseline_responses.append(baseline_resp.text)
71+
72+
candidate_resp = genai_client.models.generate_content(
73+
model=CANDIDATE_MODEL,
74+
contents=PROMPT,
75+
config=types.GenerateContentConfig(temperature=0.4),
76+
)
77+
candidate_responses.append(candidate_resp.text)
78+
79+
eval_df = pd.DataFrame(
80+
{
81+
"prompt": PROMPT,
82+
"response": candidate_responses,
83+
"baseline_model_response": baseline_responses,
84+
}
85+
)
86+
87+
prompt_template = MetricPromptTemplateExamples.get_prompt_template(
88+
"pairwise_summarization_quality"
89+
)
90+
91+
pairwise_text_quality = PairwiseMetric(
92+
metric="pairwise_summarization_quality",
93+
metric_prompt_template=prompt_template,
94+
)
95+
96+
eval_task = EvalTask(
97+
dataset=eval_df,
98+
metrics=[pairwise_text_quality],
99+
experiment="pairwise-benchmark2",
100+
)
101+
102+
comparison_result = eval_task.evaluate()
103+
104+
pd.set_option("display.max_columns", None)
105+
pd.set_option("display.max_colwidth", 250)
106+
107+
columns_to_print = [
108+
"prompt",
109+
"baseline_model_response",
110+
"response",
111+
"pairwise_summarization_quality/pairwise_choice",
112+
"pairwise_summarization_quality/explanation",
113+
]
114+
print(comparison_result.metrics_table[columns_to_print])
115+
116+
return comparison_result
117+
118+
119+
# [END aiplatform_genai_evaluation_pairwise_summarization_quality]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest==9.0.3

genai/evaluation/requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pandas==2.3.3
2+
google-auth==2.55.2
3+
google-cloud-aiplatform[evaluation]==1.161.0
4+
google-genai==2.12.1
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
# https://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+
import pairwise_summarization_quality
16+
17+
def test_pairwise_evaluation_summarization_quality() -> None:
18+
response = pairwise_summarization_quality.evaluate_output()
19+
assert response

0 commit comments

Comments
 (0)