Skip to content

Commit 955632c

Browse files
ankursharmascopybara-github
authored andcommitted
feat:Allow agent evaluation from modules ending in ".agent"
PiperOrigin-RevId: 822888194
1 parent 6d4d72a commit 955632c

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

src/google/adk/evaluation/agent_evaluator.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -478,19 +478,27 @@ async def _get_agent_for_eval(
478478
module_path = f"{module_name}"
479479
agent_module = importlib.import_module(module_path)
480480
print(dir(agent_module))
481-
if hasattr(agent_module, "agent"):
482-
if hasattr(agent_module.agent, "root_agent"):
483-
root_agent = agent_module.agent.root_agent
484-
elif hasattr(agent_module.agent, "get_agent_async"):
485-
root_agent, _ = await agent_module.agent.get_agent_async()
486-
else:
487-
raise ValueError(
488-
f"Module {module_name} does not have a root_agent or"
489-
" get_agent_async method."
490-
)
481+
482+
# One of the two things should be satisfied, either the module should have
483+
# an "agent" as a member in it or the module name itself should end with
484+
# ".agent".
485+
if not (hasattr(agent_module, "agent") or module_name.endswith(".agent")):
486+
raise ValueError(
487+
f"Module {module_name} does not have a member named `agent` or the"
488+
" name should endwith `.agent`."
489+
)
490+
491+
agent_module_with_agent = (
492+
agent_module.agent if hasattr(agent_module, "agent") else agent_module
493+
)
494+
if hasattr(agent_module_with_agent, "root_agent"):
495+
root_agent = agent_module_with_agent.root_agent
496+
elif hasattr(agent_module_with_agent, "get_agent_async"):
497+
root_agent, _ = await agent_module_with_agent.get_agent_async()
491498
else:
492499
raise ValueError(
493-
f"Module {module_name} does not have a member named `agent`."
500+
f"Module {module_name} does not have a root_agent or"
501+
" get_agent_async method."
494502
)
495503

496504
agent_for_eval = root_agent

tests/integration/test_single_agent.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ async def test_eval_agent():
2525
)
2626

2727

28+
@pytest.mark.asyncio
29+
async def test_eval_agent_with_agent_suffix_in_module_name():
30+
await AgentEvaluator.evaluate(
31+
agent_module="tests.integration.fixture.home_automation_agent.agent",
32+
eval_dataset_file_path_or_dir="tests/integration/fixture/home_automation_agent/simple_test.test.json",
33+
num_runs=4,
34+
)
35+
36+
2837
@pytest.mark.asyncio
2938
async def test_eval_agent_async():
3039
await AgentEvaluator.evaluate(

0 commit comments

Comments
 (0)