|
| 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] |
0 commit comments