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/needs_auth/Bard.py | 14 ++++++------- g4f/Provider/needs_auth/Raycast.py | 10 ++++------ g4f/Provider/needs_auth/Theb.py | 40 +++++++++++++++++++------------------- 3 files changed, 31 insertions(+), 33 deletions(-) (limited to 'g4f/Provider/needs_auth') diff --git a/g4f/Provider/needs_auth/Bard.py b/g4f/Provider/needs_auth/Bard.py index fe9777db..092268f8 100644 --- a/g4f/Provider/needs_auth/Bard.py +++ b/g4f/Provider/needs_auth/Bard.py @@ -38,18 +38,18 @@ class Bard(AsyncProvider): 'x-same-domain': '1', } async with ClientSession( - cookies=cookies, - headers=headers - ) as session: + cookies=cookies, + headers=headers + ) as session: if not cls._snlm0e: async with session.get(cls.url, proxy=proxy) as response: text = await response.text() - match = re.search(r'SNlM0e\":\"(.*?)\"', text) - if not match: + if match := re.search(r'SNlM0e\":\"(.*?)\"', text): + cls._snlm0e = match.group(1) + + else: raise RuntimeError("No snlm0e value.") - cls._snlm0e = match.group(1) - params = { 'bl': 'boq_assistant-bard-web-server_20230326.21_p0', '_reqid': random.randint(1111, 9999), diff --git a/g4f/Provider/needs_auth/Raycast.py b/g4f/Provider/needs_auth/Raycast.py index 4b448985..4570fd9f 100644 --- a/g4f/Provider/needs_auth/Raycast.py +++ b/g4f/Provider/needs_auth/Raycast.py @@ -32,12 +32,10 @@ class Raycast(BaseProvider): 'Content-Type': 'application/json', 'User-Agent': 'Raycast/0 CFNetwork/1410.0.3 Darwin/22.6.0', } - parsed_messages = [] - for message in messages: - parsed_messages.append({ - 'author': message['role'], - 'content': {'text': message['content']} - }) + parsed_messages = [ + {'author': message['role'], 'content': {'text': message['content']}} + for message in messages + ] data = { "debug": False, "locale": "en-CN", diff --git a/g4f/Provider/needs_auth/Theb.py b/g4f/Provider/needs_auth/Theb.py index 9803db97..b3c9019d 100644 --- a/g4f/Provider/needs_auth/Theb.py +++ b/g4f/Provider/needs_auth/Theb.py @@ -28,28 +28,28 @@ class Theb(BaseProvider): "bearer_token":"free", "org_id":"theb", }) - + bearer_token = auth["bearer_token"] org_id = auth["org_id"] - + headers = { - 'authority' : 'beta.theb.ai', - 'accept' : 'text/event-stream', - 'accept-language' : 'id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7', - 'authorization' : 'Bearer '+bearer_token, - 'content-type' : 'application/json', - 'origin' : 'https://beta.theb.ai', - 'referer' : 'https://beta.theb.ai/home', - 'sec-ch-ua' : '"Chromium";v="116", "Not)A;Brand";v="24", "Google Chrome";v="116"', - 'sec-ch-ua-mobile' : '?0', + 'authority': 'beta.theb.ai', + 'accept': 'text/event-stream', + 'accept-language': 'id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7', + 'authorization': f'Bearer {bearer_token}', + 'content-type': 'application/json', + 'origin': 'https://beta.theb.ai', + 'referer': 'https://beta.theb.ai/home', + 'sec-ch-ua': '"Chromium";v="116", "Not)A;Brand";v="24", "Google Chrome";v="116"', + 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"Windows"', - 'sec-fetch-dest' : 'empty', - 'sec-fetch-mode' : 'cors', - 'sec-fetch-site' : 'same-origin', - 'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36', - 'x-ai-model' : 'ee8d4f29cb7047f78cbe84313ed6ace8', + 'sec-fetch-dest': 'empty', + 'sec-fetch-mode': 'cors', + 'sec-fetch-site': 'same-origin', + 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36', + 'x-ai-model': 'ee8d4f29cb7047f78cbe84313ed6ace8', } - + req_rand = random.randint(100000000, 9999999999) json_data: dict[str, Any] = { @@ -65,7 +65,7 @@ class Theb(BaseProvider): "long_term_memory" : "auto" } } - + response = requests.post( f"https://beta.theb.ai/api/conversation?org_id={org_id}&req_rand={req_rand}", headers=headers, @@ -73,7 +73,7 @@ class Theb(BaseProvider): stream=True, proxies={"https": proxy} ) - + response.raise_for_status() content = "" next_content = "" @@ -82,7 +82,7 @@ class Theb(BaseProvider): next_content = content data = json.loads(chunk.decode().split("data: ")[1]) content = data["content"] - yield data["content"].replace(next_content, "") + yield content.replace(next_content, "") @classmethod @property -- cgit v1.2.3