返回提交历史
Deleted
g4f/Provider/ChatgptAi.py
+0
-88
XFEstudio/gpt4free
Delete g4f/Provider/ChatgptAi.py
b251c47d
代码差异
1 个文件
+0
-88
@@ -1,88 +0,0 @@
1
from __future__ import annotations
2
3
import re, html, json, string, random
4
from aiohttp import ClientSession
5
6
from ..typing import Messages, AsyncResult
7
from ..errors import RateLimitError
8
from .base_provider import AsyncGeneratorProvider
9
from .helper import get_random_string
10
11
class ChatgptAi(AsyncGeneratorProvider):
12
url = "https://chatgpt.ai"
13
working = True
14
supports_message_history = True
15
supports_system_message = True,
16
supports_gpt_4 = True,
17
_system = None
18
19
@classmethod
20
async def create_async_generator(
21
cls,
22
model: str,
23
messages: Messages,
24
proxy: str = None,
25
**kwargs
26
) -> AsyncResult:
27
headers = {
28
"authority" : "chatgpt.ai",
29
"accept" : "*/*",
30
"accept-language" : "en-US",
31
"cache-control" : "no-cache",
32
"origin" : cls.url,
33
"pragma" : "no-cache",
34
"referer" : f"{cls.url}/",
35
"sec-ch-ua" : '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
36
"sec-ch-ua-mobile" : "?0",
37
"sec-ch-ua-platform" : '"Windows"',
38
"sec-fetch-dest" : "empty",
39
"sec-fetch-mode" : "cors",
40
"sec-fetch-site" : "same-origin",
41
"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",
42
}
43
async with ClientSession(
44
headers=headers
45
) as session:
46
if not cls._system:
47
async with session.get(cls.url, proxy=proxy) as response:
48
response.raise_for_status()
49
text = await response.text()
50
result = re.search(r"data-system='(.*?)'", text)
51
if result :
52
cls._system = json.loads(html.unescape(result.group(1)))
53
if not cls._system:
54
raise RuntimeError("System args not found")
55
56
data = {
57
"botId": cls._system["botId"],
58
"customId": cls._system["customId"],
59
"session": cls._system["sessionId"],
60
"chatId": get_random_string(),
61
"contextId": cls._system["contextId"],
62
"messages": messages[:-1],
63
"newMessage": messages[-1]["content"],
64
"newFileId": None,
65
"stream":True
66
}
67
async with session.post(
68
"https://chatgate.ai/wp-json/mwai-ui/v1/chats/submit",
69
proxy=proxy,
70
json=data,
71
headers={"X-Wp-Nonce": cls._system["restNonce"]}
72
) as response:
73
response.raise_for_status()
74
async for line in response.content:
75
if line.startswith(b"data: "):
76
try:
77
line = json.loads(line[6:])
78
assert "type" in line
79
except:
80
raise RuntimeError(f"Broken line: {line.decode()}")
81
if line["type"] == "error":
82
if "https://chatgate.ai/login" in line["data"]:
83
raise RateLimitError("Rate limit reached")
84
raise RuntimeError(line["data"])
85
if line["type"] == "live":
86
yield line["data"]
87
elif line["type"] == "end":
88
break