GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: GenAI SDK client(evals) - Add StepBasedLLMMetric for multi-step… · hamgor/python-aiplatform@954c90d

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • agentplatform/_genai
    • _evals_metric_handlers.py
    • _transformers.py
    • evals.py
    • types
      • __init__.py
      • common.py
  • tests/unit/agentplatform/genai
    • replays
      • test_step_based_llm_metric.py
    • test_evals.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎agentplatform/_genai/_evals_metric_handlers.py‎

Copy file name to clipboardExpand all lines: agentplatform/_genai/_evals_metric_handlers.py
+62Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,66 @@ def aggregate(
846846
return _default_aggregate_scores(self.metric.name, eval_case_metric_results)
847847

848848

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+
849909
class CustomMetricHandler(MetricHandler[types.Metric]):
850910
"""Metric handler for custom metrics."""
851911

@@ -1375,6 +1435,7 @@ def aggregate(
13751435
lambda m: getattr(m, "metric_resource_name", None) is not None,
13761436
RegisteredMetricHandler,
13771437
),
1438+
(lambda m: getattr(m, "steps", None), StepBasedLLMMetricHandler),
13781439
(
13791440
lambda m: m.name in ComputationMetricHandler.SUPPORTED_COMPUTATION_METRICS,
13801441
ComputationMetricHandler,
@@ -1395,6 +1456,7 @@ def aggregate(
13951456
ComputationMetricHandler,
13961457
TranslationMetricHandler,
13971458
LLMMetricHandler,
1459+
StepBasedLLMMetricHandler,
13981460
CustomMetricHandler,
13991461
CustomCodeExecutionMetricHandler,
14001462
PredefinedMetricHandler,
Collapse file

‎agentplatform/_genai/_transformers.py‎

Copy file name to clipboardExpand all lines: agentplatform/_genai/_transformers.py
+46Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ def t_metrics(
8989
metric_payload_item["custom_code_execution_spec"] = {
9090
"evaluation_function": metric.custom_function
9191
}
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+
)
9297
# LLM-based metrics
9398
elif hasattr(metric, "prompt_template") and metric.prompt_template:
9499
llm_based_spec: dict[str, Any] = {
@@ -145,6 +150,47 @@ def t_metrics(
145150
return metrics_payload
146151

147152

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+
148194
def t_metric_sources(metrics: list[Any]) -> list[dict[str, Any]]:
149195
"""Prepares the MetricSource payload."""
150196
sources_payload = []
Collapse file

‎agentplatform/_genai/evals.py‎

Copy file name to clipboardExpand all lines: agentplatform/_genai/evals.py
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,13 @@ def _UnifiedMetric_from_vertex(
11721172
getv(from_object, ["computationBasedMetricSpec"]),
11731173
)
11741174

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+
11751182
return to_object
11761183

11771184

@@ -1223,6 +1230,13 @@ def _UnifiedMetric_to_vertex(
12231230
getv(from_object, ["computation_based_metric_spec"]),
12241231
)
12251232

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+
12261240
return to_object
12271241

12281242

Collapse file

‎agentplatform/_genai/types/__init__.py‎

Copy file name to clipboardExpand all lines: agentplatform/_genai/types/__init__.py
+50Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@
199199
from .common import _UploadRagFileParameters
200200
from .common import _UpsertExamplesParameters
201201
from .common import AcceleratorType
202+
from .common import AgentDataPreprocessor
203+
from .common import AgentDataPreprocessorDict
204+
from .common import AgentDataPreprocessorOrDict
202205
from .common import AgentRunConfig
203206
from .common import AgentRunConfigDict
204207
from .common import AgentRunConfigOrDict
@@ -312,6 +315,9 @@
312315
from .common import ClientConnectionConfigDict
313316
from .common import ClientConnectionConfigOrDict
314317
from .common import CodeExecutionMetric
318+
from .common import CodePreprocessorConfig
319+
from .common import CodePreprocessorConfigDict
320+
from .common import CodePreprocessorConfigOrDict
315321
from .common import ColorMap
316322
from .common import CometResult
317323
from .common import CometResultDict
@@ -409,6 +415,9 @@
409415
from .common import CustomCodeExecutionSpec
410416
from .common import CustomCodeExecutionSpecDict
411417
from .common import CustomCodeExecutionSpecOrDict
418+
from .common import CustomCodeParserConfig
419+
from .common import CustomCodeParserConfigDict
420+
from .common import CustomCodeParserConfigOrDict
412421
from .common import CustomJob
413422
from .common import CustomJobDict
414423
from .common import CustomJobOrDict
@@ -806,6 +815,9 @@
806815
from .common import FetchExamplesResponse
807816
from .common import FetchExamplesResponseDict
808817
from .common import FetchExamplesResponseOrDict
818+
from .common import FilterPreprocessorConfig
819+
from .common import FilterPreprocessorConfigDict
820+
from .common import FilterPreprocessorConfigOrDict
809821
from .common import FlexStart
810822
from .common import FlexStartDict
811823
from .common import FlexStartOrDict
@@ -1851,6 +1863,9 @@
18511863
from .common import ResponseCandidateResult
18521864
from .common import ResponseCandidateResultDict
18531865
from .common import ResponseCandidateResultOrDict
1866+
from .common import ResponseParserConfig
1867+
from .common import ResponseParserConfigDict
1868+
from .common import ResponseParserConfigOrDict
18541869
from .common import RestoreVersionConfig
18551870
from .common import RestoreVersionConfigDict
18561871
from .common import RestoreVersionConfigOrDict
@@ -2185,6 +2200,13 @@
21852200
from .common import SpeculativeDecodingSpecNgramSpeculationOrDict
21862201
from .common import SpeculativeDecodingSpecOrDict
21872202
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
21882210
from .common import StoredContentsExample
21892211
from .common import StoredContentsExampleDict
21902212
from .common import StoredContentsExampleFilter
@@ -2273,6 +2295,9 @@
22732295
from .common import ToolParameterKVMatchSpec
22742296
from .common import ToolParameterKVMatchSpecDict
22752297
from .common import ToolParameterKVMatchSpecOrDict
2298+
from .common import TraceCompressionConfig
2299+
from .common import TraceCompressionConfigDict
2300+
from .common import TraceCompressionConfigOrDict
22762301
from .common import TuningResourceUsageAssessmentConfig
22772302
from .common import TuningResourceUsageAssessmentConfigDict
22782303
from .common import TuningResourceUsageAssessmentConfigOrDict
@@ -2406,6 +2431,27 @@
24062431
"EvaluationItem",
24072432
"EvaluationItemDict",
24082433
"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",
24092455
"Metric",
24102456
"MetricDict",
24112457
"MetricOrDict",
@@ -2415,6 +2461,9 @@
24152461
"CustomCodeExecutionSpec",
24162462
"CustomCodeExecutionSpecDict",
24172463
"CustomCodeExecutionSpecOrDict",
2464+
"StepBasedLLMMetricSpec",
2465+
"StepBasedLLMMetricSpecDict",
2466+
"StepBasedLLMMetricSpecOrDict",
24182467
"UnifiedMetric",
24192468
"UnifiedMetricDict",
24202469
"UnifiedMetricOrDict",
@@ -4504,6 +4553,7 @@
45044553
"PromptDataOrDict",
45054554
"LLMMetric",
45064555
"CodeExecutionMetric",
4556+
"StepBasedLLMMetric",
45074557
"MetricPromptBuilder",
45084558
"RubricContentProperty",
45094559
"RubricContentPropertyDict",

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.