diff --git a/LunaTranslator/LunaTranslator/translator/gptcommon.py b/LunaTranslator/LunaTranslator/translator/gptcommon.py index a82f46f8..6a3e3eba 100644 --- a/LunaTranslator/LunaTranslator/translator/gptcommon.py +++ b/LunaTranslator/LunaTranslator/translator/gptcommon.py @@ -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 diff --git a/LunaTranslator/LunaTranslator/translator/sakura.py b/LunaTranslator/LunaTranslator/translator/sakura.py index 3388550d..1f59957b 100644 --- a/LunaTranslator/LunaTranslator/translator/sakura.py +++ b/LunaTranslator/LunaTranslator/translator/sakura.py @@ -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