|
| 1 | +# bud-model-catalog |
| 2 | + |
| 3 | +Multi-source LLM model catalog with cost-accurate pricing. Fetches model metadata from [LiteLLM](https://github.com/BerriAI/litellm) and [truefoundry/models](https://github.com/truefoundry/models), merges them with cost-accurate pricing, filters deprecated models, and returns a unified catalog keyed by TensorZero provider/model. |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +```bash |
| 8 | +pip install bud-model-catalog |
| 9 | +``` |
| 10 | + |
| 11 | +## Quick Start |
| 12 | + |
| 13 | +```python |
| 14 | +from bud_model_catalog import CatalogClient |
| 15 | + |
| 16 | +# Synchronous usage |
| 17 | +result = CatalogClient().fetch_catalog_sync() |
| 18 | +print(f"Fetched {len(result.models)} models") |
| 19 | +print(f"Stats: {result.stats}") |
| 20 | +``` |
| 21 | + |
| 22 | +## Async Usage |
| 23 | + |
| 24 | +```python |
| 25 | +import asyncio |
| 26 | +from bud_model_catalog import CatalogClient, CatalogConfig |
| 27 | + |
| 28 | +async def main(): |
| 29 | + config = CatalogConfig(include_deprecated=True, timeout=60) |
| 30 | + client = CatalogClient(config) |
| 31 | + result = await client.fetch_catalog() |
| 32 | + |
| 33 | + for key, model in list(result.models.items())[:5]: |
| 34 | + print(f"{key}: input={model.get('input_cost_per_token')}") |
| 35 | + |
| 36 | +asyncio.run(main()) |
| 37 | +``` |
| 38 | + |
| 39 | +Or use the module-level convenience function: |
| 40 | + |
| 41 | +```python |
| 42 | +from bud_model_catalog import fetch_catalog |
| 43 | + |
| 44 | +result = await fetch_catalog() |
| 45 | +``` |
| 46 | + |
| 47 | +## Configuration |
| 48 | + |
| 49 | +All options are passed via `CatalogConfig`: |
| 50 | + |
| 51 | +| Field | Type | Default | Description | |
| 52 | +|-------|------|---------|-------------| |
| 53 | +| `litellm_url` | `str` | GitHub raw URL | URL to the LiteLLM model prices JSON | |
| 54 | +| `ai_models_url` | `str` | GitHub archive URL | URL to the truefoundry/models ZIP archive | |
| 55 | +| `timeout` | `int` | `30` | HTTP request timeout in seconds (must be > 0) | |
| 56 | +| `include_deprecated` | `bool` | `False` | Whether to include deprecated models in output | |
| 57 | +| `max_retries` | `int` | `2` | Maximum retry attempts per HTTP request (with exponential backoff) | |
| 58 | +| `cache` | `bool` | `True` | Enable ETag-based conditional GET caching across calls | |
| 59 | + |
| 60 | +```python |
| 61 | +from bud_model_catalog import CatalogConfig |
| 62 | + |
| 63 | +config = CatalogConfig( |
| 64 | + timeout=60, |
| 65 | + include_deprecated=True, |
| 66 | + max_retries=3, |
| 67 | + cache=True, |
| 68 | +) |
| 69 | +``` |
| 70 | + |
| 71 | +Validation is enforced at construction time: |
| 72 | + |
| 73 | +```python |
| 74 | +CatalogConfig(timeout=-1) # ValueError: timeout must be positive |
| 75 | +CatalogConfig(litellm_url="not-a-url") # ValueError: must be an HTTP(S) URL |
| 76 | +``` |
| 77 | + |
| 78 | +## Error Handling |
| 79 | + |
| 80 | +```python |
| 81 | +from bud_model_catalog import CatalogClient, CatalogConfig, SourceFetchError |
| 82 | + |
| 83 | +try: |
| 84 | + result = CatalogClient().fetch_catalog_sync() |
| 85 | +except SourceFetchError as e: |
| 86 | + print(f"Failed to fetch data: {e}") |
| 87 | +``` |
| 88 | + |
| 89 | +- `SourceFetchError` — raised when LiteLLM fetch fails (HTTP error, invalid JSON, timeout) |
| 90 | +- ai-models failures are handled gracefully — the SDK falls back to LiteLLM-only costs |
| 91 | + |
| 92 | +## API Reference |
| 93 | + |
| 94 | +### `CatalogClient` |
| 95 | + |
| 96 | +Main entry point for fetching the catalog. |
| 97 | + |
| 98 | +- `CatalogClient(config=None)` — create a client with optional `CatalogConfig` |
| 99 | +- `await client.fetch_catalog()` — async fetch, returns `CatalogResult` |
| 100 | +- `client.fetch_catalog_sync()` — sync wrapper, safe in both sync and async contexts |
| 101 | + |
| 102 | +### `CatalogResult` |
| 103 | + |
| 104 | +Pydantic model returned from fetch operations. |
| 105 | + |
| 106 | +- `models: dict[str, dict]` — merged model catalog keyed by `{provider}/{model}` |
| 107 | +- `stats: MergeStats` — merge statistics |
| 108 | +- `litellm_fetched_at: datetime` — timestamp of LiteLLM fetch |
| 109 | +- `ai_models_fetched_at: datetime | None` — timestamp of ai-models fetch (None if failed/skipped) |
| 110 | + |
| 111 | +### `MergeStats` |
| 112 | + |
| 113 | +- `total_litellm` — total models from LiteLLM source |
| 114 | +- `total_output` — models in final output |
| 115 | +- `matched` — models matched with ai-models data |
| 116 | +- `unmatched` — models without ai-models match |
| 117 | +- `deprecated_removed` — models filtered as deprecated |
| 118 | +- `cost_fields_updated` — individual cost field values updated from ai-models |
| 119 | + |
| 120 | +## Logging |
| 121 | + |
| 122 | +The SDK uses Python's `logging` module. Enable output to see fetch/merge details: |
| 123 | + |
| 124 | +```python |
| 125 | +import logging |
| 126 | +logging.basicConfig(level=logging.INFO) |
| 127 | +``` |
| 128 | + |
| 129 | +Key log messages: |
| 130 | +- `INFO` — fetch counts, merge statistics, cache hits |
| 131 | +- `WARNING` — ai-models fallback, malformed YAML files skipped, retry attempts |
| 132 | + |
| 133 | +## Development |
| 134 | + |
| 135 | +```bash |
| 136 | +# Install dev dependencies |
| 137 | +pip install -e ".[dev]" |
| 138 | + |
| 139 | +# Run tests |
| 140 | +pytest -v |
| 141 | + |
| 142 | +# Lint |
| 143 | +ruff check src/ tests/ |
| 144 | +``` |
0 commit comments