|
3 | 3 | import json |
4 | 4 | from dataclasses import asdict, dataclass, field |
5 | 5 | from pathlib import Path |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +import yaml |
6 | 9 |
|
7 | 10 | from . import __version__ |
8 | 11 |
|
9 | 12 |
|
| 13 | +def load_model_config( |
| 14 | + model: str, config_path: Path = Path("config/models.yaml") |
| 15 | +) -> dict[str, Any]: |
| 16 | + """Load model-specific configuration from models.yaml. |
| 17 | +
|
| 18 | + Merges model-specific settings with global defaults to create final |
| 19 | + parameters for OpenAI client requests. |
| 20 | +
|
| 21 | + Args: |
| 22 | + model: Model identifier (e.g., 'openai/gpt-oss-20b') |
| 23 | + config_path: Path to models.yaml file |
| 24 | +
|
| 25 | + Returns: |
| 26 | + Dictionary containing merged configuration for the model |
| 27 | +
|
| 28 | + Raises: |
| 29 | + FileNotFoundError: If config file doesn't exist |
| 30 | + KeyError: If model not found in configuration |
| 31 | + """ |
| 32 | + if not config_path.exists(): |
| 33 | + raise FileNotFoundError(f"Model config file not found: {config_path}") |
| 34 | + with config_path.open() as f: |
| 35 | + config = yaml.safe_load(f) |
| 36 | + |
| 37 | + model_config = None |
| 38 | + for model_entry in config.get("models", []): |
| 39 | + if model_entry.get("model") == model: |
| 40 | + model_config = model_entry.copy() |
| 41 | + break |
| 42 | + if model_config is None: |
| 43 | + raise KeyError(f"Model '{model}' not found in {config_path}") |
| 44 | + |
| 45 | + defaults = config.get("defaults", {}) |
| 46 | + |
| 47 | + # Start with defaults, then merge model config |
| 48 | + # For nested dicts like extra_body, we need deep merging |
| 49 | + final_config = defaults.copy() |
| 50 | + |
| 51 | + for key, value in model_config.items(): |
| 52 | + if ( |
| 53 | + key in final_config |
| 54 | + and isinstance(final_config[key], dict) |
| 55 | + and isinstance(value, dict) |
| 56 | + ): |
| 57 | + # Deep merge for nested dicts |
| 58 | + final_config[key] = {**final_config[key], **value} |
| 59 | + else: |
| 60 | + final_config[key] = value |
| 61 | + |
| 62 | + return final_config |
| 63 | + |
| 64 | + |
10 | 65 | @dataclass |
11 | 66 | class Config: |
12 | 67 | """Configuration for LLMBot. |
|
0 commit comments