-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathfinal_response_match_v1.py
More file actions
147 lines (118 loc) · 5.02 KB
/
Copy pathfinal_response_match_v1.py
File metadata and controls
147 lines (118 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# 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
#
# 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.
from __future__ import annotations
import re
from typing import Optional
from google.genai import types as genai_types
from typing_extensions import override
from ..dependencies.rouge_scorer import rouge_scorer
from .eval_case import ConversationScenario
from .eval_case import Invocation
from .eval_metrics import EvalMetric
from .evaluator import EvalStatus
from .evaluator import EvaluationResult
from .evaluator import Evaluator
from .evaluator import PerInvocationResult
class RougeEvaluator(Evaluator):
"""Evaluates if agent's final response matches a golden/expected final response using Rouge_1 metric.
Value range for this metric is [0,1], with values closer to 1 more desirable.
"""
def __init__(self, eval_metric: EvalMetric):
self._eval_metric = eval_metric
@override
def evaluate_invocations(
self,
actual_invocations: list[Invocation],
expected_invocations: Optional[list[Invocation]] = None,
conversation_scenario: Optional[ConversationScenario] = None,
) -> EvaluationResult:
if expected_invocations is None:
raise ValueError("expected_invocations is required for this metric.")
del conversation_scenario # not used by this metric.
total_score = 0.0
num_invocations = 0
per_invocation_results = []
for actual, expected in zip(actual_invocations, expected_invocations):
reference = _get_text_from_content(expected.final_response)
response = _get_text_from_content(actual.final_response)
rouge_1_scores = _calculate_rouge_1_scores(response, reference)
score = rouge_1_scores.fmeasure
per_invocation_results.append(
PerInvocationResult(
actual_invocation=actual,
expected_invocation=expected,
score=score,
eval_status=_get_eval_status(score, self._eval_metric.threshold),
)
)
total_score += score
num_invocations += 1
if per_invocation_results:
overall_score = total_score / num_invocations
return EvaluationResult(
overall_score=overall_score,
overall_eval_status=_get_eval_status(
overall_score, self._eval_metric.threshold
),
per_invocation_results=per_invocation_results,
)
return EvaluationResult()
def _get_text_from_content(content: Optional[genai_types.Content]) -> str:
if content and content.parts:
return "\n".join([part.text for part in content.parts if part.text])
return ""
def _get_eval_status(score: float, threshold: float):
return EvalStatus.PASSED if score >= threshold else EvalStatus.FAILED
class _UnicodeTokenizer:
"""Tokenizer that handles Unicode text with word-boundary awareness.
The default RougeScorer tokenizer splits on whitespace, which works for
ASCII and Latin-script text but produces zero tokens for text in scripts
without word boundaries (Chinese, Japanese, Thai, etc.).
For ASCII-majority text this tokenizer uses Unicode-aware word-character
matching (``\\w+`` in re). For non-ASCII text it falls back to whitespace
splitting, then character-level tokenization.
"""
def tokenize(self, text: str) -> list[str]:
"""Tokenizes text using Unicode-aware word boundaries."""
ascii_chars = sum(1 for c in text if ord(c) < 128)
if ascii_chars > len(text) * 0.5:
return re.findall(r"\w+", text.lower())
tokens = text.lower().split()
if tokens:
return tokens
return list(text.lower())
def _calculate_rouge_1_scores(candidate: str, reference: str):
"""Calculates the ROUGE-1 score between a candidate and reference text.
ROUGE-1 measures the overlap of unigrams (single words) between the
candidate and reference texts. The score is broken down into:
- Precision: The proportion of unigrams in the candidate that are also in the
reference.
- Recall: The proportion of unigrams in the reference that are also in the
candidate.
- F-measure: The harmonic mean of precision and recall.
Args:
candidate: The generated text to be evaluated.
reference: The ground-truth text to compare against.
Returns:
A dictionary containing the ROUGE-1 precision, recall, and f-measure.
"""
scorer = rouge_scorer.RougeScorer(
["rouge1"],
use_stemmer=True,
tokenizer=_UnicodeTokenizer(),
)
# The score method returns a dictionary where keys are the ROUGE types
# and values are Score objects (tuples) with precision, recall, and fmeasure.
scores = scorer.score(reference, candidate)
return scores["rouge1"]