2023-10-05 06:13:37 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-03-12 04:06:06 +03:00
|
|
|
import time, json, re, asyncio
|
2023-10-07 10:02:48 +03:00
|
|
|
from aiohttp import ClientSession
|
2023-10-05 06:13:37 +03:00
|
|
|
|
2024-03-12 04:06:06 +03:00
|
|
|
from ...typing import AsyncResult, Messages
|
|
|
|
from ...errors import RateLimitError
|
|
|
|
from ..base_provider import AsyncGeneratorProvider
|
|
|
|
from ..helper import format_prompt
|
2023-10-05 06:13:37 +03:00
|
|
|
|
|
|
|
class ChatgptDemo(AsyncGeneratorProvider):
|
2024-03-12 04:06:06 +03:00
|
|
|
url = "https://chatgptdemo.info/chat"
|
2024-02-27 13:55:40 +03:00
|
|
|
working = False
|
2024-03-12 04:06:06 +03:00
|
|
|
supports_gpt_35_turbo = True
|
2023-10-05 06:13:37 +03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
async def create_async_generator(
|
|
|
|
cls,
|
|
|
|
model: str,
|
2023-10-09 11:22:17 +03:00
|
|
|
messages: Messages,
|
2023-10-05 06:13:37 +03:00
|
|
|
proxy: str = None,
|
|
|
|
**kwargs
|
2023-10-09 11:22:17 +03:00
|
|
|
) -> AsyncResult:
|
2023-10-05 06:13:37 +03:00
|
|
|
headers = {
|
2024-03-12 04:06:06 +03:00
|
|
|
"authority": "chatgptdemo.info",
|
|
|
|
"accept-language": "en-US",
|
|
|
|
"origin": "https://chatgptdemo.info",
|
|
|
|
"referer": "https://chatgptdemo.info/chat/",
|
2023-10-05 06:13:37 +03:00
|
|
|
"sec-ch-ua": '"Google Chrome";v="117", "Not;A=Brand";v="8", "Chromium";v="117"',
|
|
|
|
"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/117.0.0.0 Safari/537.36"
|
|
|
|
}
|
2023-10-07 10:02:48 +03:00
|
|
|
async with ClientSession(headers=headers) as session:
|
2023-10-05 06:13:37 +03:00
|
|
|
async with session.get(f"{cls.url}/", proxy=proxy) as response:
|
|
|
|
response.raise_for_status()
|
2024-03-12 04:06:06 +03:00
|
|
|
text = await response.text()
|
|
|
|
result = re.search(
|
|
|
|
r'<div id="USERID" style="display: none">(.*?)<\/div>',
|
|
|
|
text,
|
|
|
|
)
|
|
|
|
if result:
|
|
|
|
user_id = result.group(1)
|
|
|
|
else:
|
|
|
|
raise RuntimeError("No user id found")
|
|
|
|
async with session.post(f"https://chatgptdemo.info/chat/new_chat", json={"user_id": user_id}, proxy=proxy) as response:
|
2023-10-05 06:13:37 +03:00
|
|
|
response.raise_for_status()
|
|
|
|
chat_id = (await response.json())["id_"]
|
|
|
|
if not chat_id:
|
|
|
|
raise RuntimeError("Could not create new chat")
|
2024-03-12 04:06:06 +03:00
|
|
|
await asyncio.sleep(10)
|
2023-10-05 06:13:37 +03:00
|
|
|
data = {
|
|
|
|
"question": format_prompt(messages),
|
|
|
|
"chat_id": chat_id,
|
2024-03-12 04:06:06 +03:00
|
|
|
"timestamp": int((time.time())*1e3),
|
2023-10-05 06:13:37 +03:00
|
|
|
}
|
2024-03-12 04:06:06 +03:00
|
|
|
async with session.post(f"https://chatgptdemo.info/chat/chat_api_stream", json=data, proxy=proxy) as response:
|
|
|
|
if response.status == 429:
|
|
|
|
raise RateLimitError("Rate limit reached")
|
2023-10-05 06:13:37 +03:00
|
|
|
response.raise_for_status()
|
|
|
|
async for line in response.content:
|
|
|
|
if line.startswith(b"data: "):
|
|
|
|
line = json.loads(line[6:-1])
|
2023-11-20 21:40:55 +03:00
|
|
|
|
|
|
|
chunk = line["choices"][0]["delta"].get("content")
|
|
|
|
if chunk:
|
2023-10-05 06:13:37 +03:00
|
|
|
yield chunk
|