Skip to content

Commit ef39e29

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
feat: Add support for referencing registered metrics by resource name in evaluation run API
PiperOrigin-RevId: 878604099
1 parent 79d8e1c commit ef39e29

4 files changed

Lines changed: 67 additions & 1 deletion

File tree

vertexai/_genai/_evals_common.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from . import _gcs_utils
4646
from . import evals
4747
from . import types
48+
from . import _transformers as t
4849

4950
logger = logging.getLogger(__name__)
5051

@@ -1328,7 +1329,7 @@ def _resolve_dataset_inputs(
13281329

13291330

13301331
def _resolve_evaluation_run_metrics(
1331-
metrics: list[types.EvaluationRunMetric], api_client: Any
1332+
metrics: list[types.EvaluationRunMetric]|list[types.Metric], api_client: Any
13321333
) -> list[types.EvaluationRunMetric]:
13331334
"""Resolves a list of evaluation run metric instances, loading RubricMetric if necessary."""
13341335
if not metrics:
@@ -1361,6 +1362,16 @@ def _resolve_evaluation_run_metrics(
13611362
e,
13621363
)
13631364
raise
1365+
elif isinstance(metric_instance, types.Metric):
1366+
config_dict = t.t_metrics([metric_instance])[0]
1367+
res_name = config_dict.pop("metric_resource_name", None)
1368+
resolved_metrics_list.append(
1369+
types.EvaluationRunMetric(
1370+
metric=metric_instance.name,
1371+
metric_config=config_dict if config_dict else None,
1372+
metric_resource_name=res_name,
1373+
)
1374+
)
13641375
else:
13651376
try:
13661377
metric_name_str = str(metric_instance)

vertexai/_genai/_transformers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ def t_metrics(
3838

3939
for metric in metrics:
4040
metric_payload_item: dict[str, Any] = {}
41+
if hasattr(metric, "metric_resource_name") and metric.metric_resource_name:
42+
metric_payload_item["metric_resource_name"] = metric.metric_resource_name
4143

4244
metric_name = getv(metric, ["name"]).lower()
4345

@@ -79,6 +81,9 @@ def t_metrics(
7981
"return_raw_output": return_raw_output
8082
}
8183
metric_payload_item["pointwise_metric_spec"] = pointwise_spec
84+
elif "metric_resource_name" in metric_payload_item:
85+
# Valid case: Metric is identified by resource name; no inline spec required.
86+
pass
8287
else:
8388
raise ValueError(
8489
f"Unsupported metric type or invalid metric name: {metric_name}"

vertexai/_genai/evals.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,13 @@ def _EvaluationRunMetric_from_vertex(
399399
_UnifiedMetric_from_vertex(getv(from_object, ["metricConfig"]), to_object),
400400
)
401401

402+
if getv(from_object, ["metricResourceName"]) is not None:
403+
setv(
404+
to_object,
405+
["metric_resource_name"],
406+
getv(from_object, ["metricResourceName"]),
407+
)
408+
402409
return to_object
403410

404411

@@ -417,6 +424,13 @@ def _EvaluationRunMetric_to_vertex(
417424
_UnifiedMetric_to_vertex(getv(from_object, ["metric_config"]), to_object),
418425
)
419426

427+
if getv(from_object, ["metric_resource_name"]) is not None:
428+
setv(
429+
to_object,
430+
["metricResourceName"],
431+
getv(from_object, ["metric_resource_name"]),
432+
)
433+
420434
return to_object
421435

422436

@@ -2307,6 +2321,7 @@ async def _generate_rubrics(
23072321
types.PredefinedMetricSpecOrDict
23082322
] = None,
23092323
rubric_generation_spec: Optional[types.RubricGenerationSpecOrDict] = None,
2324+
metric_resource_name: Optional[str] = None,
23102325
config: Optional[types.RubricGenerationConfigOrDict] = None,
23112326
) -> types.GenerateInstanceRubricsResponse:
23122327
"""
@@ -2317,6 +2332,7 @@ async def _generate_rubrics(
23172332
contents=contents,
23182333
predefined_rubric_generation_spec=predefined_rubric_generation_spec,
23192334
rubric_generation_spec=rubric_generation_spec,
2335+
metric_resource_name=metric_resource_name,
23202336
config=config,
23212337
)
23222338

vertexai/_genai/types/common.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,6 +2235,9 @@ class PredefinedMetricSpec(_common.BaseModel):
22352235
default=None,
22362236
description="""The parameters needed to run the pre-defined metric.""",
22372237
)
2238+
metric_resource_name: Optional[str] = Field(
2239+
default=None, description="""The resource name of the metric definition."""
2240+
)
22382241

22392242

22402243
class PredefinedMetricSpecDict(TypedDict, total=False):
@@ -2247,6 +2250,9 @@ class PredefinedMetricSpecDict(TypedDict, total=False):
22472250
metric_spec_parameters: Optional[dict[str, Any]]
22482251
"""The parameters needed to run the pre-defined metric."""
22492252

2253+
metric_resource_name: Optional[str]
2254+
"""The resource name of the metric definition."""
2255+
22502256

22512257
PredefinedMetricSpecOrDict = Union[PredefinedMetricSpec, PredefinedMetricSpecDict]
22522258

@@ -2326,6 +2332,10 @@ class LLMBasedMetricSpec(_common.BaseModel):
23262332
default=None,
23272333
description="""Dynamically generate rubrics using this specification.""",
23282334
)
2335+
metric_resource_name: Optional[str] = Field(
2336+
default=None,
2337+
description="""The resource name of the metric definition. Example: projects/{project}/locations/{location}/evaluationMetrics/{evaluation_metric_id}""",
2338+
)
23292339

23302340

23312341
class LLMBasedMetricSpecDict(TypedDict, total=False):
@@ -2350,6 +2360,9 @@ class LLMBasedMetricSpecDict(TypedDict, total=False):
23502360
rubric_generation_spec: Optional[RubricGenerationSpecDict]
23512361
"""Dynamically generate rubrics using this specification."""
23522362

2363+
metric_resource_name: Optional[str]
2364+
"""The resource name of the metric definition. Example: projects/{project}/locations/{location}/evaluationMetrics/{evaluation_metric_id}"""
2365+
23532366

23542367
LLMBasedMetricSpecOrDict = Union[LLMBasedMetricSpec, LLMBasedMetricSpecDict]
23552368

@@ -2482,6 +2495,10 @@ class EvaluationRunMetric(_common.BaseModel):
24822495
metric_config: Optional[UnifiedMetric] = Field(
24832496
default=None, description="""The unified metric used for evaluation run."""
24842497
)
2498+
metric_resource_name: Optional[str] = Field(
2499+
default=None,
2500+
description="""The resource name of the metric definition. Example: projects/{project}/locations/{location}/evaluationMetrics/{evaluation_metric_id}""",
2501+
)
24852502

24862503

24872504
class EvaluationRunMetricDict(TypedDict, total=False):
@@ -2493,6 +2510,9 @@ class EvaluationRunMetricDict(TypedDict, total=False):
24932510
metric_config: Optional[UnifiedMetricDict]
24942511
"""The unified metric used for evaluation run."""
24952512

2513+
metric_resource_name: Optional[str]
2514+
"""The resource name of the metric definition. Example: projects/{project}/locations/{location}/evaluationMetrics/{evaluation_metric_id}"""
2515+
24962516

24972517
EvaluationRunMetricOrDict = Union[EvaluationRunMetric, EvaluationRunMetricDict]
24982518

@@ -4439,6 +4459,10 @@ class Metric(_common.BaseModel):
44394459
default=None,
44404460
description="""Optional steering instruction parameters for the automated predefined metric.""",
44414461
)
4462+
metric_resource_name: Optional[str] = Field(
4463+
default=None,
4464+
description="""The resource name of the metric definition. Example: projects/{project}/locations/{location}/evaluationMetrics/{evaluation_metric_id}""",
4465+
)
44424466

44434467
# Allow extra fields to support metric-specific config fields.
44444468
model_config = ConfigDict(extra="allow")
@@ -4643,6 +4667,9 @@ class MetricDict(TypedDict, total=False):
46434667
metric_spec_parameters: Optional[dict[str, Any]]
46444668
"""Optional steering instruction parameters for the automated predefined metric."""
46454669

4670+
metric_resource_name: Optional[str]
4671+
"""The resource name of the metric definition. Example: projects/{project}/locations/{location}/evaluationMetrics/{evaluation_metric_id}"""
4672+
46464673

46474674
MetricOrDict = Union[Metric, MetricDict]
46484675

@@ -5354,6 +5381,10 @@ class _GenerateInstanceRubricsRequest(_common.BaseModel):
53545381
default=None,
53555382
description="""Specification for how the rubrics should be generated.""",
53565383
)
5384+
metric_resource_name: Optional[str] = Field(
5385+
default=None,
5386+
description="""The resource name of the metric definition to use for rubric generation.""",
5387+
)
53575388
config: Optional[RubricGenerationConfig] = Field(default=None, description="""""")
53585389

53595390

@@ -5374,6 +5405,9 @@ class _GenerateInstanceRubricsRequestDict(TypedDict, total=False):
53745405
rubric_generation_spec: Optional[RubricGenerationSpecDict]
53755406
"""Specification for how the rubrics should be generated."""
53765407

5408+
metric_resource_name: Optional[str]
5409+
"""The resource name of the metric definition to use for rubric generation."""
5410+
53775411
config: Optional[RubricGenerationConfigDict]
53785412
""""""
53795413

0 commit comments

Comments
 (0)