Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions genai/evaluation/get_rouge_score.py
Original file line number Diff line number Diff line change
@@ -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()
42 changes: 42 additions & 0 deletions genai/evaluation/noxfile_config.py
Original file line number Diff line number Diff line change
@@ -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": {},
}
123 changes: 123 additions & 0 deletions genai/evaluation/pairwise_summarization_quality.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# 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

from google import genai
from google.genai import types

import pandas as pd

from vertexai.evaluation import (
EvalTask,
MetricPromptTemplateExamples,
PairwiseMetric,
)
from vertexai.preview.evaluation import EvalResult

# 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-benchmark2",
)

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()
1 change: 1 addition & 0 deletions genai/evaluation/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest==9.0.3
4 changes: 4 additions & 0 deletions genai/evaluation/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pandas==2.3.3
google-auth==2.55.2
google-cloud-aiplatform[evaluation]==1.161.0
google-genai==2.12.1
26 changes: 26 additions & 0 deletions genai/evaluation/test_evaluation.py
Original file line number Diff line number Diff line change
@@ -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