|
| 1 | +# Copyright 2025 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 | +# http://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 | +from __future__ import annotations |
| 16 | + |
| 17 | +from .eval_metrics import Interval |
| 18 | +from .eval_metrics import MetricInfo |
| 19 | +from .eval_metrics import MetricInfoProvider |
| 20 | +from .eval_metrics import MetricValueInfo |
| 21 | +from .eval_metrics import PrebuiltMetrics |
| 22 | + |
| 23 | + |
| 24 | +class TrajectoryEvaluatorMetricInfoProvider(MetricInfoProvider): |
| 25 | + """Metric info provider for TrajectoryEvaluator.""" |
| 26 | + |
| 27 | + def get_metric_info(self) -> MetricInfo: |
| 28 | + return MetricInfo( |
| 29 | + metric_name=PrebuiltMetrics.TOOL_TRAJECTORY_AVG_SCORE.value, |
| 30 | + description=( |
| 31 | + "This metric compares two tool call trajectories (expected vs." |
| 32 | + " actual) for the same user interaction. It performs an exact match" |
| 33 | + " on the tool name and arguments for each step in the trajectory." |
| 34 | + " A score of 1.0 indicates a perfect match, while 0.0 indicates a" |
| 35 | + " mismatch. Higher values are better." |
| 36 | + ), |
| 37 | + metric_value_info=MetricValueInfo( |
| 38 | + interval=Interval(min_value=0.0, max_value=1.0) |
| 39 | + ), |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +class ResponseEvaluatorMetricInfoProvider(MetricInfoProvider): |
| 44 | + """Metric info provider for ResponseEvaluator.""" |
| 45 | + |
| 46 | + def __init__(self, metric_name: str): |
| 47 | + self._metric_name = metric_name |
| 48 | + |
| 49 | + def get_metric_info(self) -> MetricInfo: |
| 50 | + """Returns MetricInfo for the given metric name.""" |
| 51 | + if PrebuiltMetrics.RESPONSE_EVALUATION_SCORE.value == self._metric_name: |
| 52 | + return MetricInfo( |
| 53 | + metric_name=PrebuiltMetrics.RESPONSE_EVALUATION_SCORE.value, |
| 54 | + description=( |
| 55 | + "This metric evaluates how coherent agent's response was. Value" |
| 56 | + " range of this metric is [1,5], with values closer to 5 more" |
| 57 | + " desirable." |
| 58 | + ), |
| 59 | + metric_value_info=MetricValueInfo( |
| 60 | + interval=Interval(min_value=1.0, max_value=5.0) |
| 61 | + ), |
| 62 | + ) |
| 63 | + elif PrebuiltMetrics.RESPONSE_MATCH_SCORE.value == self._metric_name: |
| 64 | + return MetricInfo( |
| 65 | + metric_name=PrebuiltMetrics.RESPONSE_MATCH_SCORE.value, |
| 66 | + description=( |
| 67 | + "This metric evaluates if the agent's final response matches a" |
| 68 | + " golden/expected final response using Rouge_1 metric. Value" |
| 69 | + " range for this metric is [0,1], with values closer to 1 more" |
| 70 | + " desirable." |
| 71 | + ), |
| 72 | + metric_value_info=MetricValueInfo( |
| 73 | + interval=Interval(min_value=0.0, max_value=1.0) |
| 74 | + ), |
| 75 | + ) |
| 76 | + else: |
| 77 | + raise ValueError(f"`{self._metric_name}` is not supported.") |
| 78 | + |
| 79 | + |
| 80 | +class SafetyEvaluatorV1MetricInfoProvider(MetricInfoProvider): |
| 81 | + """Metric info provider for SafetyEvaluatorV1.""" |
| 82 | + |
| 83 | + def get_metric_info(self) -> MetricInfo: |
| 84 | + return MetricInfo( |
| 85 | + metric_name=PrebuiltMetrics.SAFETY_V1.value, |
| 86 | + description=( |
| 87 | + "This metric evaluates the safety (harmlessness) of an Agent's" |
| 88 | + " Response. Value range of the metric is [0, 1], with values closer" |
| 89 | + " to 1 to be more desirable (safe)." |
| 90 | + ), |
| 91 | + metric_value_info=MetricValueInfo( |
| 92 | + interval=Interval(min_value=0.0, max_value=1.0) |
| 93 | + ), |
| 94 | + ) |
| 95 | + |
| 96 | + |
| 97 | +class FinalResponseMatchV2EvaluatorMetricInfoProvider(MetricInfoProvider): |
| 98 | + """Metric info provider for FinalResponseMatchV2Evaluator.""" |
| 99 | + |
| 100 | + def get_metric_info(self) -> MetricInfo: |
| 101 | + return MetricInfo( |
| 102 | + metric_name=PrebuiltMetrics.FINAL_RESPONSE_MATCH_V2.value, |
| 103 | + description=( |
| 104 | + "This metric evaluates if the agent's final response matches a" |
| 105 | + " golden/expected final response using LLM as a judge. Value range" |
| 106 | + " for this metric is [0,1], with values closer to 1 more desirable." |
| 107 | + ), |
| 108 | + metric_value_info=MetricValueInfo( |
| 109 | + interval=Interval(min_value=0.0, max_value=1.0) |
| 110 | + ), |
| 111 | + ) |
| 112 | + |
| 113 | + |
| 114 | +class RubricBasedFinalResponseQualityV1EvaluatorMetricInfoProvider( |
| 115 | + MetricInfoProvider |
| 116 | +): |
| 117 | + """Metric info provider for RubricBasedFinalResponseQualityV1Evaluator.""" |
| 118 | + |
| 119 | + def get_metric_info(self) -> MetricInfo: |
| 120 | + return MetricInfo( |
| 121 | + metric_name=PrebuiltMetrics.RUBRIC_BASED_FINAL_RESPONSE_QUALITY_V1.value, |
| 122 | + description=( |
| 123 | + "This metric assess if the agent's final response against a set of" |
| 124 | + " rubrics using LLM as a judge. Value range for this metric is" |
| 125 | + " [0,1], with values closer to 1 more desirable." |
| 126 | + ), |
| 127 | + metric_value_info=MetricValueInfo( |
| 128 | + interval=Interval(min_value=0.0, max_value=1.0) |
| 129 | + ), |
| 130 | + ) |
| 131 | + |
| 132 | + |
| 133 | +class HallucinationsV1EvaluatorMetricInfoProvider(MetricInfoProvider): |
| 134 | + """Metric info provider for HallucinationsV1Evaluator.""" |
| 135 | + |
| 136 | + def get_metric_info(self) -> MetricInfo: |
| 137 | + return MetricInfo( |
| 138 | + metric_name=PrebuiltMetrics.HALLUCINATIONS_V1.value, |
| 139 | + description=( |
| 140 | + "This metric assesses whether a model response contains any false," |
| 141 | + " contradictory, or unsupported claims using a LLM as judge. Value" |
| 142 | + " range for this metric is [0,1], with values closer to 1 more" |
| 143 | + " desirable." |
| 144 | + ), |
| 145 | + metric_value_info=MetricValueInfo( |
| 146 | + interval=Interval(min_value=0.0, max_value=1.0) |
| 147 | + ), |
| 148 | + ) |
| 149 | + |
| 150 | + |
| 151 | +class RubricBasedToolUseV1EvaluatorMetricInfoProvider(MetricInfoProvider): |
| 152 | + """Metric info provider for RubricBasedToolUseV1Evaluator.""" |
| 153 | + |
| 154 | + def get_metric_info(self) -> MetricInfo: |
| 155 | + return MetricInfo( |
| 156 | + metric_name=PrebuiltMetrics.RUBRIC_BASED_TOOL_USE_QUALITY_V1.value, |
| 157 | + description=( |
| 158 | + "This metric assess if the agent's usage of tools against a set of" |
| 159 | + " rubrics using LLM as a judge. Value range for this metric is" |
| 160 | + " [0,1], with values closer to 1 more desirable." |
| 161 | + ), |
| 162 | + metric_value_info=MetricValueInfo( |
| 163 | + interval=Interval(min_value=0.0, max_value=1.0) |
| 164 | + ), |
| 165 | + ) |
| 166 | + |
| 167 | + |
| 168 | +class PerTurnUserSimulatorQualityV1MetricInfoProvider(MetricInfoProvider): |
| 169 | + """Metric info provider for PerTurnUserSimulatorQualityV1.""" |
| 170 | + |
| 171 | + def get_metric_info(self) -> MetricInfo: |
| 172 | + return MetricInfo( |
| 173 | + metric_name=PrebuiltMetrics.PER_TURN_USER_SIMULATOR_QUALITY_V1, |
| 174 | + description=( |
| 175 | + "This metric evaluates if the user messages generated by a " |
| 176 | + "user simulator follow the given conversation scenario. It " |
| 177 | + "validates each message separately. The resulting metric " |
| 178 | + "computes the percentage of user messages that we mark as " |
| 179 | + "valid. The value range for this metric is [0,1], with values " |
| 180 | + "closer to 1 more desirable. " |
| 181 | + ), |
| 182 | + metric_value_info=MetricValueInfo( |
| 183 | + interval=Interval(min_value=0.0, max_value=1.0) |
| 184 | + ), |
| 185 | + ) |
0 commit comments