mirror of
https://github.com/encounter/adk-python.git
synced 2026-07-09 18:19:28 -07:00
chore: provide a way to disable model check for builtin tools
Co-authored-by: George Weale <gweale@google.com> PiperOrigin-RevId: 872503435
This commit is contained in:
committed by
Copybara-Service
parent
f27a9cfb87
commit
eaf50ce37e
@@ -20,6 +20,7 @@ from typing_extensions import override
|
||||
from ..agents.invocation_context import InvocationContext
|
||||
from ..models import LlmRequest
|
||||
from ..utils.model_name_utils import is_gemini_2_or_above
|
||||
from ..utils.model_name_utils import is_gemini_model_id_check_disabled
|
||||
from .base_code_executor import BaseCodeExecutor
|
||||
from .code_execution_utils import CodeExecutionInput
|
||||
from .code_execution_utils import CodeExecutionResult
|
||||
@@ -42,7 +43,8 @@ class BuiltInCodeExecutor(BaseCodeExecutor):
|
||||
|
||||
def process_llm_request(self, llm_request: LlmRequest) -> None:
|
||||
"""Pre-process the LLM request for Gemini 2.0+ models to use the code execution tool."""
|
||||
if is_gemini_2_or_above(llm_request.model):
|
||||
model_check_disabled = is_gemini_model_id_check_disabled()
|
||||
if is_gemini_2_or_above(llm_request.model) or model_check_disabled:
|
||||
llm_request.config = llm_request.config or types.GenerateContentConfig()
|
||||
llm_request.config.tools = llm_request.config.tools or []
|
||||
llm_request.config.tools.append(
|
||||
|
||||
@@ -21,6 +21,7 @@ from typing_extensions import override
|
||||
|
||||
from ..utils.model_name_utils import is_gemini_1_model
|
||||
from ..utils.model_name_utils import is_gemini_model
|
||||
from ..utils.model_name_utils import is_gemini_model_id_check_disabled
|
||||
from .base_tool import BaseTool
|
||||
from .tool_context import ToolContext
|
||||
|
||||
@@ -54,14 +55,16 @@ class EnterpriseWebSearchTool(BaseTool):
|
||||
tool_context: ToolContext,
|
||||
llm_request: LlmRequest,
|
||||
) -> None:
|
||||
if is_gemini_model(llm_request.model):
|
||||
model_check_disabled = is_gemini_model_id_check_disabled()
|
||||
llm_request.config = llm_request.config or types.GenerateContentConfig()
|
||||
llm_request.config.tools = llm_request.config.tools or []
|
||||
|
||||
if is_gemini_model(llm_request.model) or model_check_disabled:
|
||||
if is_gemini_1_model(llm_request.model) and llm_request.config.tools:
|
||||
raise ValueError(
|
||||
'Enterprise Web Search tool cannot be used with other tools in'
|
||||
' Gemini 1.x.'
|
||||
)
|
||||
llm_request.config = llm_request.config or types.GenerateContentConfig()
|
||||
llm_request.config.tools = llm_request.config.tools or []
|
||||
llm_request.config.tools.append(
|
||||
types.Tool(enterprise_web_search=types.EnterpriseWebSearch())
|
||||
)
|
||||
|
||||
@@ -21,6 +21,7 @@ from typing_extensions import override
|
||||
|
||||
from ..utils.model_name_utils import is_gemini_1_model
|
||||
from ..utils.model_name_utils import is_gemini_model
|
||||
from ..utils.model_name_utils import is_gemini_model_id_check_disabled
|
||||
from .base_tool import BaseTool
|
||||
from .tool_context import ToolContext
|
||||
|
||||
@@ -49,13 +50,14 @@ class GoogleMapsGroundingTool(BaseTool):
|
||||
tool_context: ToolContext,
|
||||
llm_request: LlmRequest,
|
||||
) -> None:
|
||||
model_check_disabled = is_gemini_model_id_check_disabled()
|
||||
llm_request.config = llm_request.config or types.GenerateContentConfig()
|
||||
llm_request.config.tools = llm_request.config.tools or []
|
||||
if is_gemini_1_model(llm_request.model):
|
||||
raise ValueError(
|
||||
'Google Maps grounding tool cannot be used with Gemini 1.x models.'
|
||||
)
|
||||
elif is_gemini_model(llm_request.model):
|
||||
elif is_gemini_model(llm_request.model) or model_check_disabled:
|
||||
llm_request.config.tools.append(
|
||||
types.Tool(google_maps=types.GoogleMaps())
|
||||
)
|
||||
|
||||
@@ -21,6 +21,7 @@ from typing_extensions import override
|
||||
|
||||
from ..utils.model_name_utils import is_gemini_1_model
|
||||
from ..utils.model_name_utils import is_gemini_model
|
||||
from ..utils.model_name_utils import is_gemini_model_id_check_disabled
|
||||
from .base_tool import BaseTool
|
||||
from .tool_context import ToolContext
|
||||
|
||||
@@ -67,6 +68,7 @@ class GoogleSearchTool(BaseTool):
|
||||
if self.model is not None:
|
||||
llm_request.model = self.model
|
||||
|
||||
model_check_disabled = is_gemini_model_id_check_disabled()
|
||||
llm_request.config = llm_request.config or types.GenerateContentConfig()
|
||||
llm_request.config.tools = llm_request.config.tools or []
|
||||
if is_gemini_1_model(llm_request.model):
|
||||
@@ -77,7 +79,7 @@ class GoogleSearchTool(BaseTool):
|
||||
llm_request.config.tools.append(
|
||||
types.Tool(google_search_retrieval=types.GoogleSearchRetrieval())
|
||||
)
|
||||
elif is_gemini_model(llm_request.model):
|
||||
elif is_gemini_model(llm_request.model) or model_check_disabled:
|
||||
llm_request.config.tools.append(
|
||||
types.Tool(google_search=types.GoogleSearch())
|
||||
)
|
||||
|
||||
@@ -24,6 +24,7 @@ from google.genai import types
|
||||
from typing_extensions import override
|
||||
|
||||
from ...utils.model_name_utils import is_gemini_2_or_above
|
||||
from ...utils.model_name_utils import is_gemini_model_id_check_disabled
|
||||
from ..tool_context import ToolContext
|
||||
from .base_retrieval_tool import BaseRetrievalTool
|
||||
|
||||
@@ -63,7 +64,8 @@ class VertexAiRagRetrieval(BaseRetrievalTool):
|
||||
llm_request: LlmRequest,
|
||||
) -> None:
|
||||
# Use Gemini built-in Vertex AI RAG tool for Gemini 2 models.
|
||||
if is_gemini_2_or_above(llm_request.model):
|
||||
model_check_disabled = is_gemini_model_id_check_disabled()
|
||||
if is_gemini_2_or_above(llm_request.model) or model_check_disabled:
|
||||
llm_request.config = (
|
||||
types.GenerateContentConfig()
|
||||
if not llm_request.config
|
||||
|
||||
@@ -21,6 +21,7 @@ from typing_extensions import override
|
||||
|
||||
from ..utils.model_name_utils import is_gemini_1_model
|
||||
from ..utils.model_name_utils import is_gemini_2_or_above
|
||||
from ..utils.model_name_utils import is_gemini_model_id_check_disabled
|
||||
from .base_tool import BaseTool
|
||||
from .tool_context import ToolContext
|
||||
|
||||
@@ -46,11 +47,12 @@ class UrlContextTool(BaseTool):
|
||||
tool_context: ToolContext,
|
||||
llm_request: LlmRequest,
|
||||
) -> None:
|
||||
model_check_disabled = is_gemini_model_id_check_disabled()
|
||||
llm_request.config = llm_request.config or types.GenerateContentConfig()
|
||||
llm_request.config.tools = llm_request.config.tools or []
|
||||
if is_gemini_1_model(llm_request.model):
|
||||
raise ValueError('Url context tool cannot be used in Gemini 1.x.')
|
||||
elif is_gemini_2_or_above(llm_request.model):
|
||||
elif is_gemini_2_or_above(llm_request.model) or model_check_disabled:
|
||||
llm_request.config.tools.append(
|
||||
types.Tool(url_context=types.UrlContext())
|
||||
)
|
||||
|
||||
@@ -24,6 +24,7 @@ from typing_extensions import override
|
||||
from ..agents.readonly_context import ReadonlyContext
|
||||
from ..utils.model_name_utils import is_gemini_1_model
|
||||
from ..utils.model_name_utils import is_gemini_model
|
||||
from ..utils.model_name_utils import is_gemini_model_id_check_disabled
|
||||
from .base_tool import BaseTool
|
||||
from .tool_context import ToolContext
|
||||
|
||||
@@ -141,14 +142,16 @@ class VertexAiSearchTool(BaseTool):
|
||||
tool_context: ToolContext,
|
||||
llm_request: LlmRequest,
|
||||
) -> None:
|
||||
if is_gemini_model(llm_request.model):
|
||||
model_check_disabled = is_gemini_model_id_check_disabled()
|
||||
llm_request.config = llm_request.config or types.GenerateContentConfig()
|
||||
llm_request.config.tools = llm_request.config.tools or []
|
||||
|
||||
if is_gemini_model(llm_request.model) or model_check_disabled:
|
||||
if is_gemini_1_model(llm_request.model) and llm_request.config.tools:
|
||||
raise ValueError(
|
||||
'Vertex AI search tool cannot be used with other tools in Gemini'
|
||||
' 1.x.'
|
||||
)
|
||||
llm_request.config = llm_request.config or types.GenerateContentConfig()
|
||||
llm_request.config.tools = llm_request.config.tools or []
|
||||
|
||||
# Build the search config (can be overridden by subclasses)
|
||||
vertex_ai_search_config = self._build_vertex_ai_search_config(
|
||||
|
||||
@@ -22,6 +22,19 @@ from typing import Optional
|
||||
from packaging.version import InvalidVersion
|
||||
from packaging.version import Version
|
||||
|
||||
from .env_utils import is_env_enabled
|
||||
|
||||
_DISABLE_GEMINI_MODEL_ID_CHECK_ENV_VAR = 'ADK_DISABLE_GEMINI_MODEL_ID_CHECK'
|
||||
|
||||
|
||||
def is_gemini_model_id_check_disabled() -> bool:
|
||||
"""Returns True when Gemini model-id validation should be bypassed.
|
||||
|
||||
This opt-in environment variable is intended for internal usage where model
|
||||
ids may not follow the public ``gemini-*`` naming convention.
|
||||
"""
|
||||
return is_env_enabled(_DISABLE_GEMINI_MODEL_ID_CHECK_ENV_VAR)
|
||||
|
||||
|
||||
def extract_model_name(model_string: str) -> str:
|
||||
"""Extract the actual model name from either simple or path-based format.
|
||||
|
||||
Reference in New Issue
Block a user