-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat(toolset): add generate_preprocessing_events method to BaseToolset #3342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
loveyana
wants to merge
1
commit into
google:main
Choose a base branch
from
loveyana:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+191
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
|
|
||
| """Unit tests for BaseLlmFlow toolset integration.""" | ||
|
|
||
| from typing import AsyncGenerator | ||
| from unittest import mock | ||
| from unittest.mock import AsyncMock | ||
|
|
||
|
|
@@ -26,6 +27,7 @@ | |
| from google.adk.plugins.base_plugin import BasePlugin | ||
| from google.adk.tools.base_toolset import BaseToolset | ||
| from google.adk.tools.google_search_tool import GoogleSearchTool | ||
| from google.adk.tools.tool_context import ToolContext | ||
| from google.genai import types | ||
| import pytest | ||
|
|
||
|
|
@@ -91,6 +93,156 @@ async def close(self): | |
| assert mock_toolset.process_llm_request_called | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_preprocess_calls_toolset_generate_preprocessing_events(): | ||
| """Test that _preprocess_async calls generate_preprocessing_events on toolsets.""" | ||
|
|
||
| # Create a mock toolset that tracks if generate_preprocessing_events was called | ||
| class _MockToolset(BaseToolset): | ||
|
|
||
| def __init__(self): | ||
| super().__init__() | ||
| self.generate_preprocessing_events_called = False | ||
| self.generated_events = [] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| async def generate_preprocessing_events( | ||
| self, *, tool_context: ToolContext, llm_request: LlmRequest | ||
| ) -> AsyncGenerator[Event, None]: | ||
| self.generate_preprocessing_events_called = True | ||
| # Generate a mock authentication event | ||
| auth_event = Event( | ||
| author='system', | ||
| invocation_id='test_invocation', | ||
| content=types.Content( | ||
| role='model', | ||
| parts=[types.Part(text='Mock authentication request')], | ||
| ), | ||
| ) | ||
| self.generated_events.append(auth_event) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| yield auth_event | ||
|
|
||
| async def get_tools(self, readonly_context=None): | ||
| return [] | ||
|
|
||
| async def close(self): | ||
| pass | ||
|
|
||
| mock_toolset = _MockToolset() | ||
|
|
||
| # Create a mock model that returns a simple response | ||
| mock_response = LlmResponse( | ||
| content=types.Content( | ||
| role='model', parts=[types.Part.from_text(text='Test response')] | ||
| ), | ||
| partial=False, | ||
| ) | ||
|
|
||
| mock_model = testing_utils.MockModel.create(responses=[mock_response]) | ||
|
|
||
| # Create agent with the mock toolset | ||
| agent = Agent(name='test_agent', model=mock_model, tools=[mock_toolset]) | ||
| invocation_context = await testing_utils.create_invocation_context( | ||
| agent=agent, user_content='test message' | ||
| ) | ||
|
|
||
| flow = BaseLlmFlowForTesting() | ||
|
|
||
| # Call _preprocess_async | ||
| llm_request = LlmRequest() | ||
| events = [] | ||
| async for event in flow._preprocess_async(invocation_context, llm_request): | ||
| events.append(event) | ||
|
|
||
| # Verify that generate_preprocessing_events was called on the toolset | ||
| assert mock_toolset.generate_preprocessing_events_called | ||
|
|
||
| # Verify that the generated event was yielded | ||
| assert len(events) == 1 | ||
| assert events[0].author == 'system' | ||
| assert events[0].content.parts[0].text == 'Mock authentication request' | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_preprocess_calls_both_generate_events_and_process_request(): | ||
| """Test that _preprocess_async calls both generate_preprocessing_events and process_llm_request.""" | ||
|
|
||
| # Create a mock toolset that tracks both method calls | ||
| class _MockToolset(BaseToolset): | ||
|
|
||
| def __init__(self): | ||
| super().__init__() | ||
| self.generate_preprocessing_events_called = False | ||
| self.process_llm_request_called = False | ||
| self.call_order = [] | ||
|
|
||
| async def generate_preprocessing_events( | ||
| self, *, tool_context: ToolContext, llm_request: LlmRequest | ||
| ) -> AsyncGenerator[Event, None]: | ||
| self.generate_preprocessing_events_called = True | ||
| self.call_order.append('generate_preprocessing_events') | ||
| # Generate a mock event | ||
| yield Event( | ||
| author='system', | ||
| invocation_id='test_invocation', | ||
| content=types.Content( | ||
| role='model', parts=[types.Part(text='Mock event')] | ||
| ), | ||
| ) | ||
|
|
||
| async def process_llm_request( | ||
| self, *, tool_context: ToolContext, llm_request: LlmRequest | ||
| ) -> None: | ||
| self.process_llm_request_called = True | ||
| self.call_order.append('process_llm_request') | ||
|
|
||
| async def get_tools(self, readonly_context=None): | ||
| return [] | ||
|
|
||
| async def close(self): | ||
| pass | ||
|
|
||
| mock_toolset = _MockToolset() | ||
|
|
||
| # Create a mock model that returns a simple response | ||
| mock_response = LlmResponse( | ||
| content=types.Content( | ||
| role='model', parts=[types.Part.from_text(text='Test response')] | ||
| ), | ||
| partial=False, | ||
| ) | ||
|
|
||
| mock_model = testing_utils.MockModel.create(responses=[mock_response]) | ||
|
|
||
| # Create agent with the mock toolset | ||
| agent = Agent(name='test_agent', model=mock_model, tools=[mock_toolset]) | ||
| invocation_context = await testing_utils.create_invocation_context( | ||
| agent=agent, user_content='test message' | ||
| ) | ||
|
|
||
| flow = BaseLlmFlowForTesting() | ||
|
|
||
| # Call _preprocess_async | ||
| llm_request = LlmRequest() | ||
| events = [] | ||
| async for event in flow._preprocess_async(invocation_context, llm_request): | ||
| events.append(event) | ||
|
|
||
| # Verify that both methods were called | ||
| assert mock_toolset.generate_preprocessing_events_called | ||
| assert mock_toolset.process_llm_request_called | ||
|
|
||
| # Verify the correct call order (generate_preprocessing_events first) | ||
| assert mock_toolset.call_order == [ | ||
| 'generate_preprocessing_events', | ||
| 'process_llm_request', | ||
| ] | ||
|
|
||
| # Verify that the generated event was yielded | ||
| assert len(events) == 1 | ||
| assert events[0].author == 'system' | ||
| assert events[0].content.parts[0].text == 'Mock event' | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_preprocess_handles_mixed_tools_and_toolsets(): | ||
| """Test that _preprocess_async properly handles both tools and toolsets.""" | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
if False: yieldpattern to create an empty async generator is a bit of a workaround. A more modern and explicit approach is to useyield from (). This is more readable, idiomatic, and achieves the same result of creating an empty async generator.