forked from google/adk-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrubric_based_evaluator.py
More file actions
390 lines (317 loc) · 13.7 KB
/
rubric_based_evaluator.py
File metadata and controls
390 lines (317 loc) · 13.7 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# Copyright 2025 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 abc
import logging
import re
from typing import Optional
from typing_extensions import override
from ..models.llm_response import LlmResponse
from ..utils.feature_decorator import experimental
from .common import EvalBaseModel
from .eval_metrics import BaseCriterion
from .eval_metrics import EvalMetric
from .eval_rubrics import Rubric
from .eval_rubrics import RubricScore
from .evaluator import EvaluationResult
from .evaluator import PerInvocationResult
from .llm_as_judge import AutoRaterScore
from .llm_as_judge import LlmAsJudge
from .llm_as_judge_utils import get_average_rubric_score
from .llm_as_judge_utils import get_eval_status
from .llm_as_judge_utils import get_text_from_content
logger = logging.getLogger("google_adk." + __name__)
class RubricResponse(EvalBaseModel):
"""Internal data model to represent a rubric's response from the auto-rater."""
property_text: Optional[str] = None
rationale: Optional[str] = None
score: Optional[float] = None
class AutoRaterResponseParser(abc.ABC):
"""An interface for parsing auto rater's response."""
@abc.abstractmethod
def parse(self, auto_rater_response: str) -> list[RubricResponse]:
"""Parses the auto rater's response."""
raise NotImplementedError
_PROPERTY_PATTERN = r"(?<=Property: )(.*)"
_RATIONALE_PATTERN = r"(?<=Rationale: )(.*)"
_VERDICT_PATTERN = r"(?<=Verdict: )(.*)"
class DefaultAutoRaterResponseParser(AutoRaterResponseParser):
"""The default implementation of the AutoRaterResponseParser."""
def parse(self, auto_rater_response: str) -> list[RubricResponse]:
"""Returns a list of RubricResponse parsed from the AutoRater's response."""
properties = re.findall(_PROPERTY_PATTERN, auto_rater_response)
rationales = re.findall(_RATIONALE_PATTERN, auto_rater_response)
scores = []
for verdict in re.findall(_VERDICT_PATTERN, auto_rater_response):
if "yes" in verdict.lower():
score = 1.0
elif "no" in verdict.lower():
score = 0.0
else:
score = None
scores.append(score)
rubric_responses = []
for p, r, s in zip(properties, rationales, scores):
rubric_responses.append(
RubricResponse(property_text=p.strip(), rationale=r.strip(), score=s)
)
return rubric_responses
class PerInvocationResultsAggregator(abc.ABC):
"""An interface for aggregating per invocation samples.
AutoRaters that are backed by an LLM are known to have certain degree of
unreliabilty to their responses. In order to counter that we sample the
autorater more than once for a single invocation.
The aggregator helps convert those multiple samples into a single result.
"""
@abc.abstractmethod
def aggregate(
self,
per_invocation_samples: list[PerInvocationResult],
threshold: float,
) -> PerInvocationResult:
"""Aggregates per invocation samples into a single result."""
raise NotImplementedError
class MajorityVotePerInvocationResultsAggregator(
PerInvocationResultsAggregator
):
"""Aggregates per invocation samples using majority vote."""
def aggregate(
self,
per_invocation_samples: list[PerInvocationResult],
threshold: float,
) -> PerInvocationResult:
"""Returns a combined result for the invocation using majority vote.
This method takes all those samples for a single invocation and combines
them to generate one single result for the invocation.
This method specifically uses majority vote to aggregate scores for a
rubric. Take following Invocation and Rubric for example:
Invocation:
User: Is it going to be cold in Seattle tomorrow?
Weather Agent: No, it will be moderately warm as predicted temperature
for Seattle, WA tomorrow is 88F.
Rubric: Agent's response was concise and to the point.
We will sample the AutoRater 5 times, and the AutoRater responds
with (skipping the rationale field for now):
Sample 1:
Verdict: Yes
Sample 2:
Verdict: No
Sample 3:
Verdict: Yes
Sample 4:
Verdict: Yes
Sample 5:
Verdict: No
This method will use majority vote and combine the results of 5 samples
into one, and it will report "Yes" as the final verdict.
"""
score_category_by_rubric_id = {}
# We go over each rubric for each sample, and categorize the rubric into
# one of the following buckets:
# - Bucket 0: No score was generated for the rubric
# - Bucket 1: Score was generated and it was positive (1.0)
# - Bucket 2: Score was generated and it was negative (0.0)
for sample in per_invocation_samples:
if not sample.rubric_scores:
continue
for rubric_score in sample.rubric_scores:
rubric_id = rubric_score.rubric_id
if rubric_id not in score_category_by_rubric_id:
score_category_by_rubric_id[rubric_id] = ([], [], [])
if rubric_score.score is None: # No score
score_category_by_rubric_id[rubric_id][0].append(rubric_score)
elif rubric_score.score == 1.0: # Positive Result
score_category_by_rubric_id[rubric_id][1].append(rubric_score)
else: # Negative result
score_category_by_rubric_id[rubric_id][2].append(rubric_score)
aggregated_rubric_scores = []
for rubric_id in score_category_by_rubric_id:
no_scores, positives, negatives = score_category_by_rubric_id[rubric_id]
if not positives and not negatives:
# There has to be at least a no score rubric!
aggregated_rubric_scores.append(no_scores[0])
# This is where we are taking a majority vote.
elif len(positives) > len(negatives):
aggregated_rubric_scores.append(positives[0])
else:
aggregated_rubric_scores.append(negatives[0])
aggregated_overall_score = get_average_rubric_score(
aggregated_rubric_scores
)
return PerInvocationResult(
actual_invocation=per_invocation_samples[0].actual_invocation,
expected_invocation=per_invocation_samples[0].expected_invocation,
score=aggregated_overall_score,
rubric_scores=aggregated_rubric_scores,
eval_status=get_eval_status(aggregated_overall_score, threshold),
)
class InvocationResultsSummarizer(abc.ABC):
"""An interface for summarizing per invocation results."""
@abc.abstractmethod
def summarize(
self, per_invocation_results: list[PerInvocationResult], threshold: float
) -> EvaluationResult:
"""Summaries per invocation results into a single result."""
raise NotImplementedError
class MeanInvocationResultsSummarizer(InvocationResultsSummarizer):
"""Summarizes per invocation results using mean score."""
def summarize(
self, per_invocation_results: list[PerInvocationResult], threshold: float
) -> EvaluationResult:
"""Summarizes per invocation evaluation results into a single score.
A single eval case can have multiple invocations and the eval metric is
assessed for each invocation. But, we do want to summarize and make a
statement on how the eval case as a whole performed on the metric.
This method helps us aggregate rubric scores across invocation.
This method calculates the mean score of a rubric across several
invocations.
"""
unaggregated_rubric_scores = [] # Later used to calculate average.
# Collect rubric scores by id, so that we can calculate average score
# for each rubric id.
rubric_scores_by_id = {}
for sample in per_invocation_results:
if not sample.rubric_scores:
continue
for rubric_score in sample.rubric_scores:
rubric_id = rubric_score.rubric_id
if rubric_id not in rubric_scores_by_id:
rubric_scores_by_id[rubric_id] = []
rubric_scores_by_id[rubric_id].append(rubric_score)
unaggregated_rubric_scores.append(rubric_score)
aggregated_rubric_scores = []
for rubric_id, rubric_scores in rubric_scores_by_id.items():
overall_score = get_average_rubric_score(rubric_scores)
aggregated_rubric_scores.append(
RubricScore(
rubric_id=rubric_id,
score=overall_score,
# There is no real way for us generate a rationale here, so we
# make is clear to the consumer of the result.
rationale=(
"This is an aggregated score derived from individual entries."
" Please refer to individual entries in each invocation for"
" actual rationale from the model."
),
)
)
# Use unaggregate rubric score to calculate overall score.
aggregated_overall_score = get_average_rubric_score(
unaggregated_rubric_scores
)
return EvaluationResult(
overall_score=aggregated_overall_score,
overall_eval_status=get_eval_status(
aggregated_overall_score, threshold
),
per_invocation_results=per_invocation_results,
overall_rubric_scores=aggregated_rubric_scores,
)
def _normalize_text(text: str) -> str:
"""Returns a normalized version of the passed in text."""
if not isinstance(text, str):
return ""
return text.lower().strip()
@experimental
class RubricBasedEvaluator(LlmAsJudge):
"""A base class for rubric based evaluators."""
def __init__(
self,
eval_metric: EvalMetric,
criterion_type: type[BaseCriterion],
auto_rater_response_parser: AutoRaterResponseParser = (
DefaultAutoRaterResponseParser()
),
per_invocation_results_aggregator: PerInvocationResultsAggregator = (
MajorityVotePerInvocationResultsAggregator()
),
invocation_results_summarizer: InvocationResultsSummarizer = (
MeanInvocationResultsSummarizer()
),
):
"""Initializes the RubricBasedEvaluator.
Args:
eval_metric: The evaluation metric configuration.
criterion_type: The type of the criterion used for this evaluator.
auto_rater_response_parser: An object that parses the auto-rater's
response text and extracts rubric scores.
per_invocation_results_aggregator: An object that aggregates multiple
samples for a single invocation into a single result. This is useful in
cases where the auto-rater is an LLM and multiple samples are generated
to account for the unreliability of the LLM.
invocation_results_summarizer: An object that summarizes the results of
all invocations in an eval case into a single result.
"""
super().__init__(
eval_metric,
criterion_type=criterion_type,
)
self._auto_rater_prompt_template = ""
self._auto_rater_response_parser = auto_rater_response_parser
self._per_invocation_results_aggregator = per_invocation_results_aggregator
self._invocation_results_summarizer = invocation_results_summarizer
assert self._criterion.rubrics, "Rubrics are required."
self._rubrics: list[Rubric] = self._criterion.rubrics
self._normalized_rubric_to_id_map = {
_normalize_text(r.rubric_content.text_property): r.rubric_id
for r in self._rubrics
}
@override
def convert_auto_rater_response_to_score(
self, auto_rater_response: LlmResponse
) -> AutoRaterScore:
"""Returns an AutoRaterScore generated from AutoRater's response."""
response_text = get_text_from_content(auto_rater_response.content)
rubric_responses = self._auto_rater_response_parser.parse(response_text)
rubric_scores = []
for rubric_response in rubric_responses:
normalized_rubric = _normalize_text(rubric_response.property_text)
rubric_id = self._normalized_rubric_to_id_map.get(normalized_rubric, None)
if rubric_id:
rubric_scores.append(
RubricScore(
rubric_id=rubric_id,
rationale=rubric_response.rationale,
score=rubric_response.score,
)
)
else:
logger.warning(
f"Rubric {rubric_response.property_text} not found in the rubrics"
" provided to the metric."
)
aggregated_score = get_average_rubric_score(rubric_scores)
return AutoRaterScore(score=aggregated_score, rubric_scores=rubric_scores)
@override
def aggregate_per_invocation_samples(
self,
per_invocation_samples: list[PerInvocationResult],
) -> PerInvocationResult:
"""Returns a combined result by aggregating multiple samples for the same invocation.
AutoRaters that are backed by an LLM are known to have certain degree of
unreliabilty to their responses. In order to counter that we sample the
autorater more than once for a single invocation.
The aggregator helps convert those multiple samples into a single result.
"""
return self._per_invocation_results_aggregator.aggregate(
per_invocation_samples, self._eval_metric.threshold
)
@override
def aggregate_invocation_results(
self, per_invocation_results: list[PerInvocationResult]
) -> EvaluationResult:
"""Summarizes per invocation evaluation results into a single score."""
return self._invocation_results_summarizer.summarize(
per_invocation_results, self._eval_metric.threshold
)