调用Qwen API
(self, system_prompt: str, user_prompt: str)
| 209 | |
| 210 | @with_graceful_retry(SEARCH_API_RETRY_CONFIG, default_return={"success": False, "error": "API服务暂时不可用"}) |
| 211 | def _call_qwen_api(self, system_prompt: str, user_prompt: str) -> Dict[str, Any]: |
| 212 | """调用Qwen API""" |
| 213 | try: |
| 214 | current_time = datetime.now().strftime("%Y年%m月%d日%H时%M分") |
| 215 | time_prefix = f"今天的实际时间是{current_time}" |
| 216 | if user_prompt: |
| 217 | user_prompt = f"{time_prefix}\n{user_prompt}" |
| 218 | else: |
| 219 | user_prompt = time_prefix |
| 220 | |
| 221 | response = self.client.chat.completions.create( |
| 222 | model=self.model, |
| 223 | messages=[ |
| 224 | {"role": "system", "content": system_prompt}, |
| 225 | {"role": "user", "content": user_prompt} |
| 226 | ], |
| 227 | temperature=0.6, |
| 228 | top_p=0.9, |
| 229 | ) |
| 230 | |
| 231 | if response.choices: |
| 232 | content = response.choices[0].message.content |
| 233 | return {"success": True, "content": content} |
| 234 | else: |
| 235 | return {"success": False, "error": "API返回格式异常"} |
| 236 | except Exception as e: |
| 237 | return {"success": False, "error": f"API调用异常: {str(e)}"} |
| 238 | |
| 239 | def _format_host_speech(self, speech: str) -> str: |
| 240 | """格式化主持人发言""" |
no test coverage detected