feat:Allow agent evaluation from modules ending in ".agent"

PiperOrigin-RevId: 822888194
This commit is contained in:
Ankur Sharma
2025-10-22 23:11:48 -07:00
committed by Copybara-Service
parent 6d4d72a499
commit 955632ce2c
2 changed files with 28 additions and 11 deletions
+19 -11
View File
@@ -478,19 +478,27 @@ class AgentEvaluator:
module_path = f"{module_name}" module_path = f"{module_name}"
agent_module = importlib.import_module(module_path) agent_module = importlib.import_module(module_path)
print(dir(agent_module)) print(dir(agent_module))
if hasattr(agent_module, "agent"):
if hasattr(agent_module.agent, "root_agent"): # One of the two things should be satisfied, either the module should have
root_agent = agent_module.agent.root_agent # an "agent" as a member in it or the module name itself should end with
elif hasattr(agent_module.agent, "get_agent_async"): # ".agent".
root_agent, _ = await agent_module.agent.get_agent_async() if not (hasattr(agent_module, "agent") or module_name.endswith(".agent")):
else: raise ValueError(
raise ValueError( f"Module {module_name} does not have a member named `agent` or the"
f"Module {module_name} does not have a root_agent or" " name should endwith `.agent`."
" get_agent_async method." )
)
agent_module_with_agent = (
agent_module.agent if hasattr(agent_module, "agent") else agent_module
)
if hasattr(agent_module_with_agent, "root_agent"):
root_agent = agent_module_with_agent.root_agent
elif hasattr(agent_module_with_agent, "get_agent_async"):
root_agent, _ = await agent_module_with_agent.get_agent_async()
else: else:
raise ValueError( raise ValueError(
f"Module {module_name} does not have a member named `agent`." f"Module {module_name} does not have a root_agent or"
" get_agent_async method."
) )
agent_for_eval = root_agent agent_for_eval = root_agent
+9
View File
@@ -25,6 +25,15 @@ async def test_eval_agent():
) )
@pytest.mark.asyncio
async def test_eval_agent_with_agent_suffix_in_module_name():
await AgentEvaluator.evaluate(
agent_module="tests.integration.fixture.home_automation_agent.agent",
eval_dataset_file_path_or_dir="tests/integration/fixture/home_automation_agent/simple_test.test.json",
num_runs=4,
)
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_eval_agent_async(): async def test_eval_agent_async():
await AgentEvaluator.evaluate( await AgentEvaluator.evaluate(