diff options
author | Tekky <98614666+xtekky@users.noreply.github.com> | 2023-10-28 09:22:33 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-28 09:22:33 +0200 |
commit | 1a3b59838e21b98b5cfcafddf1737afb25129cfe (patch) | |
tree | b9c645cbe897af198ea0551509f901a249af35f2 /g4f/Provider/needs_auth | |
parent | Merge pull request #1176 from hlohaus/history (diff) | |
parent | Add arkose_token to OpenaiChat (diff) | |
download | gpt4free-1a3b59838e21b98b5cfcafddf1737afb25129cfe.tar gpt4free-1a3b59838e21b98b5cfcafddf1737afb25129cfe.tar.gz gpt4free-1a3b59838e21b98b5cfcafddf1737afb25129cfe.tar.bz2 gpt4free-1a3b59838e21b98b5cfcafddf1737afb25129cfe.tar.lz gpt4free-1a3b59838e21b98b5cfcafddf1737afb25129cfe.tar.xz gpt4free-1a3b59838e21b98b5cfcafddf1737afb25129cfe.tar.zst gpt4free-1a3b59838e21b98b5cfcafddf1737afb25129cfe.zip |
Diffstat (limited to '')
-rw-r--r-- | g4f/Provider/needs_auth/OpenaiChat.py | 49 |
1 files changed, 46 insertions, 3 deletions
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 |