Update TGW.py (#762)

更新api未响应处理
This commit is contained in:
Smzh 2024-05-23 15:00:50 +08:00 committed by GitHub
parent 22379db61f
commit a4db0c981a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -164,13 +164,25 @@ class TS(basetrans):
history_prompt = self.get_history("zh") history_prompt = self.get_history("zh")
payload = self.make_request_stream(context, history_zh=history_prompt) payload = self.make_request_stream(context, history_zh=history_prompt)
response = requests.post(url, timeout=self.timeout, json=payload, stream=True) try:
if response.status_code == 200: response = requests.post(url, timeout=self.timeout, json=payload, stream=True)
for line in response.iter_lines(): if response.status_code == 200:
if line: for line in response.iter_lines():
if line.startswith(b"data: "): if line:
line = line[len(b"data: "):] if line.startswith(b"data: "):
payload = json.loads(line) line = line[len(b"data: "):]
chunk = payload['choices'][0]['delta']['content'] payload = json.loads(line)
yield chunk 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}")