-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathtest_evaluation_generator.py
More file actions
527 lines (453 loc) · 19.1 KB
/
test_evaluation_generator.py
File metadata and controls
527 lines (453 loc) · 19.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# 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 google.adk.evaluation.app_details import AgentDetails
from google.adk.evaluation.app_details import AppDetails
from google.adk.evaluation.evaluation_generator import EvaluationGenerator
from google.adk.evaluation.request_intercepter_plugin import _RequestIntercepterPlugin
from google.adk.evaluation.simulation.user_simulator import NextUserMessage
from google.adk.evaluation.simulation.user_simulator import Status as UserSimulatorStatus
from google.adk.evaluation.simulation.user_simulator import UserSimulator
from google.adk.events.event import Event
from google.adk.models.llm_request import LlmRequest
from google.genai import types
import pytest
def _build_event(
author: str, parts: list[types.Part], invocation_id: str
) -> Event:
"""Builds an Event object with specified parts."""
return Event(
author=author,
content=types.Content(parts=parts),
invocation_id=invocation_id,
)
class TestConvertEventsToEvalInvocation:
"""Test cases for EvaluationGenerator.convert_events_to_eval_invocations method."""
def test_convert_events_to_eval_invocations_empty(
self,
):
"""Tests conversion with an empty list of events."""
invocations = EvaluationGenerator.convert_events_to_eval_invocations([])
assert invocations == []
def test_convert_single_turn_text_only(
self,
):
"""Tests a single turn with a text response."""
events = [
_build_event("user", [types.Part(text="Hello")], "inv1"),
_build_event("agent", [types.Part(text="Hi there!")], "inv1"),
]
invocations = EvaluationGenerator.convert_events_to_eval_invocations(events)
assert len(invocations) == 1
invocation = invocations[0]
assert invocation.invocation_id == "inv1"
assert invocation.user_content.parts[0].text == "Hello"
assert invocation.final_response.parts[0].text == "Hi there!"
assert len(invocation.intermediate_data.invocation_events) == 0
def test_convert_single_turn_tool_call(
self,
):
"""Tests a single turn with a tool call."""
events = [
_build_event("user", [types.Part(text="what is the weather?")], "inv1"),
_build_event(
"agent",
[
types.Part(
function_call=types.FunctionCall(
name="get_weather", args={}
)
)
],
"inv1",
),
]
invocations = EvaluationGenerator.convert_events_to_eval_invocations(events)
assert len(invocations) == 1
invocation = invocations[0]
assert invocation.user_content.parts[0].text == "what is the weather?"
assert invocation.final_response is None
events = invocation.intermediate_data.invocation_events
assert len(events) == 1
assert events[0].author == "agent"
assert events[0].content.parts[0].function_call.name == "get_weather"
def test_convert_single_turn_tool_and_text_response(
self,
):
"""Tests a single turn with a tool call and a final text response."""
events = [
_build_event("user", [types.Part(text="what is the weather?")], "inv1"),
_build_event(
"agent",
[
types.Part(
function_call=types.FunctionCall(
name="get_weather", args={}
)
)
],
"inv1",
),
_build_event("agent", [types.Part(text="It is sunny in SF.")], "inv1"),
]
invocations = EvaluationGenerator.convert_events_to_eval_invocations(events)
assert len(invocations) == 1
invocation = invocations[0]
assert invocation.final_response.parts[0].text == "It is sunny in SF."
events = invocation.intermediate_data.invocation_events
assert len(events) == 1
assert events[0].content.parts[0].function_call.name == "get_weather"
def test_multi_turn(
self,
):
"""Tests a conversation with multiple turns."""
events = [
_build_event("user", [types.Part(text="Hello")], "inv1"),
_build_event("agent", [types.Part(text="Hi there!")], "inv1"),
_build_event("user", [types.Part(text="How are you?")], "inv2"),
_build_event("agent", [types.Part(text="I am fine.")], "inv2"),
]
invocations = EvaluationGenerator.convert_events_to_eval_invocations(events)
assert len(invocations) == 2
assert invocations[0].user_content.parts[0].text == "Hello"
assert invocations[0].final_response.parts[0].text == "Hi there!"
assert invocations[1].user_content.parts[0].text == "How are you?"
assert invocations[1].final_response.parts[0].text == "I am fine."
def test_multi_agent(
self,
):
"""Tests a multi-agent scenario creating multiple steps."""
events = [
_build_event("user", [types.Part(text="Do something")], "inv1"),
_build_event(
"root_agent",
[
types.Part(
function_call=types.FunctionCall(name="tool1", args={})
)
],
"inv1",
),
_build_event(
"sub_agent_1",
[
types.Part(
function_call=types.FunctionCall(name="tool2", args={})
)
],
"inv1",
),
_build_event(
"sub_agent_1",
[
types.Part(
function_call=types.FunctionCall(name="tool3", args={})
),
types.Part(text="intermediate response"),
],
"inv1",
),
_build_event(
"sub_agent_2",
[
types.Part(
function_call=types.FunctionCall(name="tool4", args={})
)
],
"inv1",
),
_build_event("root_agent", [types.Part(text="All done.")], "inv1"),
]
invocations = EvaluationGenerator.convert_events_to_eval_invocations(events)
assert len(invocations) == 1
invocation = invocations[0]
assert invocation.final_response.parts[0].text == "All done."
events = invocation.intermediate_data.invocation_events
assert len(events) == 4
assert events[0].author == "root_agent"
assert events[1].author == "sub_agent_1"
assert events[2].author == "sub_agent_1"
assert events[3].author == "sub_agent_2"
def test_convert_multi_agent_final_responses(
self,
):
"""Tests that only the last final response is excluded from intermediate data."""
events = [
_build_event("user", [types.Part(text="Hello")], "inv1"),
_build_event("agent1", [types.Part(text="First response")], "inv1"),
_build_event("agent2", [types.Part(text="Second response")], "inv1"),
]
invocations = EvaluationGenerator.convert_events_to_eval_invocations(events)
assert len(invocations) == 1
invocation = invocations[0]
assert invocation.final_response.parts[0].text == "Second response"
intermediate_events = invocation.intermediate_data.invocation_events
# agent1 is included because it is not the final_event (which is agent2)
assert len(intermediate_events) == 1
assert intermediate_events[0].author == "agent1"
assert intermediate_events[0].content.parts[0].text == "First response"
def test_invocation_without_user_event_is_skipped(self):
"""Invocations with no user-authored event must be skipped.
Regression test for https://github.com/google/adk-python/issues/3760.
When a session contains an invocation_id whose events are all authored by
agents or tools (no 'user' event), convert_events_to_eval_invocations used
to leave user_content as a bare string, causing a Pydantic ValidationError
from Invocation.user_content which requires genai_types.Content.
The fix skips such invocations because they represent internal/system-driven
turns that are not meaningful for evaluation.
"""
events = [
_build_event("agent", [types.Part(text="agent-only event")], "inv1"),
]
# Must not raise a Pydantic ValidationError.
invocations = EvaluationGenerator.convert_events_to_eval_invocations(events)
assert (
invocations == []
), "Invocations without a user event should be skipped."
def test_mixed_invocations_skips_only_agent_only_ones(self):
"""Only agent-only invocations are skipped; normal invocations are kept.
Regression test for https://github.com/google/adk-python/issues/3760.
"""
events = [
# inv1: normal user+agent turn — should be kept.
_build_event("user", [types.Part(text="Hello")], "inv1"),
_build_event("agent", [types.Part(text="Hi there!")], "inv1"),
# inv2: agent-only turn (e.g. background/system task) — should be skipped.
_build_event("agent", [types.Part(text="Internal work")], "inv2"),
# inv3: normal user+agent turn — should be kept.
_build_event("user", [types.Part(text="Follow-up")], "inv3"),
_build_event("agent", [types.Part(text="Sure!")], "inv3"),
]
invocations = EvaluationGenerator.convert_events_to_eval_invocations(events)
assert len(invocations) == 2
assert invocations[0].invocation_id == "inv1"
assert invocations[0].user_content.parts[0].text == "Hello"
assert invocations[1].invocation_id == "inv3"
assert invocations[1].user_content.parts[0].text == "Follow-up"
class TestGetAppDetailsByInvocationId:
"""Test cases for EvaluationGenerator._get_app_details_by_invocation_id method."""
def test_get_app_details_by_invocation_id_empty(self, mocker):
"""Tests with an empty list of events."""
mock_request_intercepter = mocker.MagicMock(spec=_RequestIntercepterPlugin)
app_details = EvaluationGenerator._get_app_details_by_invocation_id(
[], mock_request_intercepter
)
assert app_details == {}
def test_get_app_details_by_invocation_id_no_model_requests(self, mocker):
"""Tests when request_intercepter returns no model requests."""
mock_request_intercepter = mocker.MagicMock(spec=_RequestIntercepterPlugin)
mock_request_intercepter.get_model_request.return_value = None
events = [
_build_event("user", [types.Part(text="Hello")], "inv1"),
_build_event("agent", [types.Part(text="Hi there!")], "inv1"),
]
app_details = EvaluationGenerator._get_app_details_by_invocation_id(
events, mock_request_intercepter
)
assert app_details == {"inv1": AppDetails(agent_details={})}
mock_request_intercepter.get_model_request.assert_called_once_with(
events[1]
)
def test_get_app_details_single_invocation_single_agent(self, mocker):
"""Tests a single invocation with one agent."""
mock_request_intercepter = mocker.MagicMock(spec=_RequestIntercepterPlugin)
mock_llm_request = LlmRequest(model="test")
mock_llm_request.config.system_instruction = "instruction1"
mock_llm_request.config.tools = [types.Tool()]
mock_request_intercepter.get_model_request.return_value = mock_llm_request
events = [
_build_event("user", [types.Part(text="Hello")], "inv1"),
_build_event("agent", [types.Part(text="Hi there!")], "inv1"),
]
app_details = EvaluationGenerator._get_app_details_by_invocation_id(
events, mock_request_intercepter
)
expected_app_details = {
"inv1": AppDetails(
agent_details={
"agent": AgentDetails(
name="agent",
instructions="instruction1",
tool_declarations=[types.Tool()],
)
}
)
}
assert app_details == expected_app_details
mock_request_intercepter.get_model_request.assert_called_once_with(
events[1]
)
def test_get_app_details_multiple_invocations_multiple_agents(self, mocker):
"""Tests multiple invocations with multiple agents."""
mock_request_intercepter = mocker.MagicMock(spec=_RequestIntercepterPlugin)
def get_model_request_side_effect(event):
mock_llm_request = LlmRequest(model="test")
if event.invocation_id == "inv1" and event.author == "agent1":
mock_llm_request.config.system_instruction = "instruction1"
mock_llm_request.config.tools = [
types.Tool(
function_declarations=[types.FunctionDeclaration(name="tool1")]
)
]
return mock_llm_request
if event.invocation_id == "inv2" and event.author == "agent2":
mock_llm_request.config.system_instruction = "instruction2"
return mock_llm_request
return None
mock_request_intercepter.get_model_request.side_effect = (
get_model_request_side_effect
)
events = [
_build_event("user", [types.Part(text="Hello")], "inv1"),
_build_event("agent1", [types.Part(text="Hi there!")], "inv1"),
_build_event("user", [types.Part(text="Hello again")], "inv2"),
_build_event("agent2", [types.Part(text="Hi again!")], "inv2"),
_build_event(
"agent1", [types.Part(text="Hi again from agent1")], "inv2"
), # no request
]
app_details = EvaluationGenerator._get_app_details_by_invocation_id(
events, mock_request_intercepter
)
expected_app_details = {
"inv1": AppDetails(
agent_details={
"agent1": AgentDetails(
name="agent1",
instructions="instruction1",
tool_declarations=[
types.Tool(
function_declarations=[
types.FunctionDeclaration(name="tool1")
]
)
],
)
}
),
"inv2": AppDetails(
agent_details={
"agent2": AgentDetails(
name="agent2",
instructions="instruction2",
tool_declarations=[],
)
}
),
}
assert app_details == expected_app_details
assert mock_request_intercepter.get_model_request.call_count == 3
class TestGenerateInferencesForSingleUserInvocation:
"""Test cases for EvaluationGenerator._generate_inferences_for_single_user_invocation method."""
@pytest.mark.asyncio
async def test_generate_inferences_with_mock_runner(self, mocker):
"""Tests inference generation with a mocked runner."""
runner = mocker.MagicMock()
agent_parts = [types.Part(text="Agent response")]
async def mock_run_async(*args, **kwargs):
yield _build_event(
author="agent",
parts=agent_parts,
invocation_id="inv1",
)
runner.run_async.return_value = mock_run_async()
user_content = types.Content(parts=[types.Part(text="User query")])
events = [
event
async for event in (
EvaluationGenerator._generate_inferences_for_single_user_invocation(
runner, "test_user", "test_session", user_content
)
)
]
assert len(events) == 2
assert events[0].author == "user"
assert events[0].content == user_content
assert events[0].invocation_id == "inv1"
assert events[1].author == "agent"
assert events[1].content.parts == agent_parts
runner.run_async.assert_called_once_with(
user_id="test_user",
session_id="test_session",
new_message=user_content,
)
@pytest.fixture
def mock_runner(mocker):
"""Provides a mock Runner for testing."""
mock_runner_cls = mocker.patch(
"google.adk.evaluation.evaluation_generator.Runner"
)
mock_runner_instance = mocker.AsyncMock()
mock_runner_instance.__aenter__.return_value = mock_runner_instance
mock_runner_cls.return_value = mock_runner_instance
yield mock_runner_instance
@pytest.fixture
def mock_session_service(mocker):
"""Provides a mock InMemorySessionService for testing."""
mock_session_service_cls = mocker.patch(
"google.adk.evaluation.evaluation_generator.InMemorySessionService"
)
mock_session_service_instance = mocker.MagicMock()
mock_session_service_instance.create_session = mocker.AsyncMock()
mock_session_service_cls.return_value = mock_session_service_instance
yield mock_session_service_instance
class TestGenerateInferencesFromRootAgent:
"""Test cases for EvaluationGenerator._generate_inferences_from_root_agent method."""
@pytest.mark.asyncio
async def test_generates_inferences_with_user_simulator(
self, mocker, mock_runner, mock_session_service
):
"""Tests that inferences are generated by interacting with a user simulator."""
mock_agent = mocker.MagicMock()
mock_user_sim = mocker.MagicMock(spec=UserSimulator)
# Mock user simulator will produce one message, then stop.
async def get_next_user_message_side_effect(*args, **kwargs):
if mock_user_sim.get_next_user_message.call_count == 1:
return NextUserMessage(
status=UserSimulatorStatus.SUCCESS,
user_message=types.Content(parts=[types.Part(text="message 1")]),
)
return NextUserMessage(status=UserSimulatorStatus.STOP_SIGNAL_DETECTED)
mock_user_sim.get_next_user_message = mocker.AsyncMock(
side_effect=get_next_user_message_side_effect
)
mock_generate_inferences = mocker.patch(
"google.adk.evaluation.evaluation_generator.EvaluationGenerator._generate_inferences_for_single_user_invocation"
)
mocker.patch(
"google.adk.evaluation.evaluation_generator.EvaluationGenerator._get_app_details_by_invocation_id"
)
mocker.patch(
"google.adk.evaluation.evaluation_generator.EvaluationGenerator.convert_events_to_eval_invocations"
)
# Each call to _generate_inferences_for_single_user_invocation will
# yield one user and one agent event.
async def mock_generate_inferences_side_effect(
runner, user_id, session_id, user_content
):
yield _build_event("user", user_content.parts, "inv1")
yield _build_event("agent", [types.Part(text="agent_response")], "inv1")
mock_generate_inferences.side_effect = mock_generate_inferences_side_effect
await EvaluationGenerator._generate_inferences_from_root_agent(
root_agent=mock_agent,
user_simulator=mock_user_sim,
)
# Check that user simulator was called until it stopped.
assert mock_user_sim.get_next_user_message.call_count == 2
# Check that we generated inferences for each user message.
assert mock_generate_inferences.call_count == 1
# Check the content of the user messages passed to inference generation
mock_generate_inferences.assert_called_once()
called_with_content = mock_generate_inferences.call_args.args[3]
assert called_with_content.parts[0].text == "message 1"