summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/ChatgptAi.py
diff options
context:
space:
mode:
authorCommenter123321 <36051603+Commenter123321@users.noreply.github.com>2023-10-09 18:02:06 +0200
committerCommenter123321 <36051603+Commenter123321@users.noreply.github.com>2023-10-09 18:02:06 +0200
commit119817c96349807efaf87ee432ce46446542b66a (patch)
tree1dbdf4d4dbf4f6c8a8247274ef500a2f1de765d1 /g4f/Provider/ChatgptAi.py
parentaivvm's no life creator keeps patching it, but I'm just better 😉 (diff)
parentMerge branch 'main' of https://github.com/xtekky/gpt4free (diff)
downloadgpt4free-119817c96349807efaf87ee432ce46446542b66a.tar
gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar.gz
gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar.bz2
gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar.lz
gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar.xz
gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar.zst
gpt4free-119817c96349807efaf87ee432ce46446542b66a.zip
Diffstat (limited to '')
-rw-r--r--g4f/Provider/ChatgptAi.py59
1 files changed, 29 insertions, 30 deletions
diff --git a/g4f/Provider/ChatgptAi.py b/g4f/Provider/ChatgptAi.py
index e6416cc3..996f99a5 100644
--- a/g4f/Provider/ChatgptAi.py
+++ b/g4f/Provider/ChatgptAi.py
@@ -1,28 +1,27 @@
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,
**kwargs
- ) -> AsyncGenerator:
+ ) -> str:
headers = {
"authority" : "chatgpt.ai",
"accept" : "*/*",
@@ -42,34 +41,34 @@ class ChatgptAi(AsyncGeneratorProvider):
async with ClientSession(
headers=headers
) 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