diff options
Diffstat (limited to '')
-rw-r--r-- | g4f/Provider/ChatgptAi.py | 62 |
1 files changed, 31 insertions, 31 deletions
diff --git a/g4f/Provider/ChatgptAi.py b/g4f/Provider/ChatgptAi.py index e6416cc3..0ae0fa38 100644 --- a/g4f/Provider/ChatgptAi.py +++ b/g4f/Provider/ChatgptAi.py @@ -1,28 +1,28 @@ from __future__ import annotations import re -import html -import json from aiohttp import ClientSession -from ..typing import AsyncGenerator -from .base_provider import AsyncGeneratorProvider +from .base_provider import AsyncProvider, format_prompt -class ChatgptAi(AsyncGeneratorProvider): +class ChatgptAi(AsyncProvider): url: str = "https://chatgpt.ai/" working = True supports_gpt_35_turbo = True - _system_data = None + _nonce = None + _post_id = None + _bot_id = None @classmethod - async def create_async_generator( + async def create_async( cls, model: str, messages: list[dict[str, str]], proxy: str = None, + timeout: int = 30, **kwargs - ) -> AsyncGenerator: + ) -> str: headers = { "authority" : "chatgpt.ai", "accept" : "*/*", @@ -40,36 +40,36 @@ class ChatgptAi(AsyncGeneratorProvider): "user-agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36", } async with ClientSession( - headers=headers + headers=headers, timeout=timeout ) as session: - if not cls._system_data: + if not cls._nonce: async with session.get(cls.url, proxy=proxy) as response: response.raise_for_status() - match = re.findall(r"data-system='([^']+)'", await response.text()) - if not match: - raise RuntimeError("No system data") - cls._system_data = json.loads(html.unescape(match[0])) + text = await response.text() + result = re.search(r'data-nonce="(.*?)"', text) + if result: + cls._nonce = result.group(1) + result = re.search(r'data-post-id="(.*?)"', text) + if result: + cls._post_id = result.group(1) + result = re.search(r'data-bot-id="(.*?)"', text) + if result: + cls._bot_id = result.group(1) + if not cls._nonce or not cls._post_id or not cls._bot_id: + raise RuntimeError("Nonce, post-id or bot-id not found") data = { - "botId": cls._system_data["botId"], - "clientId": "", - "contextId": cls._system_data["contextId"], - "id": cls._system_data["id"], - "messages": messages[:-1], - "newMessage": messages[-1]["content"], - "session": cls._system_data["sessionId"], - "stream": True + "_wpnonce": cls._nonce, + "post_id": cls._post_id, + "url": "https://chatgpt.ai", + "action": "wpaicg_chat_shortcode_message", + "message": format_prompt(messages), + "bot_id": cls._bot_id } async with session.post( - "https://chatgpt.ai/wp-json/mwai-ui/v1/chats/submit", + "https://chatgpt.ai/wp-admin/admin-ajax.php", proxy=proxy, - json=data + data=data ) as response: response.raise_for_status() - start = "data: " - async for line in response.content: - line = line.decode('utf-8') - if line.startswith(start): - line = json.loads(line[len(start):-1]) - if line["type"] == "live": - yield line["data"]
\ No newline at end of file + return (await response.json())["data"]
\ No newline at end of file |