Image API 生成器
| 11 | |
| 12 | |
| 13 | class ImageApiGenerator(ImageGeneratorBase): |
| 14 | """Image API 生成器""" |
| 15 | |
| 16 | def __init__(self, config: Dict[str, Any]): |
| 17 | super().__init__(config) |
| 18 | logger.debug("初始化 ImageApiGenerator...") |
| 19 | self.policy = ImageProviderPolicy.from_config( |
| 20 | config, |
| 21 | default_model='default-model', |
| 22 | default_endpoint='/v1/images/generations', |
| 23 | ) |
| 24 | self.client = ImageApiClient(self.policy) |
| 25 | self.base_url = self.policy.base_url |
| 26 | self.model = self.policy.model |
| 27 | self.default_aspect_ratio = config.get('default_aspect_ratio', '3:4') |
| 28 | self.image_size = config.get('image_size', '4K') |
| 29 | self.endpoint_type = self.policy.endpoint_type |
| 30 | |
| 31 | logger.info(f"ImageApiGenerator 初始化完成: base_url={self.base_url}, model={self.model}, endpoint={self.endpoint_type}") |
| 32 | |
| 33 | def validate_config(self) -> bool: |
| 34 | """验证配置是否有效""" |
| 35 | if not self.api_key: |
| 36 | logger.error("Image API Key 未配置") |
| 37 | raise ValueError( |
| 38 | "Image API Key 未配置。\n" |
| 39 | "解决方案:在系统设置页面编辑该服务商,填写 API Key" |
| 40 | ) |
| 41 | return True |
| 42 | |
| 43 | def get_supported_sizes(self) -> List[str]: |
| 44 | """获取支持的图片尺寸""" |
| 45 | return ["1K", "2K", "4K"] |
| 46 | |
| 47 | def get_supported_aspect_ratios(self) -> List[str]: |
| 48 | """获取支持的宽高比""" |
| 49 | return ["1:1", "2:3", "3:2", "3:4", "4:3", "4:5", "5:4", "9:16", "16:9", "21:9"] |
| 50 | |
| 51 | def generate_image( |
| 52 | self, |
| 53 | prompt: str, |
| 54 | aspect_ratio: str = None, |
| 55 | temperature: float = 1.0, |
| 56 | model: str = None, |
| 57 | reference_image: Optional[bytes] = None, |
| 58 | reference_images: Optional[List[bytes]] = None, |
| 59 | **kwargs |
| 60 | ) -> bytes: |
| 61 | """ |
| 62 | 生成图片 |
| 63 | |
| 64 | Args: |
| 65 | prompt: 图片描述 |
| 66 | aspect_ratio: 宽高比 |
| 67 | temperature: 创意度(未使用,保留接口兼容) |
| 68 | model: 模型名称 |
| 69 | reference_image: 单张参考图片数据(向后兼容) |
| 70 | reference_images: 多张参考图片数据列表 |
nothing calls this directly
no outgoing calls
no test coverage detected