-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat: implement enterprise search agent tool and related functionality #3544
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
m-baden
wants to merge
6
commits into
google:main
Choose a base branch
from
m-baden: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.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4588b9d
feat: implement enterprise search agent tool and related functionality
m-baden 3a20b7d
feat: refactor search agent tools to inherit from a common base class
m-baden 090b540
Merge branch 'main' into main
m-baden b19353a
feat: streamline tool handling in LLM agent by implementing dedicated…
m-baden e41ea88
fix: enhance state filtering in _SearchAgentTool to exclude temporary…
m-baden 1856e83
style: format lambda handler for better readability in llm_agent.py
m-baden 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
Some comments aren't visible on the classic Files Changed page.
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 |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| # Copyright 2025 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 typing import Any | ||
|
|
||
| from google.genai import types | ||
| from typing_extensions import override | ||
|
|
||
| from ..agents.llm_agent import LlmAgent | ||
| from ..memory.in_memory_memory_service import InMemoryMemoryService | ||
| from ..runners import Runner | ||
| from ..sessions.in_memory_session_service import InMemorySessionService | ||
| from ..utils.context_utils import Aclosing | ||
| from ._forwarding_artifact_service import ForwardingArtifactService | ||
| from .agent_tool import AgentTool | ||
| from .tool_context import ToolContext | ||
|
|
||
|
|
||
| class _SearchAgentTool(AgentTool): | ||
| """A base class for search agent tools.""" | ||
|
|
||
| @override | ||
| async def run_async( | ||
| self, | ||
| *, | ||
| args: dict[str, Any], | ||
| tool_context: ToolContext, | ||
| ) -> Any: | ||
|
|
||
| if isinstance(self.agent, LlmAgent) and self.agent.input_schema: | ||
| input_value = self.agent.input_schema.model_validate(args) | ||
| content = types.Content( | ||
| role='user', | ||
| parts=[ | ||
| types.Part.from_text( | ||
| text=input_value.model_dump_json(exclude_none=True) | ||
| ) | ||
| ], | ||
| ) | ||
| else: | ||
| content = types.Content( | ||
| role='user', | ||
| parts=[types.Part.from_text(text=args['request'])], | ||
| ) | ||
| runner = Runner( | ||
| app_name=self.agent.name, | ||
| agent=self.agent, | ||
| artifact_service=ForwardingArtifactService(tool_context), | ||
| session_service=InMemorySessionService(), | ||
| memory_service=InMemoryMemoryService(), | ||
| credential_service=tool_context._invocation_context.credential_service, | ||
| plugins=list(tool_context._invocation_context.plugin_manager.plugins), | ||
| ) | ||
| try: | ||
| state_dict = { | ||
| k: v | ||
| for k, v in tool_context.state.to_dict().items() | ||
| if not k.startswith('_adk') and not k.startswith('temp:') | ||
| } | ||
| session = await runner.session_service.create_session( | ||
| app_name=self.agent.name, | ||
| user_id=tool_context._invocation_context.user_id, | ||
| state=state_dict, | ||
| ) | ||
|
|
||
| last_content = None | ||
| last_grounding_metadata = None | ||
| async with Aclosing( | ||
| runner.run_async( | ||
| user_id=session.user_id, | ||
| session_id=session.id, | ||
| new_message=content, | ||
| ) | ||
| ) as agen: | ||
| async for event in agen: | ||
| # Forward state delta to parent session. | ||
| if event.actions.state_delta: | ||
| tool_context.state.update(event.actions.state_delta) | ||
| if event.content: | ||
| last_content = event.content | ||
| last_grounding_metadata = event.grounding_metadata | ||
|
|
||
| if not last_content: | ||
| return '' | ||
| merged_text = '\n'.join(p.text for p in last_content.parts if p.text) | ||
| if isinstance(self.agent, LlmAgent) and self.agent.output_schema: | ||
| tool_result = self.agent.output_schema.model_validate_json( | ||
| merged_text | ||
| ).model_dump(exclude_none=True) | ||
| else: | ||
| tool_result = merged_text | ||
|
|
||
| if last_grounding_metadata: | ||
| tool_context.state['temp:_adk_grounding_metadata'] = ( | ||
| last_grounding_metadata | ||
| ) | ||
| return tool_result | ||
| finally: | ||
| await runner.close() | ||
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 |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # Copyright 2025 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 typing import Union | ||
|
|
||
| from ..agents.llm_agent import LlmAgent | ||
| from ..models.base_llm import BaseLlm | ||
| from ._search_agent_tool import _SearchAgentTool | ||
| from .enterprise_search_tool import enterprise_web_search_tool | ||
|
|
||
|
|
||
| def create_enterprise_search_agent(model: Union[str, BaseLlm]) -> LlmAgent: | ||
| """Create a sub-agent that only uses enterprise_web_search tool.""" | ||
| return LlmAgent( | ||
| name='enterprise_search_agent', | ||
| model=model, | ||
| description=( | ||
| 'An agent for performing Enterprise search using the' | ||
| ' `enterprise_web_search` tool' | ||
| ), | ||
| instruction=""" | ||
| You are a specialized Enterprise search agent. | ||
|
|
||
| When given a search query, use the `enterprise_web_search` tool to find the related information. | ||
| """, | ||
| tools=[enterprise_web_search_tool], | ||
| ) | ||
|
|
||
|
|
||
| class EnterpriseSearchAgentTool(_SearchAgentTool): | ||
| """A tool that wraps a sub-agent that only uses enterprise_web_search tool. | ||
|
|
||
| This is a workaround to support using enterprise_web_search tool with other tools. | ||
| TODO(b/448114567): Remove once the workaround is no longer needed. | ||
|
|
||
| Attributes: | ||
| agent: The sub-agent that this tool wraps. | ||
| """ | ||
|
|
||
| def __init__(self, agent: LlmAgent): | ||
| self.agent = agent | ||
| super().__init__(agent=self.agent) |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.