2023-10-05 06:13:37 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-11-13 20:58:52 +03:00
|
|
|
import re
|
2024-01-01 01:35:11 +03:00
|
|
|
import json
|
2023-10-05 06:13:37 +03:00
|
|
|
from aiohttp import ClientSession
|
|
|
|
|
2024-01-01 01:35:11 +03:00
|
|
|
from ..typing import Messages, AsyncResult
|
|
|
|
from .base_provider import AsyncGeneratorProvider
|
|
|
|
from .helper import get_random_string
|
2023-10-05 06:13:37 +03:00
|
|
|
|
2024-01-01 01:35:11 +03:00
|
|
|
class Chatgpt4Online(AsyncGeneratorProvider):
|
2023-10-27 23:59:14 +03:00
|
|
|
url = "https://chatgpt4online.org"
|
|
|
|
supports_message_history = True
|
2023-10-05 06:13:37 +03:00
|
|
|
supports_gpt_35_turbo = True
|
2023-11-13 20:58:52 +03:00
|
|
|
working = True
|
|
|
|
_wpnonce = None
|
2023-10-05 06:13:37 +03:00
|
|
|
|
|
|
|
@classmethod
|
2024-01-01 01:35:11 +03:00
|
|
|
async def create_async_generator(
|
2023-10-05 06:13:37 +03:00
|
|
|
cls,
|
|
|
|
model: str,
|
2023-10-09 11:22:17 +03:00
|
|
|
messages: Messages,
|
|
|
|
proxy: str = None,
|
2023-10-05 06:13:37 +03:00
|
|
|
**kwargs
|
2024-01-01 01:35:11 +03:00
|
|
|
) -> AsyncResult:
|
|
|
|
headers = {
|
|
|
|
"accept": "*/*",
|
|
|
|
"accept-language": "en-US",
|
|
|
|
"content-type": "application/json",
|
|
|
|
"sec-ch-ua": "\"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"",
|
|
|
|
"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 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
|
|
|
"referer": "https://chatgpt4online.org/",
|
|
|
|
"referrer-policy": "strict-origin-when-cross-origin"
|
|
|
|
}
|
|
|
|
async with ClientSession(headers=headers) as session:
|
2023-11-13 20:58:52 +03:00
|
|
|
if not cls._wpnonce:
|
|
|
|
async with session.get(f"{cls.url}/", proxy=proxy) as response:
|
|
|
|
response.raise_for_status()
|
|
|
|
response = await response.text()
|
2024-01-01 01:35:11 +03:00
|
|
|
result = re.search(r'restNonce":"(.*?)"', response)
|
2023-11-20 21:40:55 +03:00
|
|
|
if result:
|
2023-11-13 20:58:52 +03:00
|
|
|
cls._wpnonce = result.group(1)
|
|
|
|
else:
|
|
|
|
raise RuntimeError("No nonce found")
|
2023-10-05 06:13:37 +03:00
|
|
|
data = {
|
2024-01-01 01:35:11 +03:00
|
|
|
"botId":"default",
|
|
|
|
"customId":None,
|
|
|
|
"session":"N/A",
|
|
|
|
"chatId":get_random_string(11),
|
|
|
|
"contextId":58,
|
|
|
|
"messages":messages[:-1],
|
|
|
|
"newMessage":messages[-1]["content"],
|
|
|
|
"newImageId":None,
|
|
|
|
"stream":True
|
2023-10-05 06:13:37 +03:00
|
|
|
}
|
2024-01-01 01:35:11 +03:00
|
|
|
async with session.post(
|
|
|
|
f"{cls.url}/wp-json/mwai-ui/v1/chats/submit",
|
|
|
|
json=data,
|
|
|
|
proxy=proxy,
|
|
|
|
headers={"x-wp-nonce": cls._wpnonce}
|
|
|
|
) as response:
|
2023-10-05 06:13:37 +03:00
|
|
|
response.raise_for_status()
|
2024-01-01 01:35:11 +03:00
|
|
|
async for line in response.content:
|
|
|
|
if line.startswith(b"data: "):
|
|
|
|
line = json.loads(line[6:])
|
|
|
|
if "type" not in line:
|
|
|
|
raise RuntimeError(f"Response: {line}")
|
|
|
|
elif line["type"] == "live":
|
|
|
|
yield line["data"]
|
|
|
|
elif line["type"] == "end":
|
|
|
|
break
|