summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/ChatgptAi.py
diff options
context:
space:
mode:
authorrazrab <razrab@PimoLin>2023-10-23 07:25:46 +0200
committerrazrab <razrab@PimoLin>2023-10-23 07:25:46 +0200
commit2b3bc749b39b0c6f3e8921b9e3b2fc52263c568c (patch)
tree7de51129865bd786bdf7b21fa658725a474a8cd8 /g4f/Provider/ChatgptAi.py
parentAdded code to ignore certain providers in the API (diff)
parent~ | g4f `v-0.1.7.5` (diff)
downloadgpt4free-2b3bc749b39b0c6f3e8921b9e3b2fc52263c568c.tar
gpt4free-2b3bc749b39b0c6f3e8921b9e3b2fc52263c568c.tar.gz
gpt4free-2b3bc749b39b0c6f3e8921b9e3b2fc52263c568c.tar.bz2
gpt4free-2b3bc749b39b0c6f3e8921b9e3b2fc52263c568c.tar.lz
gpt4free-2b3bc749b39b0c6f3e8921b9e3b2fc52263c568c.tar.xz
gpt4free-2b3bc749b39b0c6f3e8921b9e3b2fc52263c568c.tar.zst
gpt4free-2b3bc749b39b0c6f3e8921b9e3b2fc52263c568c.zip
Diffstat (limited to 'g4f/Provider/ChatgptAi.py')
-rw-r--r--g4f/Provider/ChatgptAi.py72
1 files changed, 38 insertions, 34 deletions
diff --git a/g4f/Provider/ChatgptAi.py b/g4f/Provider/ChatgptAi.py
index cf45909c..d5fd5bff 100644
--- a/g4f/Provider/ChatgptAi.py
+++ b/g4f/Provider/ChatgptAi.py
@@ -1,36 +1,34 @@
from __future__ import annotations
-import re
+import re, html, json, string, random
from aiohttp import ClientSession
-from ..typing import Messages
-from .base_provider import AsyncProvider, format_prompt
+from ..typing import Messages, AsyncResult
+from .base_provider import AsyncGeneratorProvider
-class ChatgptAi(AsyncProvider):
- url: str = "https://chatgpt.ai/"
+class ChatgptAi(AsyncGeneratorProvider):
+ url: str = "https://chatgpt.ai"
working = True
supports_gpt_35_turbo = True
- _nonce = None
- _post_id = None
- _bot_id = None
+ _system = None
@classmethod
- async def create_async(
+ async def create_async_generator(
cls,
model: str,
messages: Messages,
proxy: str = None,
**kwargs
- ) -> str:
+ ) -> AsyncResult:
headers = {
"authority" : "chatgpt.ai",
"accept" : "*/*",
- "accept-language" : "en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3",
+ "accept-language" : "en-US",
"cache-control" : "no-cache",
- "origin" : "https://chatgpt.ai",
+ "origin" : cls.url,
"pragma" : "no-cache",
- "referer" : cls.url,
+ "referer" : f"{cls.url}/",
"sec-ch-ua" : '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
"sec-ch-ua-mobile" : "?0",
"sec-ch-ua-platform" : '"Windows"',
@@ -42,34 +40,40 @@ class ChatgptAi(AsyncProvider):
async with ClientSession(
headers=headers
) as session:
- if not cls._nonce:
+ if not cls._system:
async with session.get(cls.url, proxy=proxy) as response:
response.raise_for_status()
text = await response.text()
- result = re.search(r'data-nonce="(.*?)"', text)
+ result = re.search(r"data-system='(.*?)'", 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")
-
+ cls._system = json.loads(html.unescape(result.group(1)))
+ if not cls._system:
+ raise RuntimeError("System args not found")
+
data = {
- "_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
+ "botId": cls._system["botId"],
+ "customId": cls._system["customId"],
+ "session": cls._system["sessionId"],
+ "chatId": "".join(random.choices(f"{string.ascii_lowercase}{string.digits}", k=11)),
+ "contextId": cls._system["contextId"],
+ "messages": messages,
+ "newMessage": messages[-1]["content"],
+ "stream": True
}
async with session.post(
- "https://chatgpt.ai/wp-admin/admin-ajax.php",
+ f"{cls.url}/wp-json/mwai-ui/v1/chats/submit",
proxy=proxy,
- data=data
+ json=data
) as response:
response.raise_for_status()
- return (await response.json())["data"] \ No newline at end of file
+ async for line in response.content:
+ if line.startswith(b"data: "):
+ try:
+ line = json.loads(line[6:])
+ assert "type" in line
+ except:
+ raise RuntimeError(f"Broken line: {line.decode()}")
+ if line["type"] == "live":
+ yield line["data"]
+ elif line["type"] == "end":
+ break \ No newline at end of file