2023-10-05 03:13:37 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-11-13 17:58:52 +00:00
|
|
|
import re
|
2023-10-05 03:13:37 +00:00
|
|
|
from aiohttp import ClientSession
|
|
|
|
|
2023-11-13 17:58:52 +00:00
|
|
|
from ..typing import Messages
|
|
|
|
from .base_provider import AsyncProvider
|
|
|
|
from .helper import format_prompt
|
2023-10-05 03:13:37 +00:00
|
|
|
|
2023-11-13 17:58:52 +00:00
|
|
|
class Chatgpt4Online(AsyncProvider):
|
2023-10-27 20:59:14 +00:00
|
|
|
url = "https://chatgpt4online.org"
|
|
|
|
supports_message_history = True
|
2023-10-05 03:13:37 +00:00
|
|
|
supports_gpt_35_turbo = True
|
2023-11-13 17:58:52 +00:00
|
|
|
working = True
|
|
|
|
_wpnonce = None
|
2023-10-05 03:13:37 +00:00
|
|
|
|
|
|
|
@classmethod
|
2023-11-13 17:58:52 +00:00
|
|
|
async def create_async(
|
2023-10-05 03:13:37 +00:00
|
|
|
cls,
|
|
|
|
model: str,
|
2023-10-09 08:22:17 +00:00
|
|
|
messages: Messages,
|
|
|
|
proxy: str = None,
|
2023-10-05 03:13:37 +00:00
|
|
|
**kwargs
|
2023-11-13 17:58:52 +00:00
|
|
|
) -> str:
|
2023-10-05 03:13:37 +00:00
|
|
|
async with ClientSession() as session:
|
2023-11-13 17:58:52 +00:00
|
|
|
if not cls._wpnonce:
|
|
|
|
async with session.get(f"{cls.url}/", proxy=proxy) as response:
|
|
|
|
response.raise_for_status()
|
|
|
|
response = await response.text()
|
|
|
|
if result := re.search(r'data-nonce="(.*?)"', response):
|
|
|
|
cls._wpnonce = result.group(1)
|
|
|
|
else:
|
|
|
|
raise RuntimeError("No nonce found")
|
2023-10-05 03:13:37 +00:00
|
|
|
data = {
|
2023-11-13 17:58:52 +00:00
|
|
|
"_wpnonce": cls._wpnonce,
|
|
|
|
"post_id": 58,
|
|
|
|
"url": "https://chatgpt4online.org",
|
|
|
|
"action": "wpaicg_chat_shortcode_message",
|
|
|
|
"message": format_prompt(messages),
|
|
|
|
"bot_id": 3405
|
2023-10-05 03:13:37 +00:00
|
|
|
}
|
2023-11-13 17:58:52 +00:00
|
|
|
async with session.post(f"{cls.url}/rizq", data=data, proxy=proxy) as response:
|
2023-10-05 03:13:37 +00:00
|
|
|
response.raise_for_status()
|
2023-11-13 17:58:52 +00:00
|
|
|
return (await response.json())["data"]
|