|
| 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 ..utils.model_name_utils import is_gemini_1_model |
| 23 | +from ..utils.model_name_utils import is_gemini_model |
| 24 | +from .base_tool import BaseTool |
| 25 | +from .tool_context import ToolContext |
| 26 | + |
| 27 | +if TYPE_CHECKING: |
| 28 | + from ..models import LlmRequest |
| 29 | + |
| 30 | + |
| 31 | +class GoogleMapsGroundingTool(BaseTool): |
| 32 | + """A built-in tool that is automatically invoked by Gemini 2 models to ground query results with Google Maps. |
| 33 | +
|
| 34 | + This tool operates internally within the model and does not require or perform |
| 35 | + local code execution. |
| 36 | +
|
| 37 | + Only available for use with the VertexAI Gemini API (e.g. |
| 38 | + GOOGLE_GENAI_USE_VERTEXAI=TRUE) |
| 39 | + """ |
| 40 | + |
| 41 | + def __init__(self): |
| 42 | + # Name and description are not used because this is a model built-in tool. |
| 43 | + super().__init__(name='google_maps', description='google_maps') |
| 44 | + |
| 45 | + @override |
| 46 | + async def process_llm_request( |
| 47 | + self, |
| 48 | + *, |
| 49 | + tool_context: ToolContext, |
| 50 | + llm_request: LlmRequest, |
| 51 | + ) -> None: |
| 52 | + llm_request.config = llm_request.config or types.GenerateContentConfig() |
| 53 | + llm_request.config.tools = llm_request.config.tools or [] |
| 54 | + if is_gemini_1_model(llm_request.model): |
| 55 | + raise ValueError( |
| 56 | + 'Google Maps grounding tool can not be used with Gemini 1.x models.' |
| 57 | + ) |
| 58 | + elif is_gemini_model(llm_request.model): |
| 59 | + llm_request.config.tools.append( |
| 60 | + types.Tool(google_maps=types.GoogleMaps()) |
| 61 | + ) |
| 62 | + else: |
| 63 | + raise ValueError( |
| 64 | + f'Google maps tool is not supported for model {llm_request.model}' |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +google_maps_grounding = GoogleMapsGroundingTool() |
0 commit comments