生成标题、文案和标签 参数: topic: 用户输入的主题 outline: 大纲内容 返回: 包含 titles, copywriting, tags 的字典
(
self,
topic: str,
outline: str
)
| 132 | raise ValueError("AI 返回的内容格式不正确,无法解析") |
| 133 | |
| 134 | def generate_content( |
| 135 | self, |
| 136 | topic: str, |
| 137 | outline: str |
| 138 | ) -> Dict[str, Any]: |
| 139 | """ |
| 140 | 生成标题、文案和标签 |
| 141 | |
| 142 | 参数: |
| 143 | topic: 用户输入的主题 |
| 144 | outline: 大纲内容 |
| 145 | |
| 146 | 返回: |
| 147 | 包含 titles, copywriting, tags 的字典 |
| 148 | """ |
| 149 | try: |
| 150 | logger.info(f"开始生成内容: topic={topic[:50]}...") |
| 151 | |
| 152 | # 构建提示词 |
| 153 | prompt = self.prompt_template.format( |
| 154 | topic=topic, |
| 155 | outline=outline |
| 156 | ) |
| 157 | |
| 158 | # 从配置中获取模型参数 |
| 159 | active_provider = self.text_config.get('active_provider', 'google_gemini') |
| 160 | providers = self.text_config.get('providers', {}) |
| 161 | provider_config = providers.get(active_provider, {}) |
| 162 | |
| 163 | model = provider_config.get('model', 'gemini-2.0-flash-exp') |
| 164 | temperature = provider_config.get('temperature', 1.0) |
| 165 | max_output_tokens = provider_config.get('max_output_tokens', 4000) |
| 166 | |
| 167 | logger.info(f"调用文本生成 API: model={model}, temperature={temperature}") |
| 168 | response_text = self.client.generate_text( |
| 169 | prompt=prompt, |
| 170 | model=model, |
| 171 | temperature=temperature, |
| 172 | max_output_tokens=max_output_tokens |
| 173 | ) |
| 174 | |
| 175 | logger.debug(f"API 返回文本长度: {len(response_text)} 字符") |
| 176 | |
| 177 | # 解析 JSON 响应 |
| 178 | content_data = self._parse_json_response(response_text) |
| 179 | |
| 180 | # 验证必要字段 |
| 181 | titles = content_data.get('titles', []) |
| 182 | copywriting = content_data.get('copywriting', '') |
| 183 | tags = content_data.get('tags', []) |
| 184 | |
| 185 | # 确保 titles 是列表 |
| 186 | if isinstance(titles, str): |
| 187 | titles = [titles] |
| 188 | |
| 189 | # 确保 tags 是列表 |
| 190 | if isinstance(tags, str): |
| 191 | tags = [t.strip() for t in tags.split(',')] |
no test coverage detected