内容生成服务:生成标题、文案、标签
| 17 | |
| 18 | |
| 19 | class ContentService: |
| 20 | """内容生成服务:生成标题、文案、标签""" |
| 21 | |
| 22 | def __init__(self): |
| 23 | logger.debug("初始化 ContentService...") |
| 24 | self.text_config = self._load_text_config() |
| 25 | self.client = self._get_client() |
| 26 | self.prompt_template = self._load_prompt_template() |
| 27 | logger.info(f"ContentService 初始化完成,使用服务商: {self.text_config.get('active_provider')}") |
| 28 | |
| 29 | def _load_text_config(self) -> dict: |
| 30 | """加载文本生成配置""" |
| 31 | config_path = Path(__file__).parent.parent.parent / 'text_providers.yaml' |
| 32 | logger.debug(f"加载文本配置: {config_path}") |
| 33 | |
| 34 | if config_path.exists(): |
| 35 | try: |
| 36 | with open(config_path, 'r', encoding='utf-8') as f: |
| 37 | config = yaml.safe_load(f) or {} |
| 38 | logger.debug(f"文本配置加载成功: active={config.get('active_provider')}") |
| 39 | return config |
| 40 | except yaml.YAMLError as e: |
| 41 | logger.error(f"文本配置 YAML 解析失败: {e}") |
| 42 | raise ValueError( |
| 43 | f"文本配置文件格式错误: text_providers.yaml\n" |
| 44 | f"YAML 解析错误: {e}\n" |
| 45 | "解决方案:检查 YAML 缩进和语法" |
| 46 | ) |
| 47 | |
| 48 | logger.warning("text_providers.yaml 不存在,使用默认配置") |
| 49 | return { |
| 50 | 'active_provider': 'google_gemini', |
| 51 | 'providers': { |
| 52 | 'google_gemini': { |
| 53 | 'type': 'google_gemini', |
| 54 | 'model': 'gemini-2.0-flash-exp', |
| 55 | 'temperature': 1.0, |
| 56 | 'max_output_tokens': 8000 |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | def _get_client(self): |
| 62 | """根据配置获取客户端""" |
| 63 | active_provider = self.text_config.get('active_provider', 'google_gemini') |
| 64 | providers = self.text_config.get('providers', {}) |
| 65 | |
| 66 | if not providers: |
| 67 | logger.error("未找到任何文本生成服务商配置") |
| 68 | raise ValueError( |
| 69 | "未找到任何文本生成服务商配置。\n" |
| 70 | "解决方案:\n" |
| 71 | "1. 在系统设置页面添加文本生成服务商\n" |
| 72 | "2. 或手动编辑 text_providers.yaml 文件" |
| 73 | ) |
| 74 | |
| 75 | if active_provider not in providers: |
| 76 | available = ', '.join(providers.keys()) |
no outgoing calls
no test coverage detected