Skip to content

Commit 9d4aeca

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 2b0a98c commit 9d4aeca

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

vertexai/_genai/_evals_common.py

Lines changed: 15 additions & 4 deletions
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

@@ -1320,16 +1321,14 @@ def _resolve_dataset_inputs(
13201321

13211322

13221323
def _resolve_evaluation_run_metrics(
1323-
metrics: list[types.EvaluationRunMetric], api_client: Any
1324+
metrics: list[Any], api_client: Any
13241325
) -> list[types.EvaluationRunMetric]:
13251326
"""Resolves a list of evaluation run metric instances, loading RubricMetric if necessary."""
13261327
if not metrics:
13271328
return []
13281329
resolved_metrics_list = []
13291330
for metric_instance in metrics:
1330-
if isinstance(metric_instance, types.EvaluationRunMetric):
1331-
resolved_metrics_list.append(metric_instance)
1332-
elif isinstance(
1331+
if isinstance(
13331332
metric_instance, _evals_metric_loaders.LazyLoadedPrebuiltMetric
13341333
):
13351334
try:
@@ -1353,6 +1352,18 @@ def _resolve_evaluation_run_metrics(
13531352
e,
13541353
)
13551354
raise
1355+
elif isinstance(metric_instance, types.EvaluationRunMetric):
1356+
resolved_metrics_list.append(metric_instance)
1357+
elif isinstance(metric_instance, types.Metric):
1358+
config_dict = t.t_metrics([metric_instance])[0]
1359+
res_name = config_dict.pop("metric_resource_name", None)
1360+
resolved_metrics_list.append(
1361+
types.EvaluationRunMetric(
1362+
metric=metric_instance.name,
1363+
metric_config=config_dict,
1364+
metric_resource_name=res_name,
1365+
)
1366+
)
13561367
else:
13571368
try:
13581369
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: 14 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

vertexai/_genai/types/common.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,6 +2326,9 @@ class LLMBasedMetricSpec(_common.BaseModel):
23262326
default=None,
23272327
description="""Dynamically generate rubrics using this specification.""",
23282328
)
2329+
metric_resource_name: Optional[str] = Field(
2330+
default=None, description="""The resource name of the metric definition. Example: projects/{project}/locations/{location}/evaluationMetrics/{evaluation_metric_id}"""
2331+
)
23292332

23302333

23312334
class LLMBasedMetricSpecDict(TypedDict, total=False):
@@ -2350,6 +2353,9 @@ class LLMBasedMetricSpecDict(TypedDict, total=False):
23502353
rubric_generation_spec: Optional[RubricGenerationSpecDict]
23512354
"""Dynamically generate rubrics using this specification."""
23522355

2356+
metric_resource_name: Optional[str]
2357+
"""The resource name of the metric definition. Example: projects/{project}/locations/{location}/evaluationMetrics/{evaluation_metric_id}"""
2358+
23532359

23542360
LLMBasedMetricSpecOrDict = Union[LLMBasedMetricSpec, LLMBasedMetricSpecDict]
23552361

@@ -2482,6 +2488,9 @@ class EvaluationRunMetric(_common.BaseModel):
24822488
metric_config: Optional[UnifiedMetric] = Field(
24832489
default=None, description="""The unified metric used for evaluation run."""
24842490
)
2491+
metric_resource_name: Optional[str] = Field(
2492+
default=None, description="""The resource name of the metric definition. Example: projects/{project}/locations/{location}/evaluationMetrics/{evaluation_metric_id}"""
2493+
)
24852494

24862495

24872496
class EvaluationRunMetricDict(TypedDict, total=False):
@@ -2493,6 +2502,9 @@ class EvaluationRunMetricDict(TypedDict, total=False):
24932502
metric_config: Optional[UnifiedMetricDict]
24942503
"""The unified metric used for evaluation run."""
24952504

2505+
metric_resource_name: Optional[str]
2506+
"""The resource name of the metric definition. Example: projects/{project}/locations/{location}/evaluationMetrics/{evaluation_metric_id}"""
2507+
24962508

24972509
EvaluationRunMetricOrDict = Union[EvaluationRunMetric, EvaluationRunMetricDict]
24982510

@@ -4439,6 +4451,9 @@ class Metric(_common.BaseModel):
44394451
default=None,
44404452
description="""Optional steering instruction parameters for the automated predefined metric.""",
44414453
)
4454+
metric_resource_name: Optional[str] = Field(
4455+
default=None, description="""The resource name of the metric definition. Example: projects/{project}/locations/{location}/evaluationMetrics/{evaluation_metric_id}"""
4456+
)
44424457

44434458
# Allow extra fields to support metric-specific config fields.
44444459
model_config = ConfigDict(extra="allow")
@@ -4643,6 +4658,9 @@ class MetricDict(TypedDict, total=False):
46434658
metric_spec_parameters: Optional[dict[str, Any]]
46444659
"""Optional steering instruction parameters for the automated predefined metric."""
46454660

4661+
metric_resource_name: Optional[str]
4662+
"""The resource name of the metric definition. Example: projects/{project}/locations/{location}/evaluationMetrics/{evaluation_metric_id}"""
4663+
46464664

46474665
MetricOrDict = Union[Metric, MetricDict]
46484666

0 commit comments

Comments
 (0)