From d8fa4c3d7e467301e62b1753326e48e6095500af Mon Sep 17 00:00:00 2001 From: python Date: Tue, 9 Dec 2025 11:58:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0API=E8=B6=85=E6=97=B6?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=EF=BC=8C=E6=94=AF=E6=8C=81=E6=80=9D=E8=80=83?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E4=B8=8B=E5=8A=A8=E6=80=81=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E6=97=B6=E9=97=B4=EF=BC=9B=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E9=87=8D=E8=AF=95=E6=9C=BA=E5=88=B6=E7=9A=84=E5=BB=B6=E8=BF=9F?= =?UTF-8?q?=E6=97=B6=E9=97=B4=EF=BC=8C=E4=BB=8E1=E7=A7=92=E6=94=B9?= =?UTF-8?q?=E4=B8=BA2=E7=A7=92=EF=BC=8C=E5=A2=9E=E5=BC=BA=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 2 ++ services/ai_service.py | 23 ++++++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) 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: