|
14 | 14 |
|
15 | 15 | """Defines the interface to support a model.""" |
16 | 16 |
|
17 | | -from .apigee_llm import ApigeeLlm |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import importlib |
| 20 | +from typing import TYPE_CHECKING |
| 21 | + |
18 | 22 | from .base_llm import BaseLlm |
19 | | -from .gemma_llm import Gemma |
20 | | -from .google_llm import Gemini |
21 | 23 | from .llm_request import LlmRequest |
22 | 24 | from .llm_response import LlmResponse |
23 | 25 | from .registry import LLMRegistry |
24 | 26 |
|
| 27 | +if TYPE_CHECKING: |
| 28 | + from .anthropic_llm import Claude |
| 29 | + from .apigee_llm import ApigeeLlm |
| 30 | + from .gemma_llm import Gemma |
| 31 | + from .gemma_llm import Gemma3Ollama |
| 32 | + from .google_llm import Gemini |
| 33 | + from .lite_llm import LiteLlm |
| 34 | + |
25 | 35 | __all__ = [ |
| 36 | + 'ApigeeLlm', |
26 | 37 | 'BaseLlm', |
| 38 | + 'Claude', |
27 | 39 | 'Gemini', |
28 | 40 | 'Gemma', |
| 41 | + 'Gemma3Ollama', |
29 | 42 | 'LLMRegistry', |
| 43 | + 'LiteLlm', |
30 | 44 | ] |
31 | 45 |
|
| 46 | +_LAZY_PROVIDERS: dict[str, tuple[list[str], str]] = { |
| 47 | + 'Gemini': ( |
| 48 | + [ |
| 49 | + r'gemini-.*', |
| 50 | + r'model-optimizer-.*', |
| 51 | + r'projects\/.+\/locations\/.+\/endpoints\/.+', |
| 52 | + r'projects\/.+\/locations\/.+\/publishers\/google\/models\/gemini.+', |
| 53 | + ], |
| 54 | + 'google_llm', |
| 55 | + ), |
| 56 | + 'Gemma': ([r'gemma-.*'], 'gemma_llm'), |
| 57 | + 'ApigeeLlm': ([r'.*-apigee$'], 'apigee_llm'), |
| 58 | + 'Claude': ([r'claude-3-.*', r'claude-.*-4.*'], 'anthropic_llm'), |
| 59 | + 'Gemma3Ollama': ([r'ollama/gemma3.*'], 'gemma_llm'), |
| 60 | + 'LiteLlm': ( |
| 61 | + [ |
| 62 | + r'openai/.*', |
| 63 | + r'azure/.*', |
| 64 | + r'azure_ai/.*', |
| 65 | + r'groq/.*', |
| 66 | + r'anthropic/.*', |
| 67 | + r'bedrock/.*', |
| 68 | + r'ollama/(?!gemma3).*', |
| 69 | + r'ollama_chat/.*', |
| 70 | + r'together_ai/.*', |
| 71 | + r'vertex_ai/.*', |
| 72 | + r'mistral/.*', |
| 73 | + r'deepseek/.*', |
| 74 | + r'fireworks_ai/.*', |
| 75 | + r'cohere/.*', |
| 76 | + r'databricks/.*', |
| 77 | + r'ai21/.*', |
| 78 | + ], |
| 79 | + 'lite_llm', |
| 80 | + ), |
| 81 | +} |
32 | 82 |
|
33 | | -LLMRegistry.register(Gemini) |
34 | | -LLMRegistry.register(Gemma) |
35 | | -LLMRegistry.register(ApigeeLlm) |
| 83 | +for _name, (_patterns, _module) in _LAZY_PROVIDERS.items(): |
| 84 | + LLMRegistry._register_lazy(_patterns, f'{__name__}.{_module}', _name) |
36 | 85 |
|
37 | | -# Optionally register Claude if anthropic package is installed |
38 | | -try: |
39 | | - from .anthropic_llm import Claude |
40 | | - |
41 | | - LLMRegistry.register(Claude) |
42 | | - __all__.append('Claude') |
43 | | -except Exception: |
44 | | - # Claude support requires: pip install google-adk[extensions] |
45 | | - pass |
46 | | - |
47 | | -# Optionally register LiteLlm if litellm package is installed |
48 | | -try: |
49 | | - from .lite_llm import LiteLlm |
50 | | - |
51 | | - LLMRegistry.register(LiteLlm) |
52 | | - __all__.append('LiteLlm') |
53 | | -except Exception: |
54 | | - # LiteLLM support requires: pip install google-adk[extensions] |
55 | | - pass |
56 | | - |
57 | | -# Optionally register Gemma3Ollama if litellm package is installed |
58 | | -try: |
59 | | - from .gemma_llm import Gemma3Ollama |
60 | 86 |
|
61 | | - LLMRegistry.register(Gemma3Ollama) |
62 | | - __all__.append('Gemma3Ollama') |
63 | | -except Exception: |
64 | | - # Gemma3Ollama requires LiteLLM: pip install google-adk[extensions] |
65 | | - pass |
| 87 | +def __getattr__(name: str): |
| 88 | + if name in _LAZY_PROVIDERS: |
| 89 | + module_name = _LAZY_PROVIDERS[name][1] |
| 90 | + try: |
| 91 | + module = importlib.import_module(f'{__name__}.{module_name}') |
| 92 | + except ImportError as e: |
| 93 | + raise ImportError( |
| 94 | + f'`{name}` requires an optional dependency that is not installed.' |
| 95 | + ' Install with: pip install google-adk[extensions]' |
| 96 | + ) from e |
| 97 | + return getattr(module, name) |
| 98 | + raise AttributeError(f'module {__name__!r} has no attribute {name!r}') |
0 commit comments