-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
8
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
8 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 9f497a0
style: Fix import
ftnext 30d54df
Merge branch 'main' into agent-evaluator-support-custom-metric
ftnext 33f3626
Merge branch 'main' into agent-evaluator-support-custom-metric
ftnext d15e78c
Merge branch 'main' into agent-evaluator-support-custom-metric
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
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.
Uh oh!
There was an error while loading. Please reload this page.