-
Notifications
You must be signed in to change notification settings - Fork 2.8k
AgentEvaluator support custom metric #4344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ftnext
wants to merge
4
commits into
google:main
Choose a base branch
from
ftnext:agent-evaluator-support-custom-metric
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8d00362
feat(eval): Support custom metrics in AgentEvaluator
ftnext 9446332
refactor(eval): Extract default metric info helper
ftnext 2985224
test(integration): Add custom metric example eval
ftnext 2afcec1
fix(eval): Isolate metric evaluator registry per instance
ftnext File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,6 +50,9 @@ | |
| from .evaluator import EvalStatus | ||
| from .in_memory_eval_sets_manager import InMemoryEvalSetsManager | ||
| from .local_eval_sets_manager import convert_eval_set_to_pydantic_schema | ||
| from .metric_defaults import get_default_metric_info | ||
| from .metric_evaluator_registry import _get_default_metric_evaluator_registry | ||
| from .metric_evaluator_registry import MetricEvaluatorRegistry | ||
| from .simulation.user_simulator_provider import UserSimulatorProvider | ||
|
|
||
| logger = logging.getLogger("google_adk." + __name__) | ||
|
|
@@ -154,13 +157,30 @@ async def evaluate_eval_set( | |
| user_simulator_config=eval_config.user_simulator_config | ||
| ) | ||
|
|
||
| metric_evaluator_registry = _get_default_metric_evaluator_registry() | ||
| if eval_config.custom_metrics: | ||
| from .custom_metric_evaluator import _CustomMetricEvaluator | ||
|
|
||
|
Comment on lines
+162
to
+163
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| for metric_name, config in eval_config.custom_metrics.items(): | ||
| if config.metric_info: | ||
| metric_info = config.metric_info.model_copy() | ||
| metric_info.metric_name = metric_name | ||
| else: | ||
| metric_info = get_default_metric_info( | ||
| metric_name=metric_name, description=config.description | ||
| ) | ||
| metric_evaluator_registry.register_evaluator( | ||
| metric_info, _CustomMetricEvaluator | ||
| ) | ||
|
|
||
| # Step 1: Perform evals, basically inferencing and evaluation of metrics | ||
| eval_results_by_eval_id = await AgentEvaluator._get_eval_results_by_eval_id( | ||
| agent_for_eval=agent_for_eval, | ||
| eval_set=eval_set, | ||
| eval_metrics=eval_metrics, | ||
| num_runs=num_runs, | ||
| user_simulator_provider=user_simulator_provider, | ||
| metric_evaluator_registry=metric_evaluator_registry, | ||
| ) | ||
|
|
||
| # Step 2: Post-process the results! | ||
|
|
@@ -536,6 +556,7 @@ async def _get_eval_results_by_eval_id( | |
| eval_metrics: list[EvalMetric], | ||
| num_runs: int, | ||
| user_simulator_provider: UserSimulatorProvider, | ||
| metric_evaluator_registry: Optional[MetricEvaluatorRegistry] = None, | ||
| ) -> dict[str, list[EvalCaseResult]]: | ||
| """Returns EvalCaseResults grouped by eval case id. | ||
|
|
||
|
|
@@ -560,6 +581,7 @@ async def _get_eval_results_by_eval_id( | |
| app_name=app_name, eval_set=eval_set | ||
| ), | ||
| user_simulator_provider=user_simulator_provider, | ||
| metric_evaluator_registry=metric_evaluator_registry, | ||
| ) | ||
|
|
||
| inference_requests = [ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from .eval_metrics import Interval | ||
| from .eval_metrics import MetricInfo | ||
| from .eval_metrics import MetricValueInfo | ||
|
|
||
|
|
||
| def get_default_metric_info( | ||
| metric_name: str, description: str = "" | ||
| ) -> MetricInfo: | ||
| """Returns a default MetricInfo for a metric.""" | ||
| return MetricInfo( | ||
| metric_name=metric_name, | ||
| description=description, | ||
| metric_value_info=MetricValueInfo( | ||
| interval=Interval(min_value=0.0, max_value=1.0) | ||
| ), | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
tests/integration/fixture/home_automation_agent/test_files/custom_metrics/metrics.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Optional | ||
|
|
||
| from google.adk.evaluation.eval_case import ConversationScenario | ||
| from google.adk.evaluation.eval_case import get_all_tool_calls | ||
| from google.adk.evaluation.eval_case import Invocation | ||
| from google.adk.evaluation.eval_metrics import EvalMetric | ||
| from google.adk.evaluation.eval_metrics import EvalStatus | ||
| from google.adk.evaluation.evaluator import EvaluationResult | ||
| from google.adk.evaluation.evaluator import PerInvocationResult | ||
|
|
||
|
|
||
| def tool_trajectory_length_match( | ||
| eval_metric: EvalMetric, | ||
| actual_invocations: list[Invocation], | ||
| expected_invocations: Optional[list[Invocation]] = None, | ||
| conversation_scenario: Optional[ConversationScenario] = None, | ||
| ) -> EvaluationResult: | ||
| del eval_metric | ||
| del conversation_scenario | ||
| expected_invocations = expected_invocations or [] | ||
|
|
||
| per_invocation_results = [] | ||
| for idx, actual in enumerate(actual_invocations): | ||
| expected = ( | ||
| expected_invocations[idx] if idx < len(expected_invocations) else None | ||
| ) | ||
| actual_tools = get_all_tool_calls(actual.intermediate_data) | ||
| expected_tools = ( | ||
| get_all_tool_calls(expected.intermediate_data) if expected else [] | ||
| ) | ||
| match = len(actual_tools) == len(expected_tools) | ||
| per_invocation_results.append( | ||
| PerInvocationResult( | ||
| actual_invocation=actual, | ||
| expected_invocation=expected, | ||
| score=1.0 if match else 0.0, | ||
| eval_status=EvalStatus.PASSED if match else EvalStatus.FAILED, | ||
| ) | ||
| ) | ||
|
|
||
| overall_score = ( | ||
| sum(r.score for r in per_invocation_results) / len(per_invocation_results) | ||
| if per_invocation_results | ||
| else 0.0 | ||
| ) | ||
| overall_eval_status = ( | ||
| EvalStatus.PASSED if overall_score == 1.0 else EvalStatus.FAILED | ||
| ) | ||
| return EvaluationResult( | ||
| overall_score=overall_score, | ||
| overall_eval_status=overall_eval_status, | ||
| per_invocation_results=per_invocation_results, | ||
| ) |
65 changes: 65 additions & 0 deletions
65
...on/fixture/home_automation_agent/test_files/custom_metrics/simple_custom_metric.test.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| { | ||
| "eval_set_id": "custom_metrics_eval_set", | ||
| "name": "custom_metrics_eval_set", | ||
| "description": "Custom metric evaluation sample.", | ||
| "eval_cases": [ | ||
| { | ||
| "eval_id": "tests/integration/fixture/home_automation_agent/test_files/custom_metrics/simple_custom_metric.test.json", | ||
| "conversation": [ | ||
| { | ||
| "invocation_id": "a9e4f840-7f1e-4b69-b9c1-3b85c03a60a4", | ||
| "user_content": { | ||
| "parts": [ | ||
| { | ||
| "video_metadata": null, | ||
| "thought": null, | ||
| "code_execution_result": null, | ||
| "executable_code": null, | ||
| "file_data": null, | ||
| "function_call": null, | ||
| "function_response": null, | ||
| "inline_data": null, | ||
| "text": "Turn off device_2 in the Bedroom." | ||
| } | ||
| ], | ||
| "role": "user" | ||
| }, | ||
| "final_response": { | ||
| "parts": [ | ||
| { | ||
| "video_metadata": null, | ||
| "thought": null, | ||
| "code_execution_result": null, | ||
| "executable_code": null, | ||
| "file_data": null, | ||
| "function_call": null, | ||
| "function_response": null, | ||
| "inline_data": null, | ||
| "text": "I have set the device_2 status to off." | ||
| } | ||
| ], | ||
| "role": "model" | ||
| }, | ||
| "intermediate_data": { | ||
| "tool_uses": [ | ||
| { | ||
| "id": null, | ||
| "args": { | ||
| "location": "Bedroom", | ||
| "device_id": "device_2", | ||
| "status": "OFF" | ||
| }, | ||
| "name": "set_device_info" | ||
| } | ||
| ], | ||
| "intermediate_responses": [] | ||
| }, | ||
| "creation_timestamp": 1747337309.2360144 | ||
| } | ||
| ], | ||
| "session_input": null, | ||
| "creation_timestamp": 1747337309.2360282 | ||
| } | ||
| ], | ||
| "creation_timestamp": 1747337309.2360387 | ||
| } |
13 changes: 13 additions & 0 deletions
13
tests/integration/fixture/home_automation_agent/test_files/custom_metrics/test_config.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "criteria": { | ||
| "tool_trajectory_length_match": 1.0 | ||
| }, | ||
| "custom_metrics": { | ||
| "tool_trajectory_length_match": { | ||
| "code_config": { | ||
| "name": "tests.integration.fixture.home_automation_agent.test_files.custom_metrics.metrics.tool_trajectory_length_match" | ||
| }, | ||
| "description": "Checks that actual and expected tool trajectories have the same length." | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To adhere to PEP 8 guidelines and improve code readability, it's best practice to place all imports at the top of the file. The
_CustomMetricEvaluatoris currently imported inline within theevaluate_eval_setmethod. Please add the import here and remove the inline version.