2023-10-06 10:50:16 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-10-05 06:13:37 +03:00
|
|
|
import re
|
2023-10-08 12:39:19 +03:00
|
|
|
import json
|
2023-10-06 10:50:16 +03:00
|
|
|
|
2023-10-05 06:13:37 +03:00
|
|
|
from aiohttp import ClientSession
|
2023-10-08 14:59:56 +03:00
|
|
|
from ..typing import AsyncResult, Messages
|
2023-10-08 12:39:19 +03:00
|
|
|
from .base_provider import AsyncGeneratorProvider
|
2023-10-05 06:13:37 +03:00
|
|
|
from .helper import format_prompt
|
2024-03-12 04:06:06 +03:00
|
|
|
from ..errors import RateLimitError
|
2023-10-06 10:50:16 +03:00
|
|
|
|
2023-10-08 12:39:19 +03:00
|
|
|
class ChatgptX(AsyncGeneratorProvider):
|
2023-10-05 06:13:37 +03:00
|
|
|
url = "https://chatgptx.de"
|
|
|
|
supports_gpt_35_turbo = True
|
2024-03-12 04:06:06 +03:00
|
|
|
working = True
|
2023-10-05 06:13:37 +03:00
|
|
|
|
|
|
|
@classmethod
|
2023-10-08 12:39:19 +03:00
|
|
|
async def create_async_generator(
|
2023-10-05 06:13:37 +03:00
|
|
|
cls,
|
|
|
|
model: str,
|
2023-10-08 14:59:56 +03:00
|
|
|
messages: Messages,
|
2023-10-09 11:22:17 +03:00
|
|
|
proxy: str = None,
|
2023-10-05 06:13:37 +03:00
|
|
|
**kwargs
|
2023-10-08 14:59:56 +03:00
|
|
|
) -> AsyncResult:
|
2023-10-05 06:13:37 +03:00
|
|
|
headers = {
|
|
|
|
'accept-language': 'de-DE,de;q=0.9,en-DE;q=0.8,en;q=0.7,en-US',
|
|
|
|
'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',
|
|
|
|
}
|
|
|
|
async with ClientSession(headers=headers) as session:
|
2023-10-09 11:22:17 +03:00
|
|
|
async with session.get(f"{cls.url}/", proxy=proxy) as response:
|
2023-10-05 06:13:37 +03:00
|
|
|
response = await response.text()
|
2023-11-20 21:40:55 +03:00
|
|
|
|
|
|
|
result = re.search(
|
2023-10-23 10:46:25 +03:00
|
|
|
r'<meta name="csrf-token" content="(.*?)"', response
|
2023-11-20 21:40:55 +03:00
|
|
|
)
|
|
|
|
if result:
|
2023-10-05 06:13:37 +03:00
|
|
|
csrf_token = result.group(1)
|
2023-11-20 21:40:55 +03:00
|
|
|
|
|
|
|
result = re.search(r"openconversions\('(.*?)'\)", response)
|
|
|
|
if result:
|
2023-10-05 06:13:37 +03:00
|
|
|
chat_id = result.group(1)
|
2023-11-20 21:40:55 +03:00
|
|
|
|
|
|
|
result = re.search(
|
2023-10-23 10:46:25 +03:00
|
|
|
r'<input type="hidden" id="user_id" value="(.*?)"', response
|
2023-11-20 21:40:55 +03:00
|
|
|
)
|
|
|
|
if result:
|
2023-10-05 06:13:37 +03:00
|
|
|
user_id = result.group(1)
|
|
|
|
|
|
|
|
if not csrf_token or not chat_id or not user_id:
|
|
|
|
raise RuntimeError("Missing csrf_token, chat_id or user_id")
|
|
|
|
|
|
|
|
data = {
|
|
|
|
'_token': csrf_token,
|
|
|
|
'user_id': user_id,
|
|
|
|
'chats_id': chat_id,
|
|
|
|
'prompt': format_prompt(messages),
|
|
|
|
'current_model': "gpt3"
|
|
|
|
}
|
|
|
|
headers = {
|
|
|
|
'authority': 'chatgptx.de',
|
|
|
|
'accept': 'application/json, text/javascript, */*; q=0.01',
|
|
|
|
'origin': cls.url,
|
|
|
|
'referer': f'{cls.url}/',
|
|
|
|
'x-csrf-token': csrf_token,
|
|
|
|
'x-requested-with': 'XMLHttpRequest'
|
|
|
|
}
|
2023-10-23 10:46:25 +03:00
|
|
|
async with session.post(f'{cls.url}/sendchat', data=data, headers=headers, proxy=proxy) as response:
|
2023-10-05 06:13:37 +03:00
|
|
|
response.raise_for_status()
|
2023-10-08 12:39:19 +03:00
|
|
|
chat = await response.json()
|
2024-03-12 04:06:06 +03:00
|
|
|
if "messages" in chat and "Anfragelimit" in chat["messages"]:
|
|
|
|
raise RateLimitError("Rate limit reached")
|
2023-10-08 12:39:19 +03:00
|
|
|
if "response" not in chat or not chat["response"]:
|
2023-10-08 14:59:56 +03:00
|
|
|
raise RuntimeError(f'Response: {chat}')
|
2023-10-08 12:39:19 +03:00
|
|
|
headers = {
|
|
|
|
'authority': 'chatgptx.de',
|
|
|
|
'accept': 'text/event-stream',
|
|
|
|
'referer': f'{cls.url}/',
|
|
|
|
'x-csrf-token': csrf_token,
|
|
|
|
'x-requested-with': 'XMLHttpRequest'
|
|
|
|
}
|
|
|
|
data = {
|
|
|
|
"user_id": user_id,
|
|
|
|
"chats_id": chat_id,
|
|
|
|
"current_model": "gpt3",
|
|
|
|
"conversions_id": chat["conversions_id"],
|
|
|
|
"ass_conversions_id": chat["ass_conversions_id"],
|
|
|
|
}
|
2023-10-09 11:22:17 +03:00
|
|
|
async with session.get(f'{cls.url}/chats_stream', params=data, headers=headers, proxy=proxy) as response:
|
2023-10-08 12:39:19 +03:00
|
|
|
response.raise_for_status()
|
|
|
|
async for line in response.content:
|
|
|
|
if line.startswith(b"data: "):
|
|
|
|
row = line[6:-1]
|
|
|
|
if row == b"[DONE]":
|
|
|
|
break
|
|
|
|
try:
|
|
|
|
content = json.loads(row)["choices"][0]["delta"].get("content")
|
|
|
|
except:
|
|
|
|
raise RuntimeError(f"Broken line: {line.decode()}")
|
|
|
|
if content:
|
2023-10-25 00:30:07 +03:00
|
|
|
yield content
|