diff --git a/.env b/.env index aa264b2..8695945 100644 --- a/.env +++ b/.env @@ -5,6 +5,8 @@ HUAWEI_API_ENDPOINT=http://10.100.31.26:3001/v1/chat/completions # API密钥 HUAWEI_API_KEY=sk-PoeiV3qwyTIRqcVc84E8E24cD2904872859a87922e0d9186 +HUAWEI_API_TIMEOUT=900 + # 模型名称 HUAWEI_MODEL=DeepSeek-R1-Distill-Llama-70B diff --git a/services/ai_service.py b/services/ai_service.py index b68d9d7..8c13fcf 100644 --- a/services/ai_service.py +++ b/services/ai_service.py @@ -19,6 +19,11 @@ class AIService: self.huawei_api_key = os.getenv('HUAWEI_API_KEY', 'sk-PoeiV3qwyTIRqcVc84E8E24cD2904872859a87922e0d9186') self.huawei_model = os.getenv('HUAWEI_MODEL', 'DeepSeek-R1-Distill-Llama-70B') + # API超时配置(秒) + # 开启思考模式时,响应时间会显著增加,需要更长的超时时间 + # 可以通过环境变量 HUAWEI_API_TIMEOUT 自定义,默认180秒(3分钟) + self.api_timeout = int(os.getenv('HUAWEI_API_TIMEOUT', '180')) + # 确定使用的AI服务 self.ai_provider = self._determine_ai_provider() @@ -122,14 +127,14 @@ class AIService: 至少重试3次,总共最多尝试4次 """ max_retries = 3 # 最多重试3次,总共4次尝试 - retry_delay = 1 # 重试延迟(秒),每次重试延迟递增 + retry_delay = 2 # 重试延迟(秒),每次重试延迟递增(从2秒开始) last_exception = None for attempt in range(max_retries + 1): # 0, 1, 2, 3 (总共4次) try: if attempt > 0: - # 重试前等待,延迟时间递增(1秒、2秒、3秒) + # 重试前等待,延迟时间递增(2秒、4秒、6秒) wait_time = retry_delay * attempt print(f"[AI服务] 第 {attempt} 次重试,等待 {wait_time} 秒后重试...") time.sleep(wait_time) @@ -219,11 +224,23 @@ class AIService: "Content-Type": "application/json" } + # 根据是否开启思考模式动态调整超时时间 + # 开启思考模式时,模型需要更多时间进行推理,超时时间需要更长 + enable_thinking = payload.get('enable_thinking', False) + if enable_thinking: + # 思考模式:使用配置的超时时间(默认180秒) + timeout = self.api_timeout + print(f"[AI服务] 思考模式已开启,使用超时时间: {timeout}秒") + else: + # 非思考模式:使用较短的超时时间 + timeout = min(self.api_timeout, 120) # 最多120秒 + print(f"[AI服务] 思考模式未开启,使用超时时间: {timeout}秒") + response = requests.post( self.huawei_api_endpoint, json=payload, headers=headers, - timeout=60 + timeout=timeout ) if response.status_code != 200: