Coverage for mt_ai/models.py: 94.12%

15 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2026-11-30 14:44 +0000

1"""Model ids → what ADK runs (SB-1315). 

2 

3`MT_AI_MODEL` stays configuration, never code (mt2/ai.md). A Gemini id is passed to 

4ADK as-is. An Ollama id (`ollama_chat/<model>`, or `ollama/<model>`) becomes ADK's 

5`LiteLlm` pointed at a local Ollama server — the "local" profile, for development 

6and offline evals. 

7 

8`litellm` is an optional extra (`uv sync --extra local-ai`) and is never in the 

9prod image. It is pinned exactly: litellm 1.82.7 and 1.82.8 were malicious PyPI 

10releases (March 2026). 

11""" 

12 

13import os 

14 

15from google.adk.models.base_llm import BaseLlm 

16 

17OLLAMA_PREFIXES = ("ollama_chat/", "ollama/") 

18DEFAULT_OLLAMA_API_BASE = "http://127.0.0.1:11434" 

19 

20 

21class ModelUnavailableError(Exception): 

22 """The configured model cannot be loaded here. The message is for logs and operators.""" 

23 

24 

25def is_local_model(model_id: str) -> bool: 

26 return model_id.startswith(OLLAMA_PREFIXES) 

27 

28 

29def resolve_model(model_id: str) -> BaseLlm | str: 

30 if not is_local_model(model_id): 

31 return model_id 

32 try: 

33 from google.adk.models.lite_llm import LiteLlm 

34 except ImportError as exc: 

35 raise ModelUnavailableError( 

36 f"{model_id} needs the local-ai extra: cd backend && uv sync --extra local-ai" 

37 ) from exc 

38 return LiteLlm(model=model_id, api_base=os.getenv("OLLAMA_API_BASE", DEFAULT_OLLAMA_API_BASE))