Spaces:
Build error
Build error
File size: 20,350 Bytes
175746c 09973b6 175746c 09973b6 175746c 09973b6 175746c 09973b6 175746c e4a41fa 175746c 09973b6 175746c 09973b6 175746c 09973b6 175746c 09973b6 175746c 09973b6 175746c 09973b6 175746c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 | """Model registry with capabilities database.
Centralized database of LLM models with their capabilities, context limits,
and provider information. Supports dynamic registration of custom models
and automatic provider detection.
Pricing is fetched dynamically from LiteLLM's community-maintained database.
"""
from __future__ import annotations
from dataclasses import dataclass
from typing import Any
from headroom.pricing.litellm_pricing import estimate_cost as litellm_estimate_cost
from headroom.pricing.litellm_pricing import get_model_pricing
@dataclass(frozen=True)
class ModelInfo:
"""Information about an LLM model.
Attributes:
name: Model identifier.
provider: Provider name (openai, anthropic, etc.).
context_window: Maximum context window in tokens.
max_output_tokens: Maximum output tokens.
supports_tools: Whether model supports tool/function calling.
supports_vision: Whether model supports image inputs.
supports_streaming: Whether model supports streaming responses.
supports_json_mode: Whether model supports JSON output mode.
tokenizer_backend: Tokenizer backend to use.
aliases: Alternative names for the model.
notes: Additional notes about the model.
Note:
Pricing is fetched dynamically from LiteLLM's database.
Use ModelRegistry.estimate_cost() to get current pricing.
"""
name: str
provider: str
context_window: int = 128000
max_output_tokens: int = 4096
supports_tools: bool = True
supports_vision: bool = False
supports_streaming: bool = True
supports_json_mode: bool = True
tokenizer_backend: str | None = None
aliases: tuple[str, ...] = ()
notes: str = ""
# Built-in model database
# Pricing as of January 2025 - verify current rates
_MODELS: dict[str, ModelInfo] = {}
def _register_builtin_models() -> None:
"""Register built-in models.
Note: Pricing is fetched dynamically from LiteLLM's database.
"""
# ============================================================
# OpenAI Models
# ============================================================
# GPT-4o family
_MODELS["gpt-4o"] = ModelInfo(
name="gpt-4o",
provider="openai",
context_window=128000,
max_output_tokens=16384,
supports_tools=True,
supports_vision=True,
supports_streaming=True,
tokenizer_backend="tiktoken",
aliases=("gpt-4o-2024-11-20", "gpt-4o-2024-08-06", "gpt-4o-2024-05-13"),
notes="Latest GPT-4o with vision and tools",
)
_MODELS["gpt-4o-mini"] = ModelInfo(
name="gpt-4o-mini",
provider="openai",
context_window=128000,
max_output_tokens=16384,
supports_tools=True,
supports_vision=True,
supports_streaming=True,
tokenizer_backend="tiktoken",
aliases=("gpt-4o-mini-2024-07-18",),
notes="Cost-effective GPT-4o variant",
)
# o1 reasoning models
_MODELS["o1"] = ModelInfo(
name="o1",
provider="openai",
context_window=200000,
max_output_tokens=100000,
supports_tools=True,
supports_vision=True,
supports_streaming=True,
tokenizer_backend="tiktoken",
notes="Full reasoning model with extended thinking",
)
_MODELS["o1-mini"] = ModelInfo(
name="o1-mini",
provider="openai",
context_window=128000,
max_output_tokens=65536,
supports_tools=True,
supports_vision=False,
supports_streaming=True,
tokenizer_backend="tiktoken",
notes="Fast reasoning model",
)
_MODELS["o3-mini"] = ModelInfo(
name="o3-mini",
provider="openai",
context_window=200000,
max_output_tokens=100000,
supports_tools=True,
supports_vision=True,
supports_streaming=True,
tokenizer_backend="tiktoken",
notes="Latest reasoning model",
)
# GPT-4 Turbo
_MODELS["gpt-4-turbo"] = ModelInfo(
name="gpt-4-turbo",
provider="openai",
context_window=128000,
max_output_tokens=4096,
supports_tools=True,
supports_vision=True,
supports_streaming=True,
tokenizer_backend="tiktoken",
aliases=("gpt-4-turbo-preview", "gpt-4-turbo-2024-04-09"),
notes="GPT-4 Turbo with vision",
)
# GPT-4
_MODELS["gpt-4"] = ModelInfo(
name="gpt-4",
provider="openai",
context_window=8192,
max_output_tokens=4096,
supports_tools=True,
supports_vision=False,
supports_streaming=True,
tokenizer_backend="tiktoken",
aliases=("gpt-4-0613",),
notes="Original GPT-4",
)
_MODELS["gpt-4-32k"] = ModelInfo(
name="gpt-4-32k",
provider="openai",
context_window=32768,
max_output_tokens=4096,
supports_tools=True,
supports_vision=False,
supports_streaming=True,
tokenizer_backend="tiktoken",
notes="Extended context GPT-4",
)
# GPT-3.5
_MODELS["gpt-3.5-turbo"] = ModelInfo(
name="gpt-3.5-turbo",
provider="openai",
context_window=16385,
max_output_tokens=4096,
supports_tools=True,
supports_vision=False,
supports_streaming=True,
tokenizer_backend="tiktoken",
aliases=("gpt-3.5-turbo-0125", "gpt-3.5-turbo-1106"),
notes="Fast and cost-effective",
)
# ============================================================
# Anthropic Models
# ============================================================
_MODELS["claude-3-5-sonnet-20241022"] = ModelInfo(
name="claude-3-5-sonnet-20241022",
provider="anthropic",
context_window=200000,
max_output_tokens=8192,
supports_tools=True,
supports_vision=True,
supports_streaming=True,
tokenizer_backend="anthropic",
aliases=("claude-3-5-sonnet-latest", "claude-sonnet-4-20250514"),
notes="Claude 3.5 Sonnet - Best balance of speed and capability",
)
_MODELS["claude-3-5-haiku-20241022"] = ModelInfo(
name="claude-3-5-haiku-20241022",
provider="anthropic",
context_window=200000,
max_output_tokens=8192,
supports_tools=True,
supports_vision=True,
supports_streaming=True,
tokenizer_backend="anthropic",
aliases=("claude-3-5-haiku-latest",),
notes="Claude 3.5 Haiku - Fast and cost-effective",
)
_MODELS["claude-3-opus-20240229"] = ModelInfo(
name="claude-3-opus-20240229",
provider="anthropic",
context_window=200000,
max_output_tokens=4096,
supports_tools=True,
supports_vision=True,
supports_streaming=True,
tokenizer_backend="anthropic",
aliases=("claude-3-opus-latest",),
notes="Claude 3 Opus - Most capable",
)
_MODELS["claude-3-haiku-20240307"] = ModelInfo(
name="claude-3-haiku-20240307",
provider="anthropic",
context_window=200000,
max_output_tokens=4096,
supports_tools=True,
supports_vision=True,
supports_streaming=True,
tokenizer_backend="anthropic",
notes="Claude 3 Haiku - Legacy fast model",
)
# ============================================================
# Google Models
# ============================================================
_MODELS["gemini-2.0-flash"] = ModelInfo(
name="gemini-2.0-flash",
provider="google",
context_window=1000000,
max_output_tokens=8192,
supports_tools=True,
supports_vision=True,
supports_streaming=True,
tokenizer_backend="google",
aliases=("gemini-2.0-flash-exp",),
notes="Gemini 2.0 Flash - Fast multimodal",
)
_MODELS["gemini-1.5-pro"] = ModelInfo(
name="gemini-1.5-pro",
provider="google",
context_window=2000000,
max_output_tokens=8192,
supports_tools=True,
supports_vision=True,
supports_streaming=True,
tokenizer_backend="google",
aliases=("gemini-1.5-pro-latest",),
notes="Gemini 1.5 Pro - 2M context window",
)
_MODELS["gemini-1.5-flash"] = ModelInfo(
name="gemini-1.5-flash",
provider="google",
context_window=1000000,
max_output_tokens=8192,
supports_tools=True,
supports_vision=True,
supports_streaming=True,
tokenizer_backend="google",
aliases=("gemini-1.5-flash-latest",),
notes="Gemini 1.5 Flash - Cost-effective",
)
# ============================================================
# Meta Llama Models (open source)
# ============================================================
_MODELS["llama-3.3-70b"] = ModelInfo(
name="llama-3.3-70b",
provider="meta",
context_window=128000,
max_output_tokens=4096,
supports_tools=True,
supports_vision=False,
supports_streaming=True,
tokenizer_backend="huggingface",
aliases=("llama-3.3-70b-instruct", "meta-llama/Llama-3.3-70B-Instruct"),
notes="Llama 3.3 70B - Open source",
)
_MODELS["llama-3.1-405b"] = ModelInfo(
name="llama-3.1-405b",
provider="meta",
context_window=128000,
max_output_tokens=4096,
supports_tools=True,
supports_vision=False,
supports_streaming=True,
tokenizer_backend="huggingface",
aliases=("llama-3.1-405b-instruct", "meta-llama/Llama-3.1-405B-Instruct"),
notes="Llama 3.1 405B - Largest open source",
)
_MODELS["llama-3.1-70b"] = ModelInfo(
name="llama-3.1-70b",
provider="meta",
context_window=128000,
max_output_tokens=4096,
supports_tools=True,
supports_vision=False,
supports_streaming=True,
tokenizer_backend="huggingface",
aliases=("llama-3.1-70b-instruct", "meta-llama/Llama-3.1-70B-Instruct"),
notes="Llama 3.1 70B",
)
_MODELS["llama-3.1-8b"] = ModelInfo(
name="llama-3.1-8b",
provider="meta",
context_window=128000,
max_output_tokens=4096,
supports_tools=True,
supports_vision=False,
supports_streaming=True,
tokenizer_backend="huggingface",
aliases=("llama-3.1-8b-instruct", "meta-llama/Llama-3.1-8B-Instruct"),
notes="Llama 3.1 8B - Fast and efficient",
)
# ============================================================
# Mistral Models
# ============================================================
_MODELS["mistral-large"] = ModelInfo(
name="mistral-large",
provider="mistral",
context_window=128000,
max_output_tokens=4096,
supports_tools=True,
supports_vision=False,
supports_streaming=True,
tokenizer_backend="huggingface",
aliases=("mistral-large-latest",),
notes="Mistral Large - Best capability",
)
_MODELS["mistral-small"] = ModelInfo(
name="mistral-small",
provider="mistral",
context_window=32768,
max_output_tokens=4096,
supports_tools=True,
supports_vision=False,
supports_streaming=True,
tokenizer_backend="huggingface",
aliases=("mistral-small-latest",),
notes="Mistral Small - Cost-effective",
)
_MODELS["mixtral-8x7b"] = ModelInfo(
name="mixtral-8x7b",
provider="mistral",
context_window=32768,
max_output_tokens=4096,
supports_tools=True,
supports_vision=False,
supports_streaming=True,
tokenizer_backend="huggingface",
aliases=("mixtral-8x7b-instruct",),
notes="Mixtral 8x7B - MoE architecture",
)
_MODELS["mistral-7b"] = ModelInfo(
name="mistral-7b",
provider="mistral",
context_window=32768,
max_output_tokens=4096,
supports_tools=False,
supports_vision=False,
supports_streaming=True,
tokenizer_backend="huggingface",
aliases=("mistral-7b-instruct",),
notes="Mistral 7B - Open source",
)
# ============================================================
# DeepSeek Models
# ============================================================
_MODELS["deepseek-v3"] = ModelInfo(
name="deepseek-v3",
provider="deepseek",
context_window=128000,
max_output_tokens=8192,
supports_tools=True,
supports_vision=False,
supports_streaming=True,
tokenizer_backend="huggingface",
notes="DeepSeek V3 - High performance, low cost",
)
_MODELS["deepseek-coder"] = ModelInfo(
name="deepseek-coder",
provider="deepseek",
context_window=16384,
max_output_tokens=4096,
supports_tools=False,
supports_vision=False,
supports_streaming=True,
tokenizer_backend="huggingface",
notes="DeepSeek Coder - Specialized for code",
)
# ============================================================
# Qwen Models
# ============================================================
_MODELS["qwen2.5-72b"] = ModelInfo(
name="qwen2.5-72b",
provider="alibaba",
context_window=131072,
max_output_tokens=8192,
supports_tools=True,
supports_vision=False,
supports_streaming=True,
tokenizer_backend="huggingface",
aliases=("qwen2.5-72b-instruct",),
notes="Qwen 2.5 72B - Strong multilingual",
)
_MODELS["qwen2.5-7b"] = ModelInfo(
name="qwen2.5-7b",
provider="alibaba",
context_window=131072,
max_output_tokens=8192,
supports_tools=True,
supports_vision=False,
supports_streaming=True,
tokenizer_backend="huggingface",
aliases=("qwen2.5-7b-instruct",),
notes="Qwen 2.5 7B - Efficient",
)
# Initialize built-in models
_register_builtin_models()
# Build alias lookup
_ALIASES: dict[str, str] = {}
for model_name, info in _MODELS.items():
for alias in info.aliases:
_ALIASES[alias.lower()] = model_name
class ModelRegistry:
"""Registry of LLM models and their capabilities.
Singleton registry providing access to model information.
Supports built-in models and custom registration.
Example:
# Get model info
info = ModelRegistry.get("gpt-4o")
print(f"Context: {info.context_window}")
# Register custom model
ModelRegistry.register(
"my-model",
provider="custom",
context_window=32000,
)
# List models by provider
openai_models = ModelRegistry.list_models(provider="openai")
"""
@classmethod
def get(cls, model: str) -> ModelInfo | None:
"""Get model information.
Args:
model: Model name or alias.
Returns:
ModelInfo if found, None otherwise.
"""
model_lower = model.lower()
# Direct lookup
if model_lower in _MODELS:
return _MODELS[model_lower]
# Alias lookup
if model_lower in _ALIASES:
return _MODELS[_ALIASES[model_lower]]
# Prefix matching
for name, info in _MODELS.items():
if model_lower.startswith(name):
return info
return None
@classmethod
def register(
cls,
model: str,
provider: str,
context_window: int = 128000,
**kwargs: Any,
) -> ModelInfo:
"""Register a custom model.
Args:
model: Model name.
provider: Provider name.
context_window: Maximum context window.
**kwargs: Additional ModelInfo fields.
Returns:
Registered ModelInfo.
"""
info = ModelInfo(
name=model,
provider=provider,
context_window=context_window,
**kwargs,
)
_MODELS[model.lower()] = info
# Register aliases
for alias in info.aliases:
_ALIASES[alias.lower()] = model.lower()
return info
@classmethod
def list_models(
cls,
provider: str | None = None,
supports_tools: bool | None = None,
supports_vision: bool | None = None,
min_context: int | None = None,
) -> list[ModelInfo]:
"""List models matching criteria.
Args:
provider: Filter by provider.
supports_tools: Filter by tool support.
supports_vision: Filter by vision support.
min_context: Minimum context window.
Returns:
List of matching ModelInfo.
"""
results = []
for info in _MODELS.values():
if provider and info.provider != provider:
continue
if supports_tools is not None and info.supports_tools != supports_tools:
continue
if supports_vision is not None and info.supports_vision != supports_vision:
continue
if min_context and info.context_window < min_context:
continue
results.append(info)
return results
@classmethod
def list_providers(cls) -> list[str]:
"""List all known providers.
Returns:
List of provider names.
"""
return list({info.provider for info in _MODELS.values()})
@classmethod
def get_context_limit(cls, model: str, default: int = 128000) -> int:
"""Get context limit for a model.
Args:
model: Model name.
default: Default if model not found.
Returns:
Context window size.
"""
info = cls.get(model)
return info.context_window if info else default
@classmethod
def estimate_cost(
cls,
model: str,
input_tokens: int,
output_tokens: int,
cached_tokens: int = 0,
) -> float | None:
"""Estimate API cost for a model using LiteLLM's pricing database.
Args:
model: Model name.
input_tokens: Number of input tokens.
output_tokens: Number of output tokens.
cached_tokens: Number of cached input tokens (not currently used).
Returns:
Estimated cost in USD, or None if pricing unknown.
"""
# Use LiteLLM's pricing database
return litellm_estimate_cost(model, input_tokens, output_tokens)
@classmethod
def get_pricing(cls, model: str) -> tuple[float, float] | None:
"""Get pricing for a model from LiteLLM's database.
Args:
model: Model name.
Returns:
Tuple of (input_cost_per_1m, output_cost_per_1m) or None if not found.
"""
pricing = get_model_pricing(model)
if pricing is None:
return None
return (pricing.input_cost_per_1m, pricing.output_cost_per_1m)
# Convenience functions
def get_model_info(model: str) -> ModelInfo | None:
"""Get information about a model.
Args:
model: Model name or alias.
Returns:
ModelInfo if found, None otherwise.
"""
return ModelRegistry.get(model)
def list_models(
provider: str | None = None,
**kwargs: Any,
) -> list[ModelInfo]:
"""List models matching criteria.
Args:
provider: Filter by provider.
**kwargs: Additional filter criteria.
Returns:
List of matching ModelInfo.
"""
return ModelRegistry.list_models(provider=provider, **kwargs)
def register_model(
model: str,
provider: str,
context_window: int = 128000,
**kwargs: Any,
) -> ModelInfo:
"""Register a custom model.
Args:
model: Model name.
provider: Provider name.
context_window: Maximum context window.
**kwargs: Additional ModelInfo fields.
Returns:
Registered ModelInfo.
"""
return ModelRegistry.register(model, provider, context_window, **kwargs)
|