添加API超时配置,支持思考模式下动态调整超时时间;修改重试机制的延迟时间,从1秒改为2秒,增强错误处理逻辑。

This commit is contained in:
python 2025-12-09 11:58:07 +08:00
parent c7a7780e71
commit d8fa4c3d7e
2 changed files with 22 additions and 3 deletions

2
.env
View File

@ -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

View File

@ -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: