diff --git a/api/answer.py b/api/answer.py index 0e45a19d..84b322c9 100644 --- a/api/answer.py +++ b/api/answer.py @@ -1282,16 +1282,18 @@ def check_llm_connection(self) -> bool: with self._lock: logger.info(f'正在检查 {self.name} 连接...') try: + # 初始化客户端 if self.http_proxy: httpx_client = httpx.Client(proxy=self.http_proxy) client = OpenAI(http_client=httpx_client, base_url=self.endpoint, api_key=self.key) else: client = OpenAI(base_url=self.endpoint, api_key=self.key) - # 发送一个简单的测试请求 + # 限流等待 self._wait_for_interval() self.last_request_time = time.time() + # 发送测试请求 completion = client.chat.completions.create(**self._completion_kwargs( model=self.model, messages=[ @@ -1300,43 +1302,22 @@ def check_llm_connection(self) -> bool: 'content': '你好,请回答:1+1 等于几?只回答数字。' } ], - max_tokens=64 + max_tokens=200 # 增大以支持可能返回的 reasoning_content )) - if completion.choices and completion.choices[0].message.content: - logger.info(f'{self.name} 连接检查成功') - return True - else: - logger.error(f'{self.name} 连接检查失败:未收到响应') - return False + # 统一检查响应 + if completion.choices: + msg = completion.choices[0].message + if msg.content or getattr(msg, 'reasoning_content', None): + logger.info(f'{self.name} 连接检查成功') + return True - # 发送一个简单的测试请求 - self._wait_for_interval() - self.last_request_time = time.time() - completion = client.chat.completions.create(**self._completion_kwargs( - model=self.model, - messages=[ - { - 'role': 'user', - 'content': '你好,请回答:1+1 等于几?只回答数字。' - } - ], - max_tokens=200 - )) + logger.error(f'{self.name} 连接检查失败:未收到响应') + return False - if completion.choices: - msg = completion.choices[0].message - has_content = bool(msg.content) - has_reasoning = bool(getattr(msg, 'reasoning_content', None)) - if has_content or has_reasoning: - logger.info(f'{self.name} 连接检查成功') - return True - logger.error(f'{self.name} 连接检查失败:未收到响应') - return False - - except Exception as e: - logger.error(f'{self.name} 连接检查失败:{e}') - return False + except Exception as e: + logger.error(f'{self.name} 连接检查失败:{e}') + return False class SiliconFlow(Tiku): diff --git a/api/base.py b/api/base.py index 134f87ef..a600325d 100644 --- a/api/base.py +++ b/api/base.py @@ -936,7 +936,7 @@ def fetch_response_with_retry(): questions = decode_questions_info(_resp.text) if _resp.status_code == 200 and questions.get("questions"): - return (_resp, questions) + return _resp, questions logger.warning( f"无效响应 (Code: {getattr(_resp, 'status_code', 'Unknown')}), 重试中...") diff --git a/main.py b/main.py index c9f8e35f..5c712983 100644 --- a/main.py +++ b/main.py @@ -178,10 +178,10 @@ def build_config_from_args(args): "username": args.username, "password": args.password, "course_list": [item.strip() for item in args.list.split(",") if item.strip()] if args.list else None, - "speed": args.speed if args.speed else 1.0, + "speed": args.speed or 1.0, "jobs": args.jobs, - "notopen_action": args.notopen_action if args.notopen_action else "retry", - "retry_interval": args.retry_interval if args.retry_interval else 1.0 + "notopen_action": args.notopen_action or "retry", + "retry_interval": args.retry_interval or 1.0, "add_learning_count": args.add_learning_count, "target_count": args.target_count, }