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