From 3982f39424ea037aca1086d45c6f657b4bfc457c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=B2=98r=E1=B9=A8h=E0=B8=AA=E2=88=82ow?= <71973368+MrShadowDev@users.noreply.github.com> Date: Mon, 23 Oct 2023 09:46:25 +0200 Subject: 'Refactored by Sourcery' (#1125) Co-authored-by: Sourcery AI <> --- g4f/Provider/deprecated/EasyChat.py | 40 +++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 22 deletions(-) (limited to 'g4f/Provider/deprecated/EasyChat.py') diff --git a/g4f/Provider/deprecated/EasyChat.py b/g4f/Provider/deprecated/EasyChat.py index ffe9a785..bd49c09c 100644 --- a/g4f/Provider/deprecated/EasyChat.py +++ b/g4f/Provider/deprecated/EasyChat.py @@ -30,7 +30,7 @@ class EasyChat(BaseProvider): "https://chat4.fastgpt.me", "https://gxos1h1ddt.fastgpt.me" ] - + server = active_servers[kwargs.get("active_server", random.randint(0, 5))] headers = { "authority" : f"{server}".replace("https://", ""), @@ -68,30 +68,26 @@ class EasyChat(BaseProvider): response = session.post(f"{server}/api/openai/v1/chat/completions", headers=headers, json=json_data, stream=stream) - - if response.status_code == 200: - - if stream == False: - json_data = response.json() - - if "choices" in json_data: - yield json_data["choices"][0]["message"]["content"] - else: - raise Exception("No response from server") - + + if response.status_code != 200: + raise Exception(f"Error {response.status_code} from server : {response.reason}") + if not stream: + json_data = response.json() + + if "choices" in json_data: + yield json_data["choices"][0]["message"]["content"] else: + raise Exception("No response from server") + + else: - for chunk in response.iter_lines(): + for chunk in response.iter_lines(): - if b"content" in chunk: - splitData = chunk.decode().split("data:") - - if len(splitData) > 1: - yield json.loads(splitData[1])["choices"][0]["delta"]["content"] - else: - continue - else: - raise Exception(f"Error {response.status_code} from server : {response.reason}") + if b"content" in chunk: + splitData = chunk.decode().split("data:") + + if len(splitData) > 1: + yield json.loads(splitData[1])["choices"][0]["delta"]["content"] @classmethod -- cgit v1.2.3