GeezerVison is showing github.com — feat: GenAI SDK client(evals) - Add StepBasedLLMMetric for multi-step… · hamgor/python-aiplatform@954c90d
File treeExpand file treeCollapse file tree Open diff view settings tests/unit/agentplatform/genai
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: agentplatform/_genai/_evals_metric_handlers.py +62Lines changed: 62 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -846,6 +846,66 @@ def aggregate( | 846 | 846 | return _default_aggregate_scores(self.metric.name, eval_case_metric_results) | 847 | 847 |
| 848 | 848 |
| | 849 | +class StepBasedLLMMetricHandler(LLMMetricHandler): | | 850 | + """Metric handler for step-based LLM metrics.""" | | 851 | + | | 852 | + @override | | 853 | + def get_metric_result( | | 854 | + self, eval_case: types.EvalCase, response_index: int | | 855 | + ) -> types.EvalCaseMetricResult: | | 856 | + """Evaluates one case with the step-based LLM metric.""" | | 857 | + try: | | 858 | + response_content = _get_response_from_eval_case( | | 859 | + eval_case, response_index, self.metric_name | | 860 | + ) | | 861 | + if not response_content and not getattr(eval_case, "agent_data", None): | | 862 | + raise ValueError( | | 863 | + f"Response content missing for candidate {response_index}." | | 864 | + ) | | 865 | + | | 866 | + # Joined so dataset columns referenced by any step reach other_data. | | 867 | + prompt_templates = "\n".join( | | 868 | + step.prompt_template or "" for step in self.metric.steps or [] | | 869 | + ) | | 870 | + instance = _build_evaluation_instance( | | 871 | + eval_case, response_content, prompt_template=prompt_templates | | 872 | + ) | | 873 | + api_response = _call_with_retry( | | 874 | + lambda: self.module._evaluate_instances( | | 875 | + metrics=[self.metric], | | 876 | + instance=instance, | | 877 | + allow_cross_region_model=self.allow_cross_region_model, | | 878 | + ), | | 879 | + self.metric_name, | | 880 | + ) | | 881 | + | | 882 | + if api_response and api_response.metric_results: | | 883 | + result = api_response.metric_results[0] | | 884 | + error_msg = None | | 885 | + if result.error and getattr(result.error, "code"): | | 886 | + error_msg = f"Error in metric result: {result.error}" | | 887 | + return types.EvalCaseMetricResult( | | 888 | + metric_name=self.metric_name, | | 889 | + score=result.score, | | 890 | + explanation=result.explanation, | | 891 | + error_message=error_msg, | | 892 | + ) | | 893 | + return types.EvalCaseMetricResult( | | 894 | + metric_name=self.metric_name, | | 895 | + error_message="Metric results missing in API response.", | | 896 | + ) | | 897 | + except Exception as e: # pylint: disable=broad-exception-caught | | 898 | + logger.error( | | 899 | + "Error processing metric %s for case %s.", | | 900 | + self.metric_name, | | 901 | + eval_case.eval_case_id, | | 902 | + exc_info=True, | | 903 | + ) | | 904 | + return types.EvalCaseMetricResult( | | 905 | + metric_name=self.metric_name, error_message=str(e) | | 906 | + ) | | 907 | + | | 908 | + | 849 | 909 | class CustomMetricHandler(MetricHandler[types.Metric]): | 850 | 910 | """Metric handler for custom metrics.""" | 851 | 911 |
| @@ -1375,6 +1435,7 @@ def aggregate( | 1375 | 1435 | lambda m: getattr(m, "metric_resource_name", None) is not None, | 1376 | 1436 | RegisteredMetricHandler, | 1377 | 1437 | ), | | 1438 | + (lambda m: getattr(m, "steps", None), StepBasedLLMMetricHandler), | 1378 | 1439 | ( | 1379 | 1440 | lambda m: m.name in ComputationMetricHandler.SUPPORTED_COMPUTATION_METRICS, | 1380 | 1441 | ComputationMetricHandler, | @@ -1395,6 +1456,7 @@ def aggregate( | 1395 | 1456 | ComputationMetricHandler, | 1396 | 1457 | TranslationMetricHandler, | 1397 | 1458 | LLMMetricHandler, | | 1459 | + StepBasedLLMMetricHandler, | 1398 | 1460 | CustomMetricHandler, | 1399 | 1461 | CustomCodeExecutionMetricHandler, | 1400 | 1462 | PredefinedMetricHandler, | |
Collapse file Copy file name to clipboardExpand all lines: agentplatform/_genai/_transformers.py +46Lines changed: 46 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -89,6 +89,11 @@ def t_metrics( | 89 | 89 | metric_payload_item["custom_code_execution_spec"] = { | 90 | 90 | "evaluation_function": metric.custom_function | 91 | 91 | } | | 92 | + # Step-based LLM metrics | | 93 | + elif getv(metric, ["steps"]): | | 94 | + metric_payload_item["step_based_llm_metric_spec"] = ( | | 95 | + _step_based_llm_metric_spec(metric) | | 96 | + ) | 92 | 97 | # LLM-based metrics | 93 | 98 | elif hasattr(metric, "prompt_template") and metric.prompt_template: | 94 | 99 | llm_based_spec: dict[str, Any] = { | @@ -145,6 +150,47 @@ def t_metrics( | 145 | 150 | return metrics_payload | 146 | 151 |
| 147 | 152 |
| | 153 | +def _dump(value: Any) -> Any: | | 154 | + if hasattr(value, "model_dump"): | | 155 | + return value.model_dump(exclude_none=True) | | 156 | + return value | | 157 | + | | 158 | + | | 159 | +def _step_based_llm_metric_spec(metric: Any) -> dict[str, Any]: | | 160 | + """Builds a StepBasedLlmMetricSpec payload. | | 161 | +
| | 162 | + autorater_config fields a step leaves unset fall back to the metric's judge | | 163 | + model settings. | | 164 | + """ | | 165 | + default_autorater_config: dict[str, Any] = {} | | 166 | + if getv(metric, ["judge_model"]): | | 167 | + default_autorater_config["autorater_model"] = getv(metric, ["judge_model"]) | | 168 | + if getv(metric, ["judge_model_generation_config"]): | | 169 | + default_autorater_config["generation_config"] = _dump( | | 170 | + getv(metric, ["judge_model_generation_config"]) | | 171 | + ) | | 172 | + if getv(metric, ["judge_model_sampling_count"]): | | 173 | + default_autorater_config["sampling_count"] = getv( | | 174 | + metric, ["judge_model_sampling_count"] | | 175 | + ) | | 176 | + | | 177 | + steps = [] | | 178 | + for step in getv(metric, ["steps"]): | | 179 | + step_payload = _dump(step) | | 180 | + if default_autorater_config: | | 181 | + step_payload["autorater_config"] = { | | 182 | + **default_autorater_config, | | 183 | + **step_payload.get("autorater_config", {}), | | 184 | + } | | 185 | + steps.append(step_payload) | | 186 | + | | 187 | + spec: dict[str, Any] = {"steps": steps} | | 188 | + preprocessor = getv(metric, ["preprocessor"]) | | 189 | + if preprocessor: | | 190 | + spec["preprocessor"] = _dump(preprocessor) | | 191 | + return spec | | 192 | + | | 193 | + | 148 | 194 | def t_metric_sources(metrics: list[Any]) -> list[dict[str, Any]]: | 149 | 195 | """Prepares the MetricSource payload.""" | 150 | 196 | sources_payload = [] | |
Collapse file Copy file name to clipboardExpand all lines: agentplatform/_genai/evals.py +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1172,6 +1172,13 @@ def _UnifiedMetric_from_vertex( | 1172 | 1172 | getv(from_object, ["computationBasedMetricSpec"]), | 1173 | 1173 | ) | 1174 | 1174 |
| | 1175 | + if getv(from_object, ["stepBasedLlmMetricSpec"]) is not None: | | 1176 | + setv( | | 1177 | + to_object, | | 1178 | + ["step_based_llm_metric_spec"], | | 1179 | + getv(from_object, ["stepBasedLlmMetricSpec"]), | | 1180 | + ) | | 1181 | + | 1175 | 1182 | return to_object | 1176 | 1183 |
| 1177 | 1184 |
| @@ -1223,6 +1230,13 @@ def _UnifiedMetric_to_vertex( | 1223 | 1230 | getv(from_object, ["computation_based_metric_spec"]), | 1224 | 1231 | ) | 1225 | 1232 |
| | 1233 | + if getv(from_object, ["step_based_llm_metric_spec"]) is not None: | | 1234 | + setv( | | 1235 | + to_object, | | 1236 | + ["stepBasedLlmMetricSpec"], | | 1237 | + getv(from_object, ["step_based_llm_metric_spec"]), | | 1238 | + ) | | 1239 | + | 1226 | 1240 | return to_object | 1227 | 1241 |
| 1228 | 1242 |
| |
Collapse file Copy file name to clipboardExpand all lines: agentplatform/_genai/types/__init__.py +50Lines changed: 50 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 199 | 199 | from .common import _UploadRagFileParameters | 200 | 200 | from .common import _UpsertExamplesParameters | 201 | 201 | from .common import AcceleratorType | | 202 | +from .common import AgentDataPreprocessor | | 203 | +from .common import AgentDataPreprocessorDict | | 204 | +from .common import AgentDataPreprocessorOrDict | 202 | 205 | from .common import AgentRunConfig | 203 | 206 | from .common import AgentRunConfigDict | 204 | 207 | from .common import AgentRunConfigOrDict | | 312 | 315 | from .common import ClientConnectionConfigDict | 313 | 316 | from .common import ClientConnectionConfigOrDict | 314 | 317 | from .common import CodeExecutionMetric | | 318 | +from .common import CodePreprocessorConfig | | 319 | +from .common import CodePreprocessorConfigDict | | 320 | +from .common import CodePreprocessorConfigOrDict | 315 | 321 | from .common import ColorMap | 316 | 322 | from .common import CometResult | 317 | 323 | from .common import CometResultDict | | 409 | 415 | from .common import CustomCodeExecutionSpec | 410 | 416 | from .common import CustomCodeExecutionSpecDict | 411 | 417 | from .common import CustomCodeExecutionSpecOrDict | | 418 | +from .common import CustomCodeParserConfig | | 419 | +from .common import CustomCodeParserConfigDict | | 420 | +from .common import CustomCodeParserConfigOrDict | 412 | 421 | from .common import CustomJob | 413 | 422 | from .common import CustomJobDict | 414 | 423 | from .common import CustomJobOrDict | | 806 | 815 | from .common import FetchExamplesResponse | 807 | 816 | from .common import FetchExamplesResponseDict | 808 | 817 | from .common import FetchExamplesResponseOrDict | | 818 | +from .common import FilterPreprocessorConfig | | 819 | +from .common import FilterPreprocessorConfigDict | | 820 | +from .common import FilterPreprocessorConfigOrDict | 809 | 821 | from .common import FlexStart | 810 | 822 | from .common import FlexStartDict | 811 | 823 | from .common import FlexStartOrDict | | 1851 | 1863 | from .common import ResponseCandidateResult | 1852 | 1864 | from .common import ResponseCandidateResultDict | 1853 | 1865 | from .common import ResponseCandidateResultOrDict | | 1866 | +from .common import ResponseParserConfig | | 1867 | +from .common import ResponseParserConfigDict | | 1868 | +from .common import ResponseParserConfigOrDict | 1854 | 1869 | from .common import RestoreVersionConfig | 1855 | 1870 | from .common import RestoreVersionConfigDict | 1856 | 1871 | from .common import RestoreVersionConfigOrDict | | 2185 | 2200 | from .common import SpeculativeDecodingSpecNgramSpeculationOrDict | 2186 | 2201 | from .common import SpeculativeDecodingSpecOrDict | 2187 | 2202 | from .common import State | | 2203 | +from .common import StepBasedLLMMetric | | 2204 | +from .common import StepBasedLLMMetricSpec | | 2205 | +from .common import StepBasedLLMMetricSpecDict | | 2206 | +from .common import StepBasedLLMMetricSpecOrDict | | 2207 | +from .common import StepBasedLLMMetricStep | | 2208 | +from .common import StepBasedLLMMetricStepDict | | 2209 | +from .common import StepBasedLLMMetricStepOrDict | 2188 | 2210 | from .common import StoredContentsExample | 2189 | 2211 | from .common import StoredContentsExampleDict | 2190 | 2212 | from .common import StoredContentsExampleFilter | | 2273 | 2295 | from .common import ToolParameterKVMatchSpec | 2274 | 2296 | from .common import ToolParameterKVMatchSpecDict | 2275 | 2297 | from .common import ToolParameterKVMatchSpecOrDict | | 2298 | +from .common import TraceCompressionConfig | | 2299 | +from .common import TraceCompressionConfigDict | | 2300 | +from .common import TraceCompressionConfigOrDict | 2276 | 2301 | from .common import TuningResourceUsageAssessmentConfig | 2277 | 2302 | from .common import TuningResourceUsageAssessmentConfigDict | 2278 | 2303 | from .common import TuningResourceUsageAssessmentConfigOrDict | | 2406 | 2431 | "EvaluationItem", | 2407 | 2432 | "EvaluationItemDict", | 2408 | 2433 | "EvaluationItemOrDict", | | 2434 | + "TraceCompressionConfig", | | 2435 | + "TraceCompressionConfigDict", | | 2436 | + "TraceCompressionConfigOrDict", | | 2437 | + "FilterPreprocessorConfig", | | 2438 | + "FilterPreprocessorConfigDict", | | 2439 | + "FilterPreprocessorConfigOrDict", | | 2440 | + "CodePreprocessorConfig", | | 2441 | + "CodePreprocessorConfigDict", | | 2442 | + "CodePreprocessorConfigOrDict", | | 2443 | + "AgentDataPreprocessor", | | 2444 | + "AgentDataPreprocessorDict", | | 2445 | + "AgentDataPreprocessorOrDict", | | 2446 | + "CustomCodeParserConfig", | | 2447 | + "CustomCodeParserConfigDict", | | 2448 | + "CustomCodeParserConfigOrDict", | | 2449 | + "ResponseParserConfig", | | 2450 | + "ResponseParserConfigDict", | | 2451 | + "ResponseParserConfigOrDict", | | 2452 | + "StepBasedLLMMetricStep", | | 2453 | + "StepBasedLLMMetricStepDict", | | 2454 | + "StepBasedLLMMetricStepOrDict", | 2409 | 2455 | "Metric", | 2410 | 2456 | "MetricDict", | 2411 | 2457 | "MetricOrDict", | | 2415 | 2461 | "CustomCodeExecutionSpec", | 2416 | 2462 | "CustomCodeExecutionSpecDict", | 2417 | 2463 | "CustomCodeExecutionSpecOrDict", | | 2464 | + "StepBasedLLMMetricSpec", | | 2465 | + "StepBasedLLMMetricSpecDict", | | 2466 | + "StepBasedLLMMetricSpecOrDict", | 2418 | 2467 | "UnifiedMetric", | 2419 | 2468 | "UnifiedMetricDict", | 2420 | 2469 | "UnifiedMetricOrDict", | | 4504 | 4553 | "PromptDataOrDict", | 4505 | 4554 | "LLMMetric", | 4506 | 4555 | "CodeExecutionMetric", | | 4556 | + "StepBasedLLMMetric", | 4507 | 4557 | "MetricPromptBuilder", | 4508 | 4558 | "RubricContentProperty", | 4509 | 4559 | "RubricContentPropertyDict", | |
|