返回提交历史
Modified
etc/testing/test_async.py
+1
-1
Modified
g4f/Provider/ChatgptAi.py
+38
-34
Modified
g4f/Provider/Geekgpt.py
+9
-5
XFEstudio/gpt4free
Fix ChatgptAi Provider
fc151811
代码差异
3 个文件
+48
-40
@@ -6,7 +6,7 @@ sys.path.append(str(Path(__file__).parent.parent))
6
6
sys.path.append(str(Path(__file__).parent.parent.parent))
7
7
8
8
import g4f
9
from testing.test_providers import get_providers
9
from testing._providers import get_providers
10
10
from testing.log_time import log_time_async
11
11
12
12
async def create_async(provider):
@@ -1,36 +1,34 @@
1
1
from __future__ import annotations
2
2
3
import re
3
import re, html, json, string, random
4
4
from aiohttp import ClientSession
5
5
6
from ..typing import Messages
7
from .base_provider import AsyncProvider, format_prompt
6
from ..typing import Messages, AsyncResult
7
from .base_provider import AsyncGeneratorProvider
8
8
9
9
10
class ChatgptAi(AsyncProvider):
11
url: str = "https://chatgpt.ai/"
10
class ChatgptAi(AsyncGeneratorProvider):
11
url: str = "https://chatgpt.ai"
12
12
working = True
13
13
supports_gpt_35_turbo = True
14
_nonce = None
15
_post_id = None
16
_bot_id = None
14
_system = None
17
15
18
16
@classmethod
19
async def create_async(
17
async def create_async_generator(
20
18
cls,
21
19
model: str,
22
20
messages: Messages,
23
21
proxy: str = None,
24
22
**kwargs
25
) -> str:
23
) -> AsyncResult:
26
24
headers = {
27
25
"authority" : "chatgpt.ai",
28
26
"accept" : "*/*",
29
"accept-language" : "en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3",
27
"accept-language" : "en-US",
30
28
"cache-control" : "no-cache",
31
"origin" : "https://chatgpt.ai",
29
"origin" : cls.url,
32
30
"pragma" : "no-cache",
33
"referer" : cls.url,
31
"referer" : f"{cls.url}/",
34
32
"sec-ch-ua" : '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
35
33
"sec-ch-ua-mobile" : "?0",
36
34
"sec-ch-ua-platform" : '"Windows"',
@@ -42,34 +40,40 @@ class ChatgptAi(AsyncProvider):
42
40
async with ClientSession(
43
41
headers=headers
44
42
) as session:
45
if not cls._nonce:
43
if not cls._system:
46
44
async with session.get(cls.url, proxy=proxy) as response:
47
45
response.raise_for_status()
48
46
text = await response.text()
49
result = re.search(r'data-nonce="(.*?)"', text)
47
result = re.search(r"data-system='(.*?)'", text)
50
48
if result:
51
cls._nonce = result.group(1)
52
result = re.search(r'data-post-id="(.*?)"', text)
53
if result:
54
cls._post_id = result.group(1)
55
result = re.search(r'data-bot-id="(.*?)"', text)
56
if result:
57
cls._bot_id = result.group(1)
58
if not cls._nonce or not cls._post_id or not cls._bot_id:
59
raise RuntimeError("Nonce, post-id or bot-id not found")
60
49
cls._system = json.loads(html.unescape(result.group(1)))
50
if not cls._system:
51
raise RuntimeError("System args not found")
52
61
53
data = {
62
"_wpnonce": cls._nonce,
63
"post_id": cls._post_id,
64
"url": "https://chatgpt.ai",
65
"action": "wpaicg_chat_shortcode_message",
66
"message": format_prompt(messages),
67
"bot_id": cls._bot_id
54
"botId": cls._system["botId"],
55
"customId": cls._system["customId"],
56
"session": cls._system["sessionId"],
57
"chatId": "".join(random.choices(f"{string.ascii_lowercase}{string.digits}", k=11)),
58
"contextId": cls._system["contextId"],
59
"messages": messages,
60
"newMessage": messages[-1]["content"],
61
"stream": True
68
62
}
69
63
async with session.post(
70
"https://chatgpt.ai/wp-admin/admin-ajax.php",
64
f"{cls.url}/wp-json/mwai-ui/v1/chats/submit",
71
65
proxy=proxy,
72
data=data
66
json=data
73
67
) as response:
74
68
response.raise_for_status()
75
return (await response.json())["data"]
69
async for line in response.content:
70
if line.startswith(b"data: "):
71
try:
72
line = json.loads(line[6:])
73
assert "type" in line
74
except:
75
raise RuntimeError(f"Broken line: {line.decode()}")
76
if line["type"] == "live":
77
yield line["data"]
78
elif line["type"] == "end":
79
break
@@ -14,11 +14,15 @@ class GeekGpt(BaseProvider):
14
14
supports_gpt_4 = True
15
15
16
16
@classmethod
17
def create_completion(cls,
18
model: str,
19
messages: Messages,
20
stream: bool, **kwargs) -> CreateResult:
21
17
def create_completion(
18
cls,
19
model: str,
20
messages: Messages,
21
stream: bool,
22
**kwargs
23
) -> CreateResult:
24
if not model:
25
model = "gpt-3.5-turbo"
22
26
json_data = {
23
27
'messages': messages,
24
28
'model': model,