2023-10-05 06:13:37 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-01-01 01:35:11 +03:00
|
|
|
import json
|
2023-10-05 06:13:37 +03:00
|
|
|
from aiohttp import ClientSession
|
|
|
|
|
2024-07-25 09:21:55 +03:00
|
|
|
from ..typing import AsyncResult, Messages
|
2024-01-01 01:35:11 +03:00
|
|
|
from .base_provider import AsyncGeneratorProvider
|
2024-07-25 09:21:55 +03:00
|
|
|
from .helper import format_prompt
|
|
|
|
|
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"
|
2024-07-25 09:21:55 +03:00
|
|
|
api_endpoint = "/wp-json/mwai-ui/v1/chats/submit"
|
|
|
|
working = True
|
2024-08-13 16:26:17 +03:00
|
|
|
|
2024-10-20 20:33:14 +03:00
|
|
|
default_model = 'gpt-4'
|
|
|
|
models = [default_model]
|
|
|
|
|
2024-08-29 09:03:32 +03:00
|
|
|
async def get_nonce(headers: dict) -> str:
|
|
|
|
async with ClientSession(headers=headers) as session:
|
2024-08-13 16:26:17 +03:00
|
|
|
async with session.post(f"https://chatgpt4online.org/wp-json/mwai/v1/start_session") as response:
|
|
|
|
return (await response.json())["restNonce"]
|
2024-10-20 20:33:14 +03:00
|
|
|
|
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:
|
2024-07-25 09:21:55 +03:00
|
|
|
headers = {
|
|
|
|
"accept": "text/event-stream",
|
|
|
|
"accept-language": "en-US,en;q=0.9",
|
|
|
|
"content-type": "application/json",
|
|
|
|
"dnt": "1",
|
|
|
|
"origin": cls.url,
|
|
|
|
"priority": "u=1, i",
|
|
|
|
"referer": f"{cls.url}/",
|
|
|
|
"sec-ch-ua": '"Not/A)Brand";v="8", "Chromium";v="126"',
|
|
|
|
"sec-ch-ua-mobile": "?0",
|
|
|
|
"sec-ch-ua-platform": '"Linux"',
|
|
|
|
"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/126.0.0.0 Safari/537.36",
|
|
|
|
}
|
2024-08-29 09:03:32 +03:00
|
|
|
headers['x-wp-nonce'] = await cls.get_nonce(headers)
|
2024-07-25 09:21:55 +03:00
|
|
|
async with ClientSession(headers=headers) as session:
|
|
|
|
prompt = format_prompt(messages)
|
2023-10-05 06:13:37 +03:00
|
|
|
data = {
|
2024-07-25 09:21:55 +03:00
|
|
|
"botId": "default",
|
|
|
|
"newMessage": prompt,
|
|
|
|
"stream": True,
|
2023-10-05 06:13:37 +03:00
|
|
|
}
|
2024-07-25 09:21:55 +03:00
|
|
|
|
|
|
|
async with session.post(f"{cls.url}{cls.api_endpoint}", json=data, proxy=proxy) as response:
|
2023-10-05 06:13:37 +03:00
|
|
|
response.raise_for_status()
|
2024-07-25 09:21:55 +03:00
|
|
|
full_response = ""
|
|
|
|
|
|
|
|
async for chunk in response.content.iter_any():
|
|
|
|
if chunk:
|
|
|
|
try:
|
|
|
|
# Extract the JSON object from the chunk
|
|
|
|
for line in chunk.decode().splitlines():
|
|
|
|
if line.startswith("data: "):
|
|
|
|
json_data = json.loads(line[6:])
|
|
|
|
if json_data["type"] == "live":
|
|
|
|
full_response += json_data["data"]
|
|
|
|
elif json_data["type"] == "end":
|
|
|
|
final_data = json.loads(json_data["data"])
|
|
|
|
full_response = final_data["reply"]
|
|
|
|
break
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
continue
|
|
|
|
|
|
|
|
yield full_response
|
2024-08-13 16:26:17 +03:00
|
|
|
|