2023-09-03 08:26:26 +00:00
|
|
|
from __future__ import annotations
|
2023-07-28 10:07:17 +00:00
|
|
|
|
2023-09-03 08:26:26 +00:00
|
|
|
import re
|
2023-10-07 07:02:48 +00:00
|
|
|
from aiohttp import ClientSession
|
2023-09-03 08:26:26 +00:00
|
|
|
|
2023-10-09 08:22:17 +00:00
|
|
|
from ..typing import Messages
|
2023-10-05 03:13:37 +00:00
|
|
|
from .base_provider import AsyncProvider, format_prompt
|
2023-09-03 08:26:26 +00:00
|
|
|
|
2023-07-28 10:07:17 +00:00
|
|
|
|
2023-10-05 03:13:37 +00:00
|
|
|
class ChatgptAi(AsyncProvider):
|
2023-09-05 15:27:24 +00:00
|
|
|
url: str = "https://chatgpt.ai/"
|
|
|
|
working = True
|
|
|
|
supports_gpt_35_turbo = True
|
2023-10-05 03:13:37 +00:00
|
|
|
_nonce = None
|
|
|
|
_post_id = None
|
|
|
|
_bot_id = None
|
2023-07-28 10:07:17 +00:00
|
|
|
|
2023-09-05 15:27:24 +00:00
|
|
|
@classmethod
|
2023-10-05 03:13:37 +00:00
|
|
|
async def create_async(
|
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-05 03:13:37 +00:00
|
|
|
) -> str:
|
2023-07-28 10:07:17 +00:00
|
|
|
headers = {
|
2023-08-27 15:37:44 +00:00
|
|
|
"authority" : "chatgpt.ai",
|
|
|
|
"accept" : "*/*",
|
|
|
|
"accept-language" : "en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3",
|
|
|
|
"cache-control" : "no-cache",
|
|
|
|
"origin" : "https://chatgpt.ai",
|
|
|
|
"pragma" : "no-cache",
|
2023-09-05 15:27:24 +00:00
|
|
|
"referer" : 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-07 07:02:48 +00:00
|
|
|
headers=headers
|
2023-09-05 15:27:24 +00:00
|
|
|
) as session:
|
2023-10-05 03:13:37 +00:00
|
|
|
if not cls._nonce:
|
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()
|
|
|
|
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")
|
2023-07-28 10:07:17 +00:00
|
|
|
|
2023-09-05 15:27:24 +00:00
|
|
|
data = {
|
2023-10-05 03:13:37 +00:00
|
|
|
"_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
|
2023-09-05 15:27:24 +00:00
|
|
|
}
|
|
|
|
async with session.post(
|
2023-10-05 03:13:37 +00:00
|
|
|
"https://chatgpt.ai/wp-admin/admin-ajax.php",
|
2023-09-05 15:27:24 +00:00
|
|
|
proxy=proxy,
|
2023-10-05 03:13:37 +00:00
|
|
|
data=data
|
2023-09-05 15:27:24 +00:00
|
|
|
) as response:
|
|
|
|
response.raise_for_status()
|
2023-10-05 03:13:37 +00:00
|
|
|
return (await response.json())["data"]
|