diff options
author | H Lohaus <hlohaus@users.noreply.github.com> | 2023-11-15 18:25:28 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-15 18:25:28 +0100 |
commit | f04e415becbf45b1ef5a250e39f838be7608c59a (patch) | |
tree | e6712e4ed552a98d60ef8a4a6bd9d48d85a04bd7 /g4f/Provider/Chatgpt4Online.py | |
parent | Merge pull request #1251 from guspan-tanadi/READMENotesection (diff) | |
parent | Fix Chatgpt4Online Provider (diff) | |
download | gpt4free-f04e415becbf45b1ef5a250e39f838be7608c59a.tar gpt4free-f04e415becbf45b1ef5a250e39f838be7608c59a.tar.gz gpt4free-f04e415becbf45b1ef5a250e39f838be7608c59a.tar.bz2 gpt4free-f04e415becbf45b1ef5a250e39f838be7608c59a.tar.lz gpt4free-f04e415becbf45b1ef5a250e39f838be7608c59a.tar.xz gpt4free-f04e415becbf45b1ef5a250e39f838be7608c59a.tar.zst gpt4free-f04e415becbf45b1ef5a250e39f838be7608c59a.zip |
Diffstat (limited to 'g4f/Provider/Chatgpt4Online.py')
-rw-r--r-- | g4f/Provider/Chatgpt4Online.py | 48 |
1 files changed, 25 insertions, 23 deletions
diff --git a/g4f/Provider/Chatgpt4Online.py b/g4f/Provider/Chatgpt4Online.py index d7509639..57ab9482 100644 --- a/g4f/Provider/Chatgpt4Online.py +++ b/g4f/Provider/Chatgpt4Online.py @@ -1,42 +1,44 @@ from __future__ import annotations -import json +import re from aiohttp import ClientSession -from ..typing import AsyncResult, Messages -from .base_provider import AsyncGeneratorProvider +from ..typing import Messages +from .base_provider import AsyncProvider +from .helper import format_prompt - -class Chatgpt4Online(AsyncGeneratorProvider): +class Chatgpt4Online(AsyncProvider): url = "https://chatgpt4online.org" supports_message_history = True supports_gpt_35_turbo = True - working = False + working = True + _wpnonce = None @classmethod - async def create_async_generator( + async def create_async( cls, model: str, messages: Messages, proxy: str = None, **kwargs - ) -> AsyncResult: + ) -> str: async with ClientSession() as session: + if not cls._wpnonce: + async with session.get(f"{cls.url}/", proxy=proxy) as response: + response.raise_for_status() + response = await response.text() + if result := re.search(r'data-nonce="(.*?)"', response): + cls._wpnonce = result.group(1) + else: + raise RuntimeError("No nonce found") data = { - "botId": "default", - "customId": None, - "session": "N/A", - "chatId": "", - "contextId": 58, - "messages": messages, - "newMessage": messages[-1]["content"], - "stream": True + "_wpnonce": cls._wpnonce, + "post_id": 58, + "url": "https://chatgpt4online.org", + "action": "wpaicg_chat_shortcode_message", + "message": format_prompt(messages), + "bot_id": 3405 } - - async with session.post(f"{cls.url}/wp-json/mwai-ui/v1/chats/submit", json=data, proxy=proxy) as response: + async with session.post(f"{cls.url}/rizq", data=data, proxy=proxy) as response: response.raise_for_status() - async for line in response.content: - if line.startswith(b"data: "): - line = json.loads(line[6:]) - if line["type"] == "live": - yield line["data"]
\ No newline at end of file + return (await response.json())["data"]
\ No newline at end of file |