2023-09-03 08:26:26 +00:00
|
|
|
from __future__ import annotations
|
2023-07-28 10:07:17 +00:00
|
|
|
|
2023-10-22 15:13:13 +00:00
|
|
|
import re, html, json, string, random
|
2023-10-07 07:02:48 +00:00
|
|
|
from aiohttp import ClientSession
|
2023-09-03 08:26:26 +00:00
|
|
|
|
2023-10-22 15:13:13 +00:00
|
|
|
from ..typing import Messages, AsyncResult
|
|
|
|
from .base_provider import AsyncGeneratorProvider
|
2023-09-03 08:26:26 +00:00
|
|
|
|
2023-07-28 10:07:17 +00:00
|
|
|
|
2023-10-22 15:13:13 +00:00
|
|
|
class ChatgptAi(AsyncGeneratorProvider):
|
2023-10-27 20:59:14 +00:00
|
|
|
url = "https://chatgpt.ai"
|
2024-02-27 10:55:40 +00:00
|
|
|
working = False
|
2023-10-27 20:59:14 +00:00
|
|
|
supports_message_history = True
|
2023-09-05 15:27:24 +00:00
|
|
|
supports_gpt_35_turbo = True
|
2023-10-22 15:13:13 +00:00
|
|
|
_system = None
|
2023-07-28 10:07:17 +00:00
|
|
|
|
2023-09-05 15:27:24 +00:00
|
|
|
@classmethod
|
2023-10-22 15:13:13 +00:00
|
|
|
async def create_async_generator(
|
2023-09-05 15:27:24 +00:00
|
|
|
cls,
|
2023-07-28 10:07:17 +00:00
|
|
|
model: str,
|
2023-10-09 08:22:17 +00:00
|
|
|
messages: Messages,
|
2023-09-05 15:27:24 +00:00
|
|
|
proxy: str = None,
|
|
|
|
**kwargs
|
2023-10-22 15:13:13 +00:00
|
|
|
) -> AsyncResult:
|
2023-07-28 10:07:17 +00:00
|
|
|
headers = {
|
2023-08-27 15:37:44 +00:00
|
|
|
"authority" : "chatgpt.ai",
|
|
|
|
"accept" : "*/*",
|
2023-10-22 15:13:13 +00:00
|
|
|
"accept-language" : "en-US",
|
2023-08-27 15:37:44 +00:00
|
|
|
"cache-control" : "no-cache",
|
2023-10-22 15:13:13 +00:00
|
|
|
"origin" : cls.url,
|
2023-08-27 15:37:44 +00:00
|
|
|
"pragma" : "no-cache",
|
2023-10-22 15:13:13 +00:00
|
|
|
"referer" : f"{cls.url}/",
|
2023-08-27 15:37:44 +00:00
|
|
|
"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"',
|
|
|
|
"sec-fetch-dest" : "empty",
|
|
|
|
"sec-fetch-mode" : "cors",
|
|
|
|
"sec-fetch-site" : "same-origin",
|
|
|
|
"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",
|
2023-07-28 10:07:17 +00:00
|
|
|
}
|
2023-09-05 15:27:24 +00:00
|
|
|
async with ClientSession(
|
2023-10-23 07:46:25 +00:00
|
|
|
headers=headers
|
|
|
|
) as session:
|
2023-10-22 15:13:13 +00:00
|
|
|
if not cls._system:
|
2023-09-05 15:27:24 +00:00
|
|
|
async with session.get(cls.url, proxy=proxy) as response:
|
|
|
|
response.raise_for_status()
|
2023-10-05 03:13:37 +00:00
|
|
|
text = await response.text()
|
2023-11-20 18:40:55 +00:00
|
|
|
|
|
|
|
result = re.search(r"data-system='(.*?)'", text)
|
|
|
|
if result :
|
2023-10-22 15:13:13 +00:00
|
|
|
cls._system = json.loads(html.unescape(result.group(1)))
|
2023-10-23 07:46:25 +00:00
|
|
|
if not cls._system:
|
|
|
|
raise RuntimeError("System args not found")
|
|
|
|
|
2023-09-05 15:27:24 +00:00
|
|
|
data = {
|
2023-10-22 15:13:13 +00:00
|
|
|
"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
|
2023-09-05 15:27:24 +00:00
|
|
|
}
|
|
|
|
async with session.post(
|
2023-10-22 15:13:13 +00:00
|
|
|
f"{cls.url}/wp-json/mwai-ui/v1/chats/submit",
|
2023-09-05 15:27:24 +00:00
|
|
|
proxy=proxy,
|
2023-11-22 20:21:57 +00:00
|
|
|
json=data,
|
|
|
|
headers={"X-Wp-Nonce": cls._system["restNonce"]}
|
2023-09-05 15:27:24 +00:00
|
|
|
) as response:
|
|
|
|
response.raise_for_status()
|
2023-10-22 15:13:13 +00:00
|
|
|
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
|