gpt4free/g4f/Provider/Chatgpt4Online.py

46 lines
1.5 KiB
Python
Raw Normal View History

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