This commit is contained in:
恍兮惚兮 2024-09-04 11:08:54 +08:00
parent c8a8c661c9
commit 3d588bdb4c
2 changed files with 16 additions and 14 deletions

View File

@ -83,15 +83,16 @@ class gptcommon(basetrans):
# text/html
raise Exception(response.text)
for chunk in response.iter_lines():
response_data = chunk.decode("utf-8").strip()
response_data: str = chunk.decode("utf-8").strip()
if not response_data.startswith("data: "):
continue
response_data = response_data[6:]
if not response_data:
continue
if response_data == "data: [DONE]":
if response_data == "[DONE]":
break
if response_data == ": OPENROUTER PROCESSING":
continue
try:
json_data = json.loads(response_data[6:])
json_data = json.loads(response_data)
rs = json_data["choices"][0].get("finish_reason")
if rs and rs != "null":
break

View File

@ -191,18 +191,19 @@ class TS(basetrans):
raise ValueError(f"连接到Sakura API超时{self.api_url}")
if not output.headers["Content-Type"].startswith("text/event-stream"):
raise Exception(output.text)
for o in output.iter_lines():
o = o.decode("utf-8").strip()
if o == "data: [DONE]":
for chunk in output.iter_lines():
response_data: str = chunk.decode("utf-8").strip()
if not response_data.startswith("data: "):
continue
response_data = response_data[6:]
if not response_data:
continue
if response_data == "[DONE]":
break
try:
res = o[6:]
# print(res)
if res == "":
continue
res = json.loads(res)
res = json.loads(response_data)
except:
raise Exception(o)
raise Exception(response_data)
yield res