|
| 1 | +"""Per-model parameter support, resolved from the LLM Gateway discovery API. |
| 2 | +
|
| 3 | +Newer models reject parameters that older ones accept: passing `temperature` to |
| 4 | +a model that dropped it fails the whole request with HTTP 400. The gateway |
| 5 | +publishes these constraints per model under `modelDetails`, so callers can strip |
| 6 | +the parameter instead of guessing from the model name. |
| 7 | +""" |
| 8 | + |
| 9 | +import logging |
| 10 | +from typing import Any |
| 11 | + |
| 12 | +from ..common._base_service import BaseService |
| 13 | +from ..common._endpoints_manager import EndpointManager |
| 14 | +from ..common._models import Endpoint |
| 15 | +from ..constants import HEADER_AGENTHUB_CONFIG |
| 16 | + |
| 17 | +logger = logging.getLogger(__name__) |
| 18 | + |
| 19 | +CacheKey = tuple[str, str | None] |
| 20 | + |
| 21 | +# One fetch per (base_url, agenthub_config); an empty mapping caches "couldn't read |
| 22 | +# discovery". Deliberately unlocked: a racing duplicate GET is cheaper than an |
| 23 | +# asyncio.Lock pinned to whichever event loop touched it first. |
| 24 | +_model_details: dict[CacheKey, dict[str, dict[str, Any]]] = {} |
| 25 | + |
| 26 | + |
| 27 | +async def should_skip_temperature( |
| 28 | + service: BaseService, model: str, agenthub_config: str | None |
| 29 | +) -> bool: |
| 30 | + """Whether `model` rejects the `temperature` parameter. |
| 31 | +
|
| 32 | + Args: |
| 33 | + service: Service used to call discovery; supplies base URL and auth. |
| 34 | + model: Model name as sent to the gateway. |
| 35 | + agenthub_config: AgentHub config scoping which models are visible. |
| 36 | +
|
| 37 | + Returns: |
| 38 | + True only when discovery explicitly reports the model skips temperature. |
| 39 | + Unknown models, unreachable discovery, and any unexpected failure return |
| 40 | + False, leaving the caller's request untouched. |
| 41 | + """ |
| 42 | + try: |
| 43 | + details = await _details_for(service, model, agenthub_config) |
| 44 | + except Exception as e: |
| 45 | + logger.warning( |
| 46 | + "Could not resolve parameter support for model %s (%s); " |
| 47 | + "sending model parameters unchanged", |
| 48 | + model, |
| 49 | + e, |
| 50 | + ) |
| 51 | + return False |
| 52 | + |
| 53 | + return bool(details.get("shouldSkipTemperature", False)) |
| 54 | + |
| 55 | + |
| 56 | +async def _details_for( |
| 57 | + service: BaseService, model: str, agenthub_config: str | None |
| 58 | +) -> dict[str, Any]: |
| 59 | + key = (service._config.base_url, agenthub_config) |
| 60 | + |
| 61 | + if key not in _model_details: |
| 62 | + _model_details[key] = await _fetch_model_details(service, agenthub_config) |
| 63 | + |
| 64 | + return _model_details[key].get(model, {}) |
| 65 | + |
| 66 | + |
| 67 | +async def _fetch_model_details( |
| 68 | + service: BaseService, agenthub_config: str | None |
| 69 | +) -> dict[str, dict[str, Any]]: |
| 70 | + headers = {HEADER_AGENTHUB_CONFIG: agenthub_config} if agenthub_config else {} |
| 71 | + endpoint = Endpoint("/" + EndpointManager.get_discovery_endpoint()) |
| 72 | + |
| 73 | + try: |
| 74 | + response = await service.request_async("GET", endpoint, headers=headers) |
| 75 | + models = response.json() |
| 76 | + except Exception as e: |
| 77 | + # Falling back to the caller's parameters is the pre-existing behaviour; |
| 78 | + # failing the LLM call because discovery is down would be worse. |
| 79 | + logger.warning( |
| 80 | + "LLM Gateway discovery unavailable (%s); sending model parameters unchanged", |
| 81 | + e, |
| 82 | + ) |
| 83 | + return {} |
| 84 | + |
| 85 | + if not isinstance(models, list): |
| 86 | + logger.warning("Unexpected LLM Gateway discovery payload; expected a list") |
| 87 | + return {} |
| 88 | + |
| 89 | + return { |
| 90 | + model["modelName"]: model.get("modelDetails") or {} |
| 91 | + for model in models |
| 92 | + if isinstance(model, dict) and model.get("modelName") |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | +def _reset_cache() -> None: |
| 97 | + """Clear the discovery cache. For tests.""" |
| 98 | + _model_details.clear() |
0 commit comments