mirror of
https://github.com/encounter/adk-python.git
synced 2026-07-09 18:19:28 -07:00
chore: Defer import of live, Client and _transformers in google.genai
This is targeting the Client import mostly, but also prevents future latency increase if the other modules in genai adds more 3p dependencies. This change will make ADK only import `live`, `Client` and `_transformers` just-in-time, therefore cutting down cold start latency. Co-authored-by: Liang Wu <wuliang@google.com> PiperOrigin-RevId: 831124329
This commit is contained in:
committed by
Copybara-Service
parent
a550509441
commit
22ca7eefc0
@@ -19,7 +19,6 @@ from __future__ import annotations
|
||||
from typing import AsyncGenerator
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from google.genai import _transformers
|
||||
from typing_extensions import override
|
||||
|
||||
from ...agents.readonly_context import ReadonlyContext
|
||||
@@ -85,6 +84,8 @@ class _InstructionsLlmRequestProcessor(BaseLlmRequestProcessor):
|
||||
|
||||
# Handle static_instruction - add via append_instructions
|
||||
if agent.static_instruction:
|
||||
from google.genai import _transformers
|
||||
|
||||
# Convert ContentUnion to Content using genai transformer
|
||||
static_content = _transformers.t_content(agent.static_instruction)
|
||||
llm_request.append_instructions(static_content)
|
||||
|
||||
@@ -18,12 +18,10 @@ from __future__ import annotations
|
||||
from functools import cached_property
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
from typing import Optional
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from google.adk import version as adk_version
|
||||
from google.genai import Client
|
||||
from google.genai import types
|
||||
from typing_extensions import override
|
||||
|
||||
@@ -31,8 +29,11 @@ from ..utils.env_utils import is_env_enabled
|
||||
from .google_llm import Gemini
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from google.genai import Client
|
||||
|
||||
from .llm_request import LlmRequest
|
||||
|
||||
|
||||
logger = logging.getLogger('google_adk.' + __name__)
|
||||
|
||||
_APIGEE_PROXY_URL_ENV_VARIABLE_NAME = 'APIGEE_PROXY_URL'
|
||||
@@ -137,6 +138,7 @@ class ApigeeLlm(Gemini):
|
||||
Returns:
|
||||
The api client.
|
||||
"""
|
||||
from google.genai import Client
|
||||
|
||||
kwargs_for_http_options = {}
|
||||
if self._api_version:
|
||||
|
||||
@@ -21,8 +21,8 @@ import json
|
||||
import logging
|
||||
import time
|
||||
from typing import Optional
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from google.genai import Client
|
||||
from google.genai import types
|
||||
|
||||
from ..utils.feature_decorator import experimental
|
||||
@@ -32,6 +32,9 @@ from .llm_response import LlmResponse
|
||||
|
||||
logger = logging.getLogger("google_adk." + __name__)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from google.genai import Client
|
||||
|
||||
|
||||
@experimental
|
||||
class GeminiContextCacheManager:
|
||||
|
||||
@@ -18,7 +18,6 @@ import logging
|
||||
from typing import AsyncGenerator
|
||||
from typing import Union
|
||||
|
||||
from google.genai import live
|
||||
from google.genai import types
|
||||
|
||||
from ..utils.context_utils import Aclosing
|
||||
@@ -28,6 +27,10 @@ from .llm_response import LlmResponse
|
||||
logger = logging.getLogger('google_adk.' + __name__)
|
||||
|
||||
RealtimeInput = Union[types.Blob, types.ActivityStart, types.ActivityEnd]
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from google.genai import live
|
||||
|
||||
|
||||
class GeminiLlmConnection(BaseLlmConnection):
|
||||
|
||||
@@ -27,7 +27,6 @@ from typing import Optional
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import Union
|
||||
|
||||
from google.genai import Client
|
||||
from google.genai import types
|
||||
from typing_extensions import override
|
||||
|
||||
@@ -41,6 +40,8 @@ from .gemini_llm_connection import GeminiLlmConnection
|
||||
from .llm_response import LlmResponse
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from google.genai import Client
|
||||
|
||||
from .llm_request import LlmRequest
|
||||
|
||||
logger = logging.getLogger('google_adk.' + __name__)
|
||||
@@ -200,6 +201,8 @@ class Gemini(BaseLlm):
|
||||
Returns:
|
||||
The api client.
|
||||
"""
|
||||
from google.genai import Client
|
||||
|
||||
return Client(
|
||||
http_options=types.HttpOptions(
|
||||
headers=self._tracking_headers,
|
||||
@@ -239,6 +242,8 @@ class Gemini(BaseLlm):
|
||||
|
||||
@cached_property
|
||||
def _live_api_client(self) -> Client:
|
||||
from google.genai import Client
|
||||
|
||||
return Client(
|
||||
http_options=types.HttpOptions(
|
||||
headers=self._tracking_headers, api_version=self._live_api_version
|
||||
|
||||
@@ -46,7 +46,7 @@ def llm_request():
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@mock.patch('google.adk.models.apigee_llm.Client')
|
||||
@mock.patch('google.genai.Client')
|
||||
async def test_generate_content_async_non_streaming(
|
||||
mock_client_constructor, llm_request
|
||||
):
|
||||
@@ -96,7 +96,7 @@ async def test_generate_content_async_non_streaming(
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@mock.patch('google.adk.models.apigee_llm.Client')
|
||||
@mock.patch('google.genai.Client')
|
||||
async def test_generate_content_async_streaming(
|
||||
mock_client_constructor, llm_request
|
||||
):
|
||||
@@ -167,7 +167,7 @@ async def test_generate_content_async_streaming(
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@mock.patch('google.adk.models.apigee_llm.Client')
|
||||
@mock.patch('google.genai.Client')
|
||||
async def test_generate_content_async_with_custom_headers(
|
||||
mock_client_constructor, llm_request
|
||||
):
|
||||
@@ -207,7 +207,7 @@ async def test_generate_content_async_with_custom_headers(
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@mock.patch('google.adk.models.apigee_llm.Client')
|
||||
@mock.patch('google.genai.Client')
|
||||
async def test_vertex_model_path_parsing(mock_client_constructor):
|
||||
"""Tests that Vertex AI model paths are parsed correctly."""
|
||||
apigee_llm = ApigeeLlm(model=APIGEE_VERTEX_MODEL_ID, proxy_url=PROXY_URL)
|
||||
@@ -249,7 +249,7 @@ async def test_vertex_model_path_parsing(mock_client_constructor):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@mock.patch('google.adk.models.apigee_llm.Client')
|
||||
@mock.patch('google.genai.Client')
|
||||
async def test_proxy_url_from_env_variable(mock_client_constructor):
|
||||
"""Tests that proxy_url is read from environment variable."""
|
||||
with mock.patch.dict(
|
||||
@@ -381,7 +381,7 @@ def test_vertex_model_missing_project_or_location_raises_error(
|
||||
),
|
||||
],
|
||||
)
|
||||
@mock.patch('google.adk.models.apigee_llm.Client')
|
||||
@mock.patch('google.genai.Client')
|
||||
async def test_model_string_parsing_and_client_initialization(
|
||||
mock_client_constructor,
|
||||
model_string,
|
||||
|
||||
Reference in New Issue
Block a user