Skip to content

Commit 86097af

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: Update AgentEvaluator to handle async ADK agent definitions
AgentEvaluator should recognize root_agent and get_agent_async as valid structures for ADK agent definitions. PiperOrigin-RevId: 819976635
1 parent a17f3b2 commit 86097af

File tree

6 files changed

+210
-5
lines changed

6 files changed

+210
-5
lines changed

src/google/adk/evaluation/agent_evaluator.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ async def evaluate_eval_set(
118118
Args:
119119
agent_module: The path to python module that contains the definition of
120120
the agent. There is convention in place here, where the code is going to
121-
look for 'root_agent' in the loaded module.
121+
look for 'root_agent' or `get_agent_async` in the loaded module.
122122
eval_set: The eval set.
123123
criteria: Evauation criterias, a dictionary of metric names to their
124124
respective thresholds. This field is deprecated.
@@ -144,7 +144,7 @@ async def evaluate_eval_set(
144144
if eval_config is None:
145145
raise ValueError("`eval_config` is required.")
146146

147-
agent_for_eval = AgentEvaluator._get_agent_for_eval(
147+
agent_for_eval = await AgentEvaluator._get_agent_for_eval(
148148
module_name=agent_module, agent_name=agent_name
149149
)
150150
eval_metrics = get_eval_metrics_from_config(eval_config)
@@ -200,7 +200,7 @@ async def evaluate(
200200
Args:
201201
agent_module: The path to python module that contains the definition of
202202
the agent. There is convention in place here, where the code is going to
203-
look for 'root_agent' in the loaded module.
203+
look for 'root_agent' or 'get_agent_async' in the loaded module.
204204
eval_dataset_file_path_or_dir: The eval data set. This can be either a
205205
string representing full path to the file containing eval dataset, or a
206206
directory that is recursively explored for all files that have a
@@ -466,12 +466,26 @@ def _convert_tool_calls_to_text(
466466
return "\n".join([str(t) for t in tool_calls])
467467

468468
@staticmethod
469-
def _get_agent_for_eval(
469+
async def _get_agent_for_eval(
470470
module_name: str, agent_name: Optional[str] = None
471471
) -> BaseAgent:
472472
module_path = f"{module_name}"
473473
agent_module = importlib.import_module(module_path)
474-
root_agent = agent_module.agent.root_agent
474+
print(dir(agent_module))
475+
if hasattr(agent_module, "agent"):
476+
if hasattr(agent_module.agent, "root_agent"):
477+
root_agent = agent_module.agent.root_agent
478+
elif hasattr(agent_module.agent, "get_agent_async"):
479+
root_agent, _ = await agent_module.agent.get_agent_async()
480+
else:
481+
raise ValueError(
482+
f"Module {module_name} does not have a root_agent or"
483+
" get_agent_async method."
484+
)
485+
else:
486+
raise ValueError(
487+
f"Module {module_name} does not have a member named `agent`."
488+
)
475489

476490
agent_for_eval = root_agent
477491
if agent_name:
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from . import agent
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Hello world agent from agent 1.0 revised to be defined with get_agent_async
16+
# instead of root_agent - https://colab.sandbox.google.com/drive/1Zq-nqmgK0nCERCv8jKIaoeTTgbNn6oSo?resourcekey=0-GYaz9pFT4wY8CI8Cvjy5GA#scrollTo=u3X3XwDOaCv9
17+
import contextlib
18+
import random
19+
from typing import Optional
20+
21+
from google.adk import Agent
22+
from google.adk.agents import llm_agent
23+
from google.genai import types
24+
25+
26+
def roll_die(sides: int) -> int:
27+
"""Roll a die and return the rolled result.
28+
29+
Args:
30+
sides: The integer number of sides the die has.
31+
32+
Returns:
33+
An integer of the result of rolling the die.
34+
"""
35+
return random.randint(1, sides)
36+
37+
38+
def check_prime(nums: list[int]) -> list[str]:
39+
"""Check if a given list of numbers are prime.
40+
41+
Args:
42+
nums: The list of numbers to check.
43+
44+
Returns:
45+
A str indicating which number is prime.
46+
"""
47+
primes = set()
48+
for number in nums:
49+
number = int(number)
50+
if number <= 1:
51+
continue
52+
is_prime = True
53+
for i in range(2, int(number**0.5) + 1):
54+
if number % i == 0:
55+
is_prime = False
56+
break
57+
if is_prime:
58+
primes.add(number)
59+
return (
60+
'No prime numbers found.'
61+
if not primes
62+
else f"{', '.join(str(num) for num in primes)} are prime numbers."
63+
)
64+
65+
66+
async def get_agent_async() -> (
67+
tuple[llm_agent.LlmAgent, Optional[contextlib.AsyncExitStack]]
68+
):
69+
"""Returns the root agent."""
70+
root_agent = Agent(
71+
model='gemini-2.0-flash-001',
72+
name='data_processing_agent',
73+
instruction="""
74+
You roll dice and answer questions about the outcome of the dice rolls.
75+
You can roll dice of different sizes.
76+
You can use multiple tools in parallel by calling functions in parallel(in one request and in one round).
77+
The only things you do are roll dice for the user and discuss the outcomes.
78+
It is ok to discuss previous dice roles, and comment on the dice rolls.
79+
When you are asked to roll a die, you must call the roll_die tool with the number of sides. Be sure to pass in an integer. Do not pass in a string.
80+
You should never roll a die on your own.
81+
When checking prime numbers, call the check_prime tool with a list of integers. Be sure to pass in a list of integers. You should never pass in a string.
82+
You should not check prime numbers before calling the tool.
83+
When you are asked to roll a die and check prime numbers, you should always make the following two function calls:
84+
1. You should first call the roll_die tool to get a roll. Wait for the function response before calling the check_prime tool.
85+
2. After you get the function response from roll_die tool, you should call the check_prime tool with the roll_die result.
86+
2.1 If user asks you to check primes based on previous rolls, make sure you include the previous rolls in the list.
87+
3. When you respond, you must include the roll_die result from step 1.
88+
You should always perform the previous 3 steps when asking for a roll and checking prime numbers.
89+
You should not rely on the previous history on prime results.
90+
""",
91+
tools=[
92+
roll_die,
93+
check_prime,
94+
],
95+
generate_content_config=types.GenerateContentConfig(
96+
safety_settings=[
97+
types.SafetySetting( # avoid false alarm about rolling dice.
98+
category=types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
99+
threshold=types.HarmBlockThreshold.OFF,
100+
),
101+
]
102+
),
103+
)
104+
return root_agent, None
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"eval_set_id": "56540925-a5ff-49fe-a4e1-589fe78066f2",
3+
"name": "56540925-a5ff-49fe-a4e1-589fe78066f2",
4+
"description": null,
5+
"eval_cases": [
6+
{
7+
"eval_id": "tests/integration/fixture/hello_world_agent_async/roll_die.test.json",
8+
"conversation": [
9+
{
10+
"invocation_id": "b01f67f0-9f23-44d6-bbe4-36ea235cb9fb",
11+
"user_content": {
12+
"parts": [
13+
{
14+
"video_metadata": null,
15+
"thought": null,
16+
"code_execution_result": null,
17+
"executable_code": null,
18+
"file_data": null,
19+
"function_call": null,
20+
"function_response": null,
21+
"inline_data": null,
22+
"text": "Hi who are you?"
23+
}
24+
],
25+
"role": "user"
26+
},
27+
"final_response": {
28+
"parts": [
29+
{
30+
"video_metadata": null,
31+
"thought": null,
32+
"code_execution_result": null,
33+
"executable_code": null,
34+
"file_data": null,
35+
"function_call": null,
36+
"function_response": null,
37+
"inline_data": null,
38+
"text": "I am a data processing agent. I can roll dice and check if the results are prime numbers. What would you like me to do? \n"
39+
}
40+
],
41+
"role": "model"
42+
},
43+
"intermediate_data": {
44+
"tool_uses": [],
45+
"intermediate_responses": []
46+
},
47+
"creation_timestamp": 1747341775.8937013
48+
}
49+
],
50+
"session_input": null,
51+
"creation_timestamp": 1747341775.8937826
52+
}
53+
],
54+
"creation_timestamp": 1747341775.8937957
55+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"criteria": {
3+
"tool_trajectory_avg_score": 1.0,
4+
"response_match_score": 0.5
5+
}
6+
}

tests/integration/test_single_agent.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,14 @@ async def test_eval_agent():
2323
eval_dataset_file_path_or_dir="tests/integration/fixture/home_automation_agent/simple_test.test.json",
2424
num_runs=4,
2525
)
26+
27+
28+
@pytest.mark.asyncio
29+
async def test_eval_agent_async():
30+
await AgentEvaluator.evaluate(
31+
agent_module="tests.integration.fixture.hello_world_agent_async",
32+
eval_dataset_file_path_or_dir=(
33+
"tests/integration/fixture/hello_world_agent_async/roll_die.test.json"
34+
),
35+
num_runs=4,
36+
)

0 commit comments

Comments
 (0)