From aa4ace614e92fea581f55e1730ace9dcafb10fac Mon Sep 17 00:00:00 2001 From: David del Real Sifuentes Date: Thu, 16 Jul 2026 22:40:56 +0000 Subject: [PATCH 1/4] 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. --- generative_ai/evaluation/get_rouge_score.py | 12 ++-- generative_ai/evaluation/noxfile_config.py | 2 +- .../pairwise_summarization_quality.py | 68 ++++++++++++------- .../evaluation/requirements-test.txt | 6 +- generative_ai/evaluation/requirements.txt | 17 ++--- 5 files changed, 59 insertions(+), 46 deletions(-) diff --git a/generative_ai/evaluation/get_rouge_score.py b/generative_ai/evaluation/get_rouge_score.py index 579c0931374..323750f95d6 100644 --- a/generative_ai/evaluation/get_rouge_score.py +++ b/generative_ai/evaluation/get_rouge_score.py @@ -11,19 +11,21 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + +# [START generativeaionvertexai_evaluation_get_rouge_score] + import os +import pandas as pd + +import vertexai from vertexai.preview.evaluation import EvalResult +from vertexai.preview.evaluation import EvalTask PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") def get_rouge_score() -> EvalResult: - # [START generativeaionvertexai_evaluation_get_rouge_score] - import pandas as pd - - import vertexai - from vertexai.preview.evaluation import EvalTask # TODO(developer): Update & uncomment line below # PROJECT_ID = "your-project-id" diff --git a/generative_ai/evaluation/noxfile_config.py b/generative_ai/evaluation/noxfile_config.py index 0973c8621c7..1d2019a0b18 100644 --- a/generative_ai/evaluation/noxfile_config.py +++ b/generative_ai/evaluation/noxfile_config.py @@ -22,7 +22,7 @@ TEST_CONFIG_OVERRIDE = { # You can opt out from the test for specific Python versions. - "ignored_versions": ["3.8", "3.9", "3.11", "3.12", "3.13"], + "ignored_versions": ["3.8", "3.9", "3.10", "3.12", "3.13"], # Old samples are opted out of enforcing Python type hints # All new samples should feature them "enforce_type_hints": True, diff --git a/generative_ai/evaluation/pairwise_summarization_quality.py b/generative_ai/evaluation/pairwise_summarization_quality.py index 88c89871904..ea116a5cb8d 100644 --- a/generative_ai/evaluation/pairwise_summarization_quality.py +++ b/generative_ai/evaluation/pairwise_summarization_quality.py @@ -11,28 +11,50 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + +# [START generativeaionvertexai_evaluation_pairwise_summarization_quality] + import os -from vertexai.preview.evaluation import EvalResult +import pandas as pd +from google import genai +from google.genai import types + +import vertexai +from vertexai.preview.evaluation import EvalResult +from vertexai.evaluation import ( + EvalTask, + PairwiseMetric, + MetricPromptTemplateExamples, +) + +# TODO (developer) set GOOGLE_CLOUD_PROJECT and REGION_ID +# environment variables before running. PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") +LOCATION = os.getenv("REGION_ID") +BASELINE_MODEL = "gemini-2.5-flash" -def evaluate_output() -> EvalResult: - # [START generativeaionvertexai_evaluation_pairwise_summarization_quality] - import pandas as pd - - import vertexai - from vertexai.generative_models import GenerativeModel - from vertexai.evaluation import ( - EvalTask, - PairwiseMetric, - MetricPromptTemplateExamples, +def custom_model_fn(prompt: str) -> str: + """Generates text from a prompt using the baseline Gemini model via Vertex AI.""" + + genai_client = genai.Client(vertexai=True, project=PROJECT_ID, location=LOCATION) + + genai_config = types.GenerateContentConfig(temperature=0.4) + + response = genai_client.models.generate_content( + model=BASELINE_MODEL, contents=prompt, config=genai_config ) - # TODO(developer): Update & uncomment line below - # PROJECT_ID = "your-project-id" - vertexai.init(project=PROJECT_ID, location="us-central1") + return response.text + + +def evaluate_output() -> EvalResult: + """ + Evaluates a candidate model's summarization quality + against a baseline model using Vertex AI. + """ prompt = """ Summarize the text such that a five-year-old can understand. @@ -49,15 +71,9 @@ def evaluate_output() -> EvalResult: efficient, environmentally conscious urban transportation. """ - eval_dataset = pd.DataFrame({"prompt": [prompt]}) - - # Baseline model for pairwise comparison - baseline_model = GenerativeModel("gemini-2.0-flash-lite-001") + vertexai.init(project=PROJECT_ID, location=LOCATION) - # Candidate model for pairwise comparison - candidate_model = GenerativeModel( - "gemini-2.0-flash-001", generation_config={"temperature": 0.4} - ) + eval_dataset = pd.DataFrame({"prompt": [prompt]}) prompt_template = MetricPromptTemplateExamples.get_prompt_template( "pairwise_summarization_quality" @@ -66,7 +82,7 @@ def evaluate_output() -> EvalResult: summarization_quality_metric = PairwiseMetric( metric="pairwise_summarization_quality", metric_prompt_template=prompt_template, - baseline_model=baseline_model, + baseline_model=BASELINE_MODEL, ) eval_task = EvalTask( @@ -74,13 +90,17 @@ def evaluate_output() -> EvalResult: metrics=[summarization_quality_metric], experiment="pairwise-experiment", ) - result = eval_task.evaluate(model=candidate_model) + result = eval_task.evaluate( + model=custom_model_fn, experiment_run_name="genai-client-vs-legacy-baseline-6" + ) baseline_model_response = result.metrics_table["baseline_model_response"].iloc[0] candidate_model_response = result.metrics_table["response"].iloc[0] + winner_model = result.metrics_table[ "pairwise_summarization_quality/pairwise_choice" ].iloc[0] + explanation = result.metrics_table[ "pairwise_summarization_quality/explanation" ].iloc[0] diff --git a/generative_ai/evaluation/requirements-test.txt b/generative_ai/evaluation/requirements-test.txt index baa23bf9c3e..598922c8c25 100644 --- a/generative_ai/evaluation/requirements-test.txt +++ b/generative_ai/evaluation/requirements-test.txt @@ -1,4 +1,4 @@ backoff==2.2.1 -google-api-core==2.19.0 -pytest==9.0.3; python_version >= "3.10" -pytest-asyncio==0.23.6 +google-api-core==2.31.0 +pytest==9.0.3 +pytest-asyncio==1.4.0 diff --git a/generative_ai/evaluation/requirements.txt b/generative_ai/evaluation/requirements.txt index 18ff8773d69..842e7cc88b6 100644 --- a/generative_ai/evaluation/requirements.txt +++ b/generative_ai/evaluation/requirements.txt @@ -1,13 +1,4 @@ -pandas==2.2.3; python_version == '3.7' -pandas==2.2.3; python_version == '3.8' -pandas==2.2.3; python_version > '3.8' -pillow==12.2.0 -google-cloud-aiplatform[full]==1.157.0 -sentencepiece==0.2.1 -google-auth==2.38.0 -anthropic[vertex]==0.28.0 -langchain-core==1.4.0 -langchain-google-vertexai==3.2.3 -numpy<3 -openai==1.68.2 -immutabledict==4.2.0 +pandas==2.3.3 +google-auth==2.55.2 +google-cloud-aiplatform[full]==1.161.0 +google-genai==2.12.1 From f55a0531b89f2b2937f8bcbe209d9fbf7907054b Mon Sep 17 00:00:00 2001 From: David del Real Sifuentes Date: Mon, 20 Jul 2026 20:35:29 +0000 Subject: [PATCH 2/4] - Migrated sample to new folder under genai. - Refactored sample to use newer genai SDK. - Cleaned requirements files from unneccessary dependencies. --- genai/evaluation/get_rouge_score.py | 101 +++++++++++++++ genai/evaluation/noxfile_config.py | 42 +++++++ .../pairwise_summarization_quality.py | 119 ++++++++++++++++++ genai/evaluation/requirements-test.txt | 1 + genai/evaluation/requirements.txt | 4 + genai/evaluation/test_evaluation.py | 26 ++++ generative_ai/evaluation/get_rouge_score.py | 14 +-- generative_ai/evaluation/noxfile_config.py | 2 +- .../pairwise_summarization_quality.py | 68 ++++------ .../evaluation/requirements-test.txt | 6 +- generative_ai/evaluation/requirements.txt | 17 ++- 11 files changed, 340 insertions(+), 60 deletions(-) create mode 100644 genai/evaluation/get_rouge_score.py create mode 100644 genai/evaluation/noxfile_config.py create mode 100644 genai/evaluation/pairwise_summarization_quality.py create mode 100644 genai/evaluation/requirements-test.txt create mode 100644 genai/evaluation/requirements.txt create mode 100644 genai/evaluation/test_evaluation.py diff --git a/genai/evaluation/get_rouge_score.py b/genai/evaluation/get_rouge_score.py new file mode 100644 index 00000000000..20e5c7871ff --- /dev/null +++ b/genai/evaluation/get_rouge_score.py @@ -0,0 +1,101 @@ +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# TODO: to be refactored in b/536110099 + +import os + +import pandas as pd + +import vertexai +from vertexai.preview.evaluation import EvalResult +from vertexai.preview.evaluation import EvalTask + +PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") + + +def get_rouge_score() -> EvalResult: + + # TODO(developer): Update & uncomment line below + # PROJECT_ID = "your-project-id" + vertexai.init(project=PROJECT_ID, location="us-central1") + + reference_summarization = """ + The Great Barrier Reef, the world's largest coral reef system, is + located off the coast of Queensland, Australia. It's a vast + ecosystem spanning over 2,300 kilometers with thousands of reefs + and islands. While it harbors an incredible diversity of marine + life, including endangered species, it faces serious threats from + climate change, ocean acidification, and coral bleaching.""" + + # Compare pre-generated model responses against the reference (ground truth). + eval_dataset = pd.DataFrame( + { + "response": [ + """The Great Barrier Reef, the world's largest coral reef system located + in Australia, is a vast and diverse ecosystem. However, it faces serious + threats from climate change, ocean acidification, and coral bleaching, + endangering its rich marine life.""", + """The Great Barrier Reef, a vast coral reef system off the coast of + Queensland, Australia, is the world's largest. It's a complex ecosystem + supporting diverse marine life, including endangered species. However, + climate change, ocean acidification, and coral bleaching are serious + threats to its survival.""", + """The Great Barrier Reef, the world's largest coral reef system off the + coast of Australia, is a vast and diverse ecosystem with thousands of + reefs and islands. It is home to a multitude of marine life, including + endangered species, but faces serious threats from climate change, ocean + acidification, and coral bleaching.""", + ], + "reference": [reference_summarization] * 3, + } + ) + eval_task = EvalTask( + dataset=eval_dataset, + metrics=[ + "rouge_1", + "rouge_2", + "rouge_l", + "rouge_l_sum", + ], + ) + result = eval_task.evaluate() + + print("Summary Metrics:\n") + for key, value in result.summary_metrics.items(): + print(f"{key}: \t{value}") + + print("\n\nMetrics Table:\n") + print(result.metrics_table) + # Example response: + # + # Summary Metrics: + # + # row_count: 3 + # rouge_1/mean: 0.7191161666666667 + # rouge_1/std: 0.06765143922270488 + # rouge_2/mean: 0.5441118566666666 + # ... + # Metrics Table: + # + # response reference ... rouge_l/score rouge_l_sum/score + # 0 The Great Barrier Reef, the world's ... \n The Great Barrier Reef, the ... ... 0.577320 0.639175 + # 1 The Great Barrier Reef, a vast coral... \n The Great Barrier Reef, the ... ... 0.552381 0.666667 + # 2 The Great Barrier Reef, the world's ... \n The Great Barrier Reef, the ... ... 0.774775 0.774775 + # + return result + + +if __name__ == "__main__": + get_rouge_score() diff --git a/genai/evaluation/noxfile_config.py b/genai/evaluation/noxfile_config.py new file mode 100644 index 00000000000..1d2019a0b18 --- /dev/null +++ b/genai/evaluation/noxfile_config.py @@ -0,0 +1,42 @@ +# Copyright 2021 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Default TEST_CONFIG_OVERRIDE for python repos. + +# You can copy this file into your directory, then it will be imported from +# the noxfile.py. + +# The source of truth: +# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py + +TEST_CONFIG_OVERRIDE = { + # You can opt out from the test for specific Python versions. + "ignored_versions": ["3.8", "3.9", "3.10", "3.12", "3.13"], + # Old samples are opted out of enforcing Python type hints + # All new samples should feature them + "enforce_type_hints": True, + # An envvar key for determining the project id to use. Change it + # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a + # build specific Cloud project. You can also use your own string + # to use your own Cloud project. + "gcloud_project_env": "GOOGLE_CLOUD_PROJECT", + # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', + # If you need to use a specific version of pip, + # change pip_version_override to the string representation + # of the version number, for example, "20.2.4" + "pip_version_override": None, + # A dictionary you want to inject into your test. Don't put any + # secrets here. These values will override predefined values. + "envs": {}, +} diff --git a/genai/evaluation/pairwise_summarization_quality.py b/genai/evaluation/pairwise_summarization_quality.py new file mode 100644 index 00000000000..3d04ad5de46 --- /dev/null +++ b/genai/evaluation/pairwise_summarization_quality.py @@ -0,0 +1,119 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START aiplatform_genai_evaluation_pairwise_summarization_quality] + +import os + +import pandas as pd + +from google import genai +from google.genai import types + +from vertexai.preview.evaluation import EvalResult +from vertexai.evaluation import ( + EvalTask, + PairwiseMetric, + MetricPromptTemplateExamples, +) + +# TODO (developer) set GOOGLE_CLOUD_PROJECT and REGION_ID +# environment variables before running. +PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") +LOCATION = os.getenv("REGION_ID") +BASELINE_MODEL = os.getenv("BASELINE_MODEL", "gemini-2.5-flash") +CANDIDATE_MODEL = os.getenv("CANDIDATE_MODEL", "gemini-2.5-pro") + +PROMPT = """ + Summarize the text such that a five-year-old can understand. + + # Text + + As part of a comprehensive initiative to tackle urban congestion and foster + sustainable urban living, a major city has revealed ambitious plans for an + extensive overhaul of its public transportation system. The project aims not + only to improve the efficiency and reliability of public transit but also to + reduce the city\'s carbon footprint and promote eco-friendly commuting options. + City officials anticipate that this strategic investment will enhance + accessibility for residents and visitors alike, ushering in a new era of + efficient, environmentally conscious urban transportation. + """ + +def evaluate_output() -> EvalResult: + """ + Evaluates a candidate model's summarization quality + against a baseline model using Vertex AI. + """ + + baseline_responses = [] + candidate_responses = [] + + genai_client = genai.Client(vertexai=True, project=PROJECT_ID, location=LOCATION) + + baseline_resp = genai_client.models.generate_content( + model="gemini-2.5-flash", + contents=PROMPT, + config=types.GenerateContentConfig(temperature=0.4) + ) + baseline_responses.append(baseline_resp.text) + + candidate_resp = genai_client.models.generate_content( + model="gemini-2.5-pro", + contents=PROMPT, + config=types.GenerateContentConfig(temperature=0.4) + ) + candidate_responses.append(candidate_resp.text) + + eval_df = pd.DataFrame({ + "prompt": PROMPT, + "response": candidate_responses, + "baseline_model_response": baseline_responses + }) + + prompt_template = MetricPromptTemplateExamples.get_prompt_template( + "pairwise_summarization_quality" + ) + + pairwise_text_quality = PairwiseMetric( + metric="pairwise_summarization_quality", + metric_prompt_template=prompt_template, + ) + + eval_task = EvalTask( + dataset=eval_df, + metrics=[pairwise_text_quality], + experiment="pairwise-benchmark", + ) + + comparison_result = eval_task.evaluate() + + pd.set_option('display.max_columns', None) + pd.set_option('display.max_colwidth', 250) + + columns_to_print = [ + "prompt", + "baseline_model_response", + "response", + "pairwise_summarization_quality/pairwise_choice", + "pairwise_summarization_quality/explanation" + ] + print(comparison_result.metrics_table[columns_to_print]) + + return comparison_result + +# [END aiplatform_genai_evaluation_pairwise_summarization_quality] + + +if __name__ == "__main__": + evaluate_output() diff --git a/genai/evaluation/requirements-test.txt b/genai/evaluation/requirements-test.txt new file mode 100644 index 00000000000..b3b2b3ca4cc --- /dev/null +++ b/genai/evaluation/requirements-test.txt @@ -0,0 +1 @@ +pytest==9.0.3 diff --git a/genai/evaluation/requirements.txt b/genai/evaluation/requirements.txt new file mode 100644 index 00000000000..666fe9e6240 --- /dev/null +++ b/genai/evaluation/requirements.txt @@ -0,0 +1,4 @@ +pandas==2.3.3 +google-auth==2.55.2 +google-cloud-aiplatform==1.161.0 +google-genai==2.12.1 diff --git a/genai/evaluation/test_evaluation.py b/genai/evaluation/test_evaluation.py new file mode 100644 index 00000000000..8263eb97709 --- /dev/null +++ b/genai/evaluation/test_evaluation.py @@ -0,0 +1,26 @@ +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import get_rouge_score +import pairwise_summarization_quality + + +def test_create_evaluation_task() -> None: + response = get_rouge_score.get_rouge_score() + assert response + + +def test_pairwise_evaluation_summarization_quality() -> None: + response = pairwise_summarization_quality.evaluate_output() + assert response diff --git a/generative_ai/evaluation/get_rouge_score.py b/generative_ai/evaluation/get_rouge_score.py index 323750f95d6..3e7206f95d5 100644 --- a/generative_ai/evaluation/get_rouge_score.py +++ b/generative_ai/evaluation/get_rouge_score.py @@ -11,21 +11,19 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - -# [START generativeaionvertexai_evaluation_get_rouge_score] - import os -import pandas as pd - -import vertexai from vertexai.preview.evaluation import EvalResult -from vertexai.preview.evaluation import EvalTask PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") def get_rouge_score() -> EvalResult: + # TODO To be refactored in b/536110099 + import pandas as pd + + import vertexai + from vertexai.preview.evaluation import EvalTask # TODO(developer): Update & uncomment line below # PROJECT_ID = "your-project-id" @@ -93,7 +91,7 @@ def get_rouge_score() -> EvalResult: # 0 The Great Barrier Reef, the world's ... \n The Great Barrier Reef, the ... ... 0.577320 0.639175 # 1 The Great Barrier Reef, a vast coral... \n The Great Barrier Reef, the ... ... 0.552381 0.666667 # 2 The Great Barrier Reef, the world's ... \n The Great Barrier Reef, the ... ... 0.774775 0.774775 - # [END generativeaionvertexai_evaluation_get_rouge_score] + return result diff --git a/generative_ai/evaluation/noxfile_config.py b/generative_ai/evaluation/noxfile_config.py index 1d2019a0b18..0973c8621c7 100644 --- a/generative_ai/evaluation/noxfile_config.py +++ b/generative_ai/evaluation/noxfile_config.py @@ -22,7 +22,7 @@ TEST_CONFIG_OVERRIDE = { # You can opt out from the test for specific Python versions. - "ignored_versions": ["3.8", "3.9", "3.10", "3.12", "3.13"], + "ignored_versions": ["3.8", "3.9", "3.11", "3.12", "3.13"], # Old samples are opted out of enforcing Python type hints # All new samples should feature them "enforce_type_hints": True, diff --git a/generative_ai/evaluation/pairwise_summarization_quality.py b/generative_ai/evaluation/pairwise_summarization_quality.py index ea116a5cb8d..88c89871904 100644 --- a/generative_ai/evaluation/pairwise_summarization_quality.py +++ b/generative_ai/evaluation/pairwise_summarization_quality.py @@ -11,50 +11,28 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - -# [START generativeaionvertexai_evaluation_pairwise_summarization_quality] - import os -import pandas as pd - -from google import genai -from google.genai import types - -import vertexai from vertexai.preview.evaluation import EvalResult -from vertexai.evaluation import ( - EvalTask, - PairwiseMetric, - MetricPromptTemplateExamples, -) - -# TODO (developer) set GOOGLE_CLOUD_PROJECT and REGION_ID -# environment variables before running. -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") -LOCATION = os.getenv("REGION_ID") -BASELINE_MODEL = "gemini-2.5-flash" - -def custom_model_fn(prompt: str) -> str: - """Generates text from a prompt using the baseline Gemini model via Vertex AI.""" - - genai_client = genai.Client(vertexai=True, project=PROJECT_ID, location=LOCATION) +PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - genai_config = types.GenerateContentConfig(temperature=0.4) - response = genai_client.models.generate_content( - model=BASELINE_MODEL, contents=prompt, config=genai_config +def evaluate_output() -> EvalResult: + # [START generativeaionvertexai_evaluation_pairwise_summarization_quality] + import pandas as pd + + import vertexai + from vertexai.generative_models import GenerativeModel + from vertexai.evaluation import ( + EvalTask, + PairwiseMetric, + MetricPromptTemplateExamples, ) - return response.text - - -def evaluate_output() -> EvalResult: - """ - Evaluates a candidate model's summarization quality - against a baseline model using Vertex AI. - """ + # TODO(developer): Update & uncomment line below + # PROJECT_ID = "your-project-id" + vertexai.init(project=PROJECT_ID, location="us-central1") prompt = """ Summarize the text such that a five-year-old can understand. @@ -71,10 +49,16 @@ def evaluate_output() -> EvalResult: efficient, environmentally conscious urban transportation. """ - vertexai.init(project=PROJECT_ID, location=LOCATION) - eval_dataset = pd.DataFrame({"prompt": [prompt]}) + # Baseline model for pairwise comparison + baseline_model = GenerativeModel("gemini-2.0-flash-lite-001") + + # Candidate model for pairwise comparison + candidate_model = GenerativeModel( + "gemini-2.0-flash-001", generation_config={"temperature": 0.4} + ) + prompt_template = MetricPromptTemplateExamples.get_prompt_template( "pairwise_summarization_quality" ) @@ -82,7 +66,7 @@ def evaluate_output() -> EvalResult: summarization_quality_metric = PairwiseMetric( metric="pairwise_summarization_quality", metric_prompt_template=prompt_template, - baseline_model=BASELINE_MODEL, + baseline_model=baseline_model, ) eval_task = EvalTask( @@ -90,17 +74,13 @@ def evaluate_output() -> EvalResult: metrics=[summarization_quality_metric], experiment="pairwise-experiment", ) - result = eval_task.evaluate( - model=custom_model_fn, experiment_run_name="genai-client-vs-legacy-baseline-6" - ) + result = eval_task.evaluate(model=candidate_model) baseline_model_response = result.metrics_table["baseline_model_response"].iloc[0] candidate_model_response = result.metrics_table["response"].iloc[0] - winner_model = result.metrics_table[ "pairwise_summarization_quality/pairwise_choice" ].iloc[0] - explanation = result.metrics_table[ "pairwise_summarization_quality/explanation" ].iloc[0] diff --git a/generative_ai/evaluation/requirements-test.txt b/generative_ai/evaluation/requirements-test.txt index 598922c8c25..baa23bf9c3e 100644 --- a/generative_ai/evaluation/requirements-test.txt +++ b/generative_ai/evaluation/requirements-test.txt @@ -1,4 +1,4 @@ backoff==2.2.1 -google-api-core==2.31.0 -pytest==9.0.3 -pytest-asyncio==1.4.0 +google-api-core==2.19.0 +pytest==9.0.3; python_version >= "3.10" +pytest-asyncio==0.23.6 diff --git a/generative_ai/evaluation/requirements.txt b/generative_ai/evaluation/requirements.txt index 842e7cc88b6..18ff8773d69 100644 --- a/generative_ai/evaluation/requirements.txt +++ b/generative_ai/evaluation/requirements.txt @@ -1,4 +1,13 @@ -pandas==2.3.3 -google-auth==2.55.2 -google-cloud-aiplatform[full]==1.161.0 -google-genai==2.12.1 +pandas==2.2.3; python_version == '3.7' +pandas==2.2.3; python_version == '3.8' +pandas==2.2.3; python_version > '3.8' +pillow==12.2.0 +google-cloud-aiplatform[full]==1.157.0 +sentencepiece==0.2.1 +google-auth==2.38.0 +anthropic[vertex]==0.28.0 +langchain-core==1.4.0 +langchain-google-vertexai==3.2.3 +numpy<3 +openai==1.68.2 +immutabledict==4.2.0 From 1183bf99f477b6c27edf0a794cf75b43804f09a8 Mon Sep 17 00:00:00 2001 From: David del Real Sifuentes Date: Mon, 20 Jul 2026 20:39:39 +0000 Subject: [PATCH 3/4] modified wrong file, undoing. --- generative_ai/evaluation/get_rouge_score.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/generative_ai/evaluation/get_rouge_score.py b/generative_ai/evaluation/get_rouge_score.py index 3e7206f95d5..579c0931374 100644 --- a/generative_ai/evaluation/get_rouge_score.py +++ b/generative_ai/evaluation/get_rouge_score.py @@ -19,7 +19,7 @@ def get_rouge_score() -> EvalResult: - # TODO To be refactored in b/536110099 + # [START generativeaionvertexai_evaluation_get_rouge_score] import pandas as pd import vertexai @@ -91,7 +91,7 @@ def get_rouge_score() -> EvalResult: # 0 The Great Barrier Reef, the world's ... \n The Great Barrier Reef, the ... ... 0.577320 0.639175 # 1 The Great Barrier Reef, a vast coral... \n The Great Barrier Reef, the ... ... 0.552381 0.666667 # 2 The Great Barrier Reef, the world's ... \n The Great Barrier Reef, the ... ... 0.774775 0.774775 - + # [END generativeaionvertexai_evaluation_get_rouge_score] return result From 00b951b8d478b847866c8b1587a113ab0feae11b Mon Sep 17 00:00:00 2001 From: David del Real Sifuentes Date: Mon, 20 Jul 2026 22:15:37 +0000 Subject: [PATCH 4/4] linting issues fixed. --- .../pairwise_summarization_quality.py | 38 ++++++++++--------- genai/evaluation/requirements.txt | 2 +- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/genai/evaluation/pairwise_summarization_quality.py b/genai/evaluation/pairwise_summarization_quality.py index 3d04ad5de46..260064da152 100644 --- a/genai/evaluation/pairwise_summarization_quality.py +++ b/genai/evaluation/pairwise_summarization_quality.py @@ -16,17 +16,17 @@ import os -import pandas as pd - from google import genai from google.genai import types -from vertexai.preview.evaluation import EvalResult +import pandas as pd + from vertexai.evaluation import ( EvalTask, - PairwiseMetric, MetricPromptTemplateExamples, + PairwiseMetric, ) +from vertexai.preview.evaluation import EvalResult # TODO (developer) set GOOGLE_CLOUD_PROJECT and REGION_ID # environment variables before running. @@ -50,6 +50,7 @@ efficient, environmentally conscious urban transportation. """ + def evaluate_output() -> EvalResult: """ Evaluates a candidate model's summarization quality @@ -64,22 +65,24 @@ def evaluate_output() -> EvalResult: baseline_resp = genai_client.models.generate_content( model="gemini-2.5-flash", contents=PROMPT, - config=types.GenerateContentConfig(temperature=0.4) + config=types.GenerateContentConfig(temperature=0.4), ) baseline_responses.append(baseline_resp.text) candidate_resp = genai_client.models.generate_content( model="gemini-2.5-pro", contents=PROMPT, - config=types.GenerateContentConfig(temperature=0.4) + config=types.GenerateContentConfig(temperature=0.4), ) candidate_responses.append(candidate_resp.text) - eval_df = pd.DataFrame({ - "prompt": PROMPT, - "response": candidate_responses, - "baseline_model_response": baseline_responses - }) + eval_df = pd.DataFrame( + { + "prompt": PROMPT, + "response": candidate_responses, + "baseline_model_response": baseline_responses, + } + ) prompt_template = MetricPromptTemplateExamples.get_prompt_template( "pairwise_summarization_quality" @@ -93,25 +96,26 @@ def evaluate_output() -> EvalResult: eval_task = EvalTask( dataset=eval_df, metrics=[pairwise_text_quality], - experiment="pairwise-benchmark", + experiment="pairwise-benchmark2", ) comparison_result = eval_task.evaluate() - pd.set_option('display.max_columns', None) - pd.set_option('display.max_colwidth', 250) + pd.set_option("display.max_columns", None) + pd.set_option("display.max_colwidth", 250) columns_to_print = [ - "prompt", + "prompt", "baseline_model_response", "response", - "pairwise_summarization_quality/pairwise_choice", - "pairwise_summarization_quality/explanation" + "pairwise_summarization_quality/pairwise_choice", + "pairwise_summarization_quality/explanation", ] print(comparison_result.metrics_table[columns_to_print]) return comparison_result + # [END aiplatform_genai_evaluation_pairwise_summarization_quality] diff --git a/genai/evaluation/requirements.txt b/genai/evaluation/requirements.txt index 666fe9e6240..f039d20dab4 100644 --- a/genai/evaluation/requirements.txt +++ b/genai/evaluation/requirements.txt @@ -1,4 +1,4 @@ pandas==2.3.3 google-auth==2.55.2 -google-cloud-aiplatform==1.161.0 +google-cloud-aiplatform[evaluation]==1.161.0 google-genai==2.12.1