|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +from typing import TYPE_CHECKING |
| 18 | + |
| 19 | +from google.genai import types |
| 20 | +from typing_extensions import override |
| 21 | + |
| 22 | +from .base_tool import BaseTool |
| 23 | +from .tool_context import ToolContext |
| 24 | + |
| 25 | +if TYPE_CHECKING: |
| 26 | + from ..models import LlmRequest |
| 27 | + |
| 28 | + |
| 29 | +class EnterpriseWebSearchTool(BaseTool): |
| 30 | + """A Gemini 2+ built-in tool using web grounding for Enterprise compliance. |
| 31 | +
|
| 32 | + See the documentation for more details: |
| 33 | + https://cloud.google.com/vertex-ai/generative-ai/docs/grounding/web-grounding-enterprise. |
| 34 | + """ |
| 35 | + |
| 36 | + def __init__(self): |
| 37 | + """Initializes the Vertex AI Search tool.""" |
| 38 | + # Name and description are not used because this is a model built-in tool. |
| 39 | + super().__init__( |
| 40 | + name='enterprise_web_search', description='enterprise_web_search' |
| 41 | + ) |
| 42 | + |
| 43 | + @override |
| 44 | + async def process_llm_request( |
| 45 | + self, |
| 46 | + *, |
| 47 | + tool_context: ToolContext, |
| 48 | + llm_request: LlmRequest, |
| 49 | + ) -> None: |
| 50 | + if llm_request.model and llm_request.model.startswith('gemini-'): |
| 51 | + if llm_request.model.startswith('gemini-1') and llm_request.config.tools: |
| 52 | + raise ValueError( |
| 53 | + 'Enterprise web search tool can not be used with other tools in' |
| 54 | + ' Gemini 1.x.' |
| 55 | + ) |
| 56 | + llm_request.config = llm_request.config or types.GenerateContentConfig() |
| 57 | + llm_request.config.tools = llm_request.config.tools or [] |
| 58 | + llm_request.config.tools.append( |
| 59 | + types.Tool(enterprise_web_search=types.EnterpriseWebSearch()) |
| 60 | + ) |
| 61 | + else: |
| 62 | + raise ValueError( |
| 63 | + 'Enterprise web search tool is not supported for model' |
| 64 | + f' {llm_request.model}' |
| 65 | + ) |
0 commit comments