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 # text/html
raise Exception(response.text) raise Exception(response.text)
for chunk in response.iter_lines(): 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: if not response_data:
continue continue
if response_data == "data: [DONE]": if response_data == "[DONE]":
break break
if response_data == ": OPENROUTER PROCESSING":
continue
try: try:
json_data = json.loads(response_data[6:]) json_data = json.loads(response_data)
rs = json_data["choices"][0].get("finish_reason") rs = json_data["choices"][0].get("finish_reason")
if rs and rs != "null": if rs and rs != "null":
break break

View File

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