Skip to content

Commit e7d9cf3

Browse files
seanzhougooglecopybara-github
authored andcommitted
make eval functions async
PiperOrigin-RevId: 756106627
1 parent cc1ef3f commit e7d9cf3

11 files changed

Lines changed: 50 additions & 36 deletions

‎src/google/adk/cli/cli_eval.py‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import sys
2121
import traceback
2222
from typing import Any
23-
from typing import Generator
23+
from typing import AsyncGenerator
2424
from typing import Optional
2525
import uuid
2626

@@ -146,15 +146,15 @@ def parse_and_get_evals_to_run(
146146
return eval_set_to_evals
147147

148148

149-
def run_evals(
149+
async def run_evals(
150150
eval_set_to_evals: dict[str, list[str]],
151151
root_agent: Agent,
152152
reset_func: Optional[Any],
153153
eval_metrics: list[EvalMetric],
154154
session_service=None,
155155
artifact_service=None,
156156
print_detailed_results=False,
157-
) -> Generator[EvalResult, None, None]:
157+
) -> AsyncGenerator[EvalResult, None]:
158158
try:
159159
from ..evaluation.agent_evaluator import EvaluationGenerator
160160
from ..evaluation.response_evaluator import ResponseEvaluator
@@ -181,14 +181,16 @@ def run_evals(
181181
print(f"Running Eval: {eval_set_file}:{eval_name}")
182182
session_id = f"{EVAL_SESSION_ID_PREFIX}{str(uuid.uuid4())}"
183183

184-
scrape_result = EvaluationGenerator._process_query_with_root_agent(
185-
data=eval_data,
186-
root_agent=root_agent,
187-
reset_func=reset_func,
188-
initial_session=initial_session,
189-
session_id=session_id,
190-
session_service=session_service,
191-
artifact_service=artifact_service,
184+
scrape_result = (
185+
await EvaluationGenerator._process_query_with_root_agent(
186+
data=eval_data,
187+
root_agent=root_agent,
188+
reset_func=reset_func,
189+
initial_session=initial_session,
190+
session_id=session_id,
191+
session_service=session_service,
192+
artifact_service=artifact_service,
193+
)
192194
)
193195

194196
eval_metric_results = []

‎src/google/adk/cli/cli_tools_click.py‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,14 @@ def cli_eval(
258258

259259
try:
260260
eval_results = list(
261-
run_evals(
262-
eval_set_to_evals,
263-
root_agent,
264-
reset_func,
265-
eval_metrics,
266-
print_detailed_results=print_detailed_results,
261+
asyncio.run(
262+
run_evals(
263+
eval_set_to_evals,
264+
root_agent,
265+
reset_func,
266+
eval_metrics,
267+
print_detailed_results=print_detailed_results,
268+
)
267269
)
268270
)
269271
except ModuleNotFoundError:

‎src/google/adk/cli/fast_api.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ async def run_eval(
467467
)
468468
root_agent = await _get_root_agent_async(app_name)
469469
eval_results = list(
470-
run_evals(
470+
await run_evals(
471471
eval_set_to_evals,
472472
root_agent,
473473
getattr(root_agent, "reset_data", None),

‎src/google/adk/evaluation/agent_evaluator.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def find_config_for_test_file(test_file: str):
7676
return DEFAULT_CRITERIA
7777

7878
@staticmethod
79-
def evaluate(
79+
async def evaluate(
8080
agent_module,
8181
eval_dataset_file_path_or_dir,
8282
num_runs=NUM_RUNS,
@@ -120,7 +120,7 @@ def evaluate(
120120

121121
AgentEvaluator._validate_input([dataset], criteria)
122122

123-
evaluation_response = AgentEvaluator._generate_responses(
123+
evaluation_response = await AgentEvaluator._generate_responses(
124124
agent_module,
125125
[dataset],
126126
num_runs,
@@ -246,7 +246,7 @@ def _get_infer_criteria(eval_dataset):
246246
return inferred_criteria
247247

248248
@staticmethod
249-
def _generate_responses(
249+
async def _generate_responses(
250250
agent_module, eval_dataset, num_runs, agent_name=None, initial_session={}
251251
):
252252
"""Generates evaluation responses by running the agent module multiple times."""

‎src/google/adk/evaluation/evaluation_generator.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class EvaluationGenerator:
3232
"""Generates evaluation responses for agents."""
3333

3434
@staticmethod
35-
def generate_responses(
35+
async def generate_responses(
3636
eval_dataset,
3737
agent_module_path,
3838
repeat_num=3,
@@ -107,7 +107,7 @@ def _process_query(data, module_name, agent_name=None, initial_session={}):
107107
)
108108

109109
@staticmethod
110-
def _process_query_with_root_agent(
110+
async def _process_query_with_root_agent(
111111
data,
112112
root_agent,
113113
reset_func,
@@ -128,7 +128,7 @@ def _process_query_with_root_agent(
128128
all_mock_tools.add(expected[EvalConstants.TOOL_NAME])
129129

130130
eval_data_copy = data.copy()
131-
EvaluationGenerator.apply_before_tool_callback(
131+
await EvaluationGenerator.apply_before_tool_callback(
132132
root_agent,
133133
lambda *args: EvaluationGenerator.before_tool_callback(
134134
*args, eval_dataset=eval_data_copy
@@ -247,7 +247,7 @@ def before_tool_callback(tool, args, tool_context, eval_dataset):
247247
return None
248248

249249
@staticmethod
250-
def apply_before_tool_callback(
250+
async def apply_before_tool_callback(
251251
agent: BaseAgent,
252252
callback: BeforeToolCallback,
253253
all_mock_tools: set[str],
@@ -265,6 +265,6 @@ def apply_before_tool_callback(
265265

266266
# Apply recursively to subagents if they exist
267267
for sub_agent in agent.sub_agents:
268-
EvaluationGenerator.apply_before_tool_callback(
268+
await EvaluationGenerator.apply_before_tool_callback(
269269
sub_agent, callback, all_mock_tools
270270
)

‎tests/integration/test_evalute_agent_in_fixture.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,13 @@ def agent_eval_artifacts_in_fixture():
5151
return agent_eval_artifacts
5252

5353

54+
@pytest.mark.asyncio
5455
@pytest.mark.parametrize(
5556
'agent_name, evalfile, initial_session_file',
5657
agent_eval_artifacts_in_fixture(),
5758
ids=[agent_name for agent_name, _, _ in agent_eval_artifacts_in_fixture()],
5859
)
59-
def test_evaluate_agents_long_running_4_runs_per_eval_item(
60+
async def test_evaluate_agents_long_running_4_runs_per_eval_item(
6061
agent_name, evalfile, initial_session_file
6162
):
6263
"""Test agents evaluation in fixture folder.
@@ -66,7 +67,7 @@ def test_evaluate_agents_long_running_4_runs_per_eval_item(
6667
6768
A single eval item is a session that can have multiple queries in it.
6869
"""
69-
AgentEvaluator.evaluate(
70+
await AgentEvaluator.evaluate(
7071
agent_module=agent_name,
7172
eval_dataset_file_path_or_dir=evalfile,
7273
initial_session_file=initial_session_file,

‎tests/integration/test_multi_agent.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
from google.adk.evaluation import AgentEvaluator
1616

1717

18-
def test_eval_agent():
18+
@pytest.mark.asyncio
19+
async def test_eval_agent():
1920
AgentEvaluator.evaluate(
2021
agent_module="tests.integration.fixture.trip_planner_agent",
2122
eval_dataset_file_path_or_dir=(

‎tests/integration/test_multi_turn.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
from google.adk.evaluation import AgentEvaluator
1616

1717

18-
def test_simple_multi_turn_conversation():
18+
@pytest.mark.asyncio
19+
async def test_simple_multi_turn_conversation():
1920
"""Test a simple multi-turn conversation."""
2021
AgentEvaluator.evaluate(
2122
agent_module="tests.integration.fixture.home_automation_agent",
@@ -24,7 +25,8 @@ def test_simple_multi_turn_conversation():
2425
)
2526

2627

27-
def test_dependent_tool_calls():
28+
@pytest.mark.asyncio
29+
async def test_dependent_tool_calls():
2830
"""Test subsequent tool calls that are dependent on previous tool calls."""
2931
AgentEvaluator.evaluate(
3032
agent_module="tests.integration.fixture.home_automation_agent",
@@ -33,8 +35,10 @@ def test_dependent_tool_calls():
3335
)
3436

3537

36-
def test_memorizing_past_events():
38+
@pytest.mark.asyncio
39+
async def test_memorizing_past_events():
3740
"""Test memorizing past events."""
41+
3842
AgentEvaluator.evaluate(
3943
agent_module="tests.integration.fixture.home_automation_agent",
4044
eval_dataset_file_path_or_dir="tests/integration/fixture/home_automation_agent/test_files/memorizing_past_events/eval_data.test.json",

‎tests/integration/test_single_agent.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
from google.adk.evaluation import AgentEvaluator
1616

1717

18-
def test_eval_agent():
18+
@pytest.mark.asyncio
19+
async def test_eval_agent():
1920
AgentEvaluator.evaluate(
2021
agent_module="tests.integration.fixture.home_automation_agent",
2122
eval_dataset_file_path_or_dir="tests/integration/fixture/home_automation_agent/simple_test.test.json",

‎tests/integration/test_sub_agent.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
from google.adk.evaluation import AgentEvaluator
1616

1717

18-
def test_eval_agent():
18+
@pytest.mark.asyncio
19+
async def test_eval_agent():
1920
"""Test hotel sub agent in a multi-agent system."""
2021
AgentEvaluator.evaluate(
2122
agent_module="tests.integration.fixture.trip_planner_agent",

0 commit comments

Comments
 (0)