|
11 | 11 | import time |
12 | 12 | import uuid |
13 | 13 | from dataclasses import dataclass, field |
14 | | -from typing import Any, Dict, List, Optional |
| 14 | +from typing import Any, Dict, Iterable, List, Optional, Tuple |
15 | 15 |
|
16 | 16 | import litellm |
17 | 17 | from litellm import Router |
@@ -101,6 +101,28 @@ class LLMResponse: |
101 | 101 | }, |
102 | 102 | } |
103 | 103 |
|
| 104 | +_FALLBACK_MODEL_PRICING: Dict[str, Any] = { |
| 105 | + "supports_function_calling": True, |
| 106 | + "supports_vision": False, |
| 107 | + "supports_audio_input": False, |
| 108 | + "supports_audio_output": False, |
| 109 | + "context_window": 100000, |
| 110 | + "max_tokens": 10000, |
| 111 | + "input_cost_per_token": 0.0, |
| 112 | + "output_cost_per_token": 0.0, |
| 113 | +} |
| 114 | +_FALLBACK_MODEL_PRICING_REGISTERED: set[str] = set() |
| 115 | + |
| 116 | + |
| 117 | +def _split_provider_model(model: str) -> Tuple[str, str]: |
| 118 | + normalized = (model or "").strip() |
| 119 | + if not normalized: |
| 120 | + return "", "" |
| 121 | + if "/" in normalized: |
| 122 | + provider, remainder = normalized.split("/", 1) |
| 123 | + return provider.lower(), remainder.strip() |
| 124 | + return "openai", normalized |
| 125 | + |
104 | 126 |
|
105 | 127 | def _model_matches(model: str, entries: List[str]) -> bool: |
106 | 128 | """Check if model name matches any entry (exact or prefix with version suffix).""" |
@@ -176,7 +198,6 @@ def _register_custom_model_pricing() -> None: |
176 | 198 | logger.debug(f"Registered custom pricing for {model_name}") |
177 | 199 | except Exception as e: |
178 | 200 | logger.debug(f"Model {model_name} may already be registered or pricing error: {e}") |
179 | | - |
180 | 201 | def _has_channel_config(self) -> bool: |
181 | 202 | """Check if multi-channel config (channels / YAML) is active.""" |
182 | 203 | return bool(self._config.llm_model_list) and not all( |
@@ -566,3 +587,30 @@ def _parse_litellm_response(self, response: Any, model: str) -> LLMResponse: |
566 | 587 | model=model, |
567 | 588 | raw=response, |
568 | 589 | ) |
| 590 | + |
| 591 | + |
| 592 | +def register_fallback_model_pricing(models: Iterable[str]) -> None: |
| 593 | + """Register zero-cost pricing for unknown OpenAI-compatible models.""" |
| 594 | + if not models: |
| 595 | + return |
| 596 | + LLMToolAdapter._register_custom_model_pricing() |
| 597 | + register = getattr(litellm, "register_model", None) |
| 598 | + if not callable(register): |
| 599 | + return |
| 600 | + cost_map = getattr(litellm, "model_cost", {}) |
| 601 | + if not isinstance(cost_map, dict): |
| 602 | + cost_map = {} |
| 603 | + for model in models: |
| 604 | + provider, wire_model = _split_provider_model(str(model)) |
| 605 | + if provider != "openai": |
| 606 | + continue |
| 607 | + if not wire_model or wire_model.startswith("__legacy_"): |
| 608 | + continue |
| 609 | + if wire_model in cost_map or wire_model in _FALLBACK_MODEL_PRICING_REGISTERED: |
| 610 | + continue |
| 611 | + try: |
| 612 | + register({wire_model: dict(_FALLBACK_MODEL_PRICING)}) |
| 613 | + _FALLBACK_MODEL_PRICING_REGISTERED.add(wire_model) |
| 614 | + logger.debug("Registered fallback pricing for %s", wire_model) |
| 615 | + except Exception as exc: |
| 616 | + logger.debug("Fallback pricing registration skipped for %s: %s", wire_model, exc) |
0 commit comments