From dc04ca93060443a3ce6263a476f4dafc66afc6b3 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sat, 28 Oct 2023 07:21:00 +0200 Subject: Add arkose_token to OpenaiChat --- g4f/Provider/needs_auth/OpenaiChat.py | 49 ++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) (limited to 'g4f/Provider/needs_auth') diff --git a/g4f/Provider/needs_auth/OpenaiChat.py b/g4f/Provider/needs_auth/OpenaiChat.py index 14619958..eaccb7f1 100644 --- a/g4f/Provider/needs_auth/OpenaiChat.py +++ b/g4f/Provider/needs_auth/OpenaiChat.py @@ -1,11 +1,13 @@ from __future__ import annotations -import uuid, json, time +import uuid, json, time, os +import tempfile, subprocess, shutil from ..base_provider import AsyncGeneratorProvider from ..helper import get_browser, get_cookies, format_prompt, get_event_loop from ...typing import AsyncResult, Messages from ...requests import StreamSession +from ... import debug class OpenaiChat(AsyncGeneratorProvider): url = "https://chat.openai.com" @@ -47,6 +49,7 @@ class OpenaiChat(AsyncGeneratorProvider): ] data = { "action": "next", + "arkose_token": await get_arkose_token(proxy), "messages": messages, "conversation_id": None, "parent_message_id": str(uuid.uuid4()), @@ -65,7 +68,11 @@ class OpenaiChat(AsyncGeneratorProvider): line = json.loads(line) except: continue - if "message" not in line or "message_type" not in line["message"]["metadata"]: + if "message" not in line: + continue + if "error" in line and line["error"]: + raise RuntimeError(line["error"]) + if "message_type" not in line["message"]["metadata"]: continue if line["message"]["metadata"]["message_type"] == "next": new_message = line["message"]["content"]["parts"][0] @@ -134,4 +141,40 @@ class OpenaiChat(AsyncGeneratorProvider): ("cookies", "dict[str, str]") ] param = ", ".join([": ".join(p) for p in params]) - return f"g4f.provider.{cls.__name__} supports: ({param})" \ No newline at end of file + return f"g4f.provider.{cls.__name__} supports: ({param})" + +async def get_arkose_token(proxy: str = None) -> str: + node = shutil.which("node") + if not node: + if debug.logging: + print('OpenaiChat: "node" not found') + return + dir = os.path.dirname(os.path.dirname(__file__)) + include = f'{dir}/npm/node_modules/funcaptcha' + config = { + "pkey": "3D86FBBA-9D22-402A-B512-3420086BA6CC", + "surl": "https://tcr9i.chat.openai.com", + "data": {}, + "headers": { + "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36' + }, + "site": "https://chat.openai.com", + "proxy": proxy + } + source = """ +fun = require({include}) +config = {config} +fun.getToken(config).then(token => { + console.log(token.token) +}) +""" + source = source.replace('{include}', json.dumps(include)) + source = source.replace('{config}', json.dumps(config)) + tmp = tempfile.NamedTemporaryFile(delete=False) + tmp.write(source.encode()) + tmp.close() + try: + p = subprocess.Popen([node, tmp.name], stdout=subprocess.PIPE) + return p.stdout.read().decode() + finally: + os.unlink(tmp.name) \ No newline at end of file -- cgit v1.2.3