From a4db0c981a419b4c36dd89f64105e0f45344f38a Mon Sep 17 00:00:00 2001 From: Smzh <129963508+HunterShenSmzh@users.noreply.github.com> Date: Thu, 23 May 2024 15:00:50 +0800 Subject: [PATCH] Update TGW.py (#762) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新api未响应处理 --- .../LunaTranslator/translator/TGW.py | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/LunaTranslator/LunaTranslator/translator/TGW.py b/LunaTranslator/LunaTranslator/translator/TGW.py index 097567af..782ef070 100644 --- a/LunaTranslator/LunaTranslator/translator/TGW.py +++ b/LunaTranslator/LunaTranslator/translator/TGW.py @@ -164,13 +164,25 @@ class TS(basetrans): history_prompt = self.get_history("zh") payload = self.make_request_stream(context, history_zh=history_prompt) - response = requests.post(url, timeout=self.timeout, json=payload, stream=True) - if response.status_code == 200: - for line in response.iter_lines(): - if line: - if line.startswith(b"data: "): - line = line[len(b"data: "):] - payload = json.loads(line) - chunk = payload['choices'][0]['delta']['content'] - yield chunk + try: + response = requests.post(url, timeout=self.timeout, json=payload, stream=True) + if response.status_code == 200: + for line in response.iter_lines(): + if line: + if line.startswith(b"data: "): + line = line[len(b"data: "):] + payload = json.loads(line) + chunk = payload['choices'][0]['delta']['content'] + yield chunk + + else: + raise ValueError(f"API无响应") + except requests.Timeout as e: + raise ValueError( + f"连接到TGW超时:{self.api_url},当前最大连接时间为: {self.timeout},请尝试修改参数。" + ) + + except Exception as e: + print(e) + raise ValueError(f"无法连接到TGW:{e}")