归一化后的图片服务商能力与调度配置。
| 5 | |
| 6 | @dataclass(frozen=True) |
| 7 | class ImageProviderPolicy: |
| 8 | """归一化后的图片服务商能力与调度配置。""" |
| 9 | |
| 10 | api_key: str |
| 11 | base_url: str |
| 12 | model: str |
| 13 | endpoint_type: str |
| 14 | max_concurrent: int = 1 |
| 15 | request_interval_seconds: float = 3.0 |
| 16 | high_concurrency: bool = False |
| 17 | response_format: Optional[str] = None |
| 18 | |
| 19 | @classmethod |
| 20 | def from_config( |
| 21 | cls, |
| 22 | config: Dict[str, Any], |
| 23 | default_model: str = "default-model", |
| 24 | default_endpoint: str = "/v1/images/generations", |
| 25 | ) -> "ImageProviderPolicy": |
| 26 | base_url = (config.get("base_url") or "https://api.example.com").rstrip("/") |
| 27 | if base_url.endswith("/v1"): |
| 28 | base_url = base_url[:-3] |
| 29 | |
| 30 | endpoint_type = cls.normalize_endpoint( |
| 31 | config.get("endpoint_type") or default_endpoint |
| 32 | ) |
| 33 | |
| 34 | max_concurrent = _positive_int(config.get("max_concurrent"), default=1) |
| 35 | request_interval = _non_negative_float( |
| 36 | config.get("request_interval_seconds"), |
| 37 | default=3.0, |
| 38 | ) |
| 39 | |
| 40 | response_format = config.get("response_format") |
| 41 | if response_format in ("", None): |
| 42 | response_format = None |
| 43 | |
| 44 | return cls( |
| 45 | api_key=config.get("api_key", ""), |
| 46 | base_url=base_url, |
| 47 | model=config.get("model") or default_model, |
| 48 | endpoint_type=endpoint_type, |
| 49 | max_concurrent=max_concurrent, |
| 50 | request_interval_seconds=request_interval, |
| 51 | high_concurrency=bool(config.get("high_concurrency", False)), |
| 52 | response_format=response_format, |
| 53 | ) |
| 54 | |
| 55 | @property |
| 56 | def is_chat_endpoint(self) -> bool: |
| 57 | endpoint = self.endpoint_type.lower() |
| 58 | return "chat" in endpoint or "completions" in endpoint |
| 59 | |
| 60 | @property |
| 61 | def worker_count(self) -> int: |
| 62 | if self.high_concurrency and self.max_concurrent > 1: |
| 63 | return self.max_concurrent |
| 64 | return 1 |
nothing calls this directly
no outgoing calls
no test coverage detected