summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/ChatgptAi.py
diff options
context:
space:
mode:
authorTekky <98614666+xtekky@users.noreply.github.com>2023-10-05 20:06:21 +0200
committerGitHub <noreply@github.com>2023-10-05 20:06:21 +0200
commita59dc2fb36b42be735d52db24016f29f09cefff6 (patch)
tree76240685a9dcc320535cf0ffb3a5b931b2a14b72 /g4f/Provider/ChatgptAi.py
parent~ | Merge pull request #984 from HexyeDEV/patch-2 (diff)
parentMerge branch 'main' into bom (diff)
downloadgpt4free-a59dc2fb36b42be735d52db24016f29f09cefff6.tar
gpt4free-a59dc2fb36b42be735d52db24016f29f09cefff6.tar.gz
gpt4free-a59dc2fb36b42be735d52db24016f29f09cefff6.tar.bz2
gpt4free-a59dc2fb36b42be735d52db24016f29f09cefff6.tar.lz
gpt4free-a59dc2fb36b42be735d52db24016f29f09cefff6.tar.xz
gpt4free-a59dc2fb36b42be735d52db24016f29f09cefff6.tar.zst
gpt4free-a59dc2fb36b42be735d52db24016f29f09cefff6.zip
Diffstat (limited to 'g4f/Provider/ChatgptAi.py')
-rw-r--r--g4f/Provider/ChatgptAi.py62
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