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/Ails.py | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) (limited to 'g4f/Provider/Ails.py') diff --git a/g4f/Provider/Ails.py b/g4f/Provider/Ails.py index 4d072646..fae3e62a 100644 --- a/g4f/Provider/Ails.py +++ b/g4f/Provider/Ails.py @@ -44,8 +44,8 @@ class Ails(AsyncGeneratorProvider): "from-url": "https://ai.ls/?chat=1" } async with ClientSession( - headers=headers - ) as session: + headers=headers + ) as session: timestamp = _format_timestamp(int(time.time() * 1000)) json_data = { "model": "gpt-3.5-turbo", @@ -57,10 +57,10 @@ class Ails(AsyncGeneratorProvider): "s": _hash({"t": timestamp, "m": messages[-1]["content"]}), } async with session.post( - "https://api.caipacity.com/v1/chat/completions", - proxy=proxy, - json=json_data - ) as response: + "https://api.caipacity.com/v1/chat/completions", + proxy=proxy, + json=json_data + ) as response: response.raise_for_status() start = "data: " async for line in response.content: @@ -68,10 +68,9 @@ class Ails(AsyncGeneratorProvider): if line.startswith(start) and line != "data: [DONE]": line = line[len(start):-1] line = json.loads(line) - token = line["choices"][0]["delta"].get("content") - if token: + if token := line["choices"][0]["delta"].get("content"): if "ai.ls" in token or "ai.ci" in token: - raise Exception("Response Error: " + token) + raise Exception(f"Response Error: {token}") yield token @@ -89,12 +88,7 @@ class Ails(AsyncGeneratorProvider): def _hash(json_data: dict[str, str]) -> SHA256: - base_string: str = "%s:%s:%s:%s" % ( - json_data["t"], - json_data["m"], - "WI,2rU#_r:r~aF4aJ36[.Z(/8Rv93Rf", - len(json_data["m"]), - ) + base_string: str = f'{json_data["t"]}:{json_data["m"]}:WI,2rU#_r:r~aF4aJ36[.Z(/8Rv93Rf:{len(json_data["m"])}' return SHA256(hashlib.sha256(base_string.encode()).hexdigest()) -- cgit v1.2.3