返回提交历史
Modified
gpt4free/forefront/__init__.py
+45
-26
Modified
gpt4free/forefront/typing.py
+6
-0
Modified
requirements.txt
+3
-3
XFEstudio/gpt4free
Support for new authentication methods
21715f44
代码差异
3 个文件
+54
-29
@@ -1,20 +1,24 @@
1
import hashlib
2
from base64 import b64encode
1
3
from json import loads
2
4
from re import findall
3
5
from time import time, sleep
4
6
from typing import Generator, Optional
5
7
from uuid import uuid4
6
8
7
from mailgw_temporary_email import Email
9
from Crypto.Cipher import AES
10
from Crypto.Random import get_random_bytes
8
11
from fake_useragent import UserAgent
12
from mailgw_temporary_email import Email
9
13
from requests import post
10
14
from tls_client import Session
11
15
12
from .typing import ForeFrontResponse
16
from .typing import ForeFrontResponse, AccountData
13
17
14
18
15
19
class Account:
16
20
@staticmethod
17
def create(proxy: Optional[str] = None, logging: bool = False):
21
def create(proxy: Optional[str] = None, logging: bool = False) -> AccountData:
18
22
proxies = {'http': 'http://' + proxy, 'https': 'http://' + proxy} if proxy else False
19
23
20
24
start = time()
@@ -34,14 +38,13 @@ class Account:
34
38
'https://clerk.forefront.ai/v1/client/sign_ups?_clerk_js_version=4.38.4',
35
39
data={'email_address': mail_address},
36
40
)
37
print(response.json()['response']['id'])
38
41
39
42
try:
40
43
trace_token = response.json()['response']['id']
41
44
if logging:
42
45
print(trace_token)
43
46
except KeyError:
44
return 'Failed to create account!'
47
raise RuntimeError('Failed to create account!')
45
48
46
49
response = client.post(
47
50
f'https://clerk.forefront.ai/v1/client/sign_ups/{trace_token}/prepare_verification?_clerk_js_version=4.38.4',
@@ -55,27 +58,26 @@ class Account:
55
58
print(response.text)
56
59
57
60
if 'sign_up_attempt' not in response.text:
58
return 'Failed to create account!'
61
raise RuntimeError('Failed to create account!')
59
62
60
63
while True:
61
64
sleep(5)
62
65
message_id = mail_client.message_list()[0]['id']
63
66
message = mail_client.message(message_id)
64
65
new_message: Message = message
66
verification_url = findall(r'https:\/\/clerk\.forefront\.ai\/v1\/verify\?token=\w.+', new_message["text"])[0]
67
67
verification_url = findall(r'https:\/\/clerk\.forefront\.ai\/v1\/verify\?token=\w.+', message["text"])[0]
68
68
if verification_url:
69
69
break
70
70
71
71
if logging:
72
72
print(verification_url)
73
client.get(verification_url)
73
74
74
response = client.get(verification_url)
75
76
response = client.get('https://clerk.forefront.ai/v1/client?_clerk_js_version=4.38.4')
75
response = client.get('https://clerk.forefront.ai/v1/client?_clerk_js_version=4.38.4').json()
76
session_data = response['response']['sessions'][0]
77
77
78
token = response.json()['response']['sessions'][0]['last_active_token']['jwt']
78
user_id = session_data['user']['id']
79
session_id = session_data['id']
80
token = session_data['last_active_token']['jwt']
79
81
80
82
with open('accounts.txt', 'a') as f:
81
83
f.write(f'{mail_address}:{token}\n')
@@ -83,32 +85,32 @@ class Account:
83
85
if logging:
84
86
print(time() - start)
85
87
86
return token
88
return AccountData(token=token, user_id=user_id, session_id=session_id)
87
89
88
90
89
91
class StreamingCompletion:
90
92
@staticmethod
91
93
def create(
92
token=None,
94
prompt: str,
95
account_data: AccountData,
93
96
chat_id=None,
94
prompt='',
95
97
action_type='new',
96
98
default_persona='607e41fe-95be-497e-8e97-010a59b2e2c0', # default
97
99
model='gpt-4',
98
100
proxy=None
99
101
) -> Generator[ForeFrontResponse, None, None]:
100
if not token:
101
raise Exception('Token is required!')
102
token = account_data.token
102
103
if not chat_id:
103
104
chat_id = str(uuid4())
104
105
105
proxies = { 'http': 'http://' + proxy, 'https': 'http://' + proxy } if proxy else None
106
proxies = {'http': 'http://' + proxy, 'https': 'http://' + proxy} if proxy else None
107
base64_data = b64encode((account_data.user_id + default_persona + chat_id).encode()).decode()
108
encrypted_signature = StreamingCompletion.__encrypt(base64_data, account_data.session_id)
106
109
107
110
headers = {
108
111
'authority': 'chat-server.tenant-forefront-default.knative.chi.coreweave.com',
109
112
'accept': '*/*',
110
113
'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',
111
'authorization': 'Bearer ' + token,
112
114
'cache-control': 'no-cache',
113
115
'content-type': 'application/json',
114
116
'origin': 'https://chat.forefront.ai',
@@ -120,6 +122,8 @@ class StreamingCompletion:
120
122
'sec-fetch-dest': 'empty',
121
123
'sec-fetch-mode': 'cors',
122
124
'sec-fetch-site': 'cross-site',
125
'authorization': f"Bearer {token}",
126
'X-Signature': encrypted_signature,
123
127
'user-agent': UserAgent().random,
124
128
}
125
129
@@ -133,7 +137,7 @@ class StreamingCompletion:
133
137
}
134
138
135
139
for chunk in post(
136
'https://chat-server.tenant-forefront-default.knative.chi.coreweave.com/chat',
140
'https://streaming.tenant-forefront-default.knative.chi.coreweave.com/chat',
137
141
headers=headers,
138
142
proxies=proxies,
139
143
json=json_data,
@@ -160,13 +164,28 @@ class StreamingCompletion:
160
164
}
161
165
)
162
166
167
@staticmethod
168
def __encrypt(data: str, key: str) -> str:
169
hash_key = hashlib.sha256(key.encode()).digest()
170
iv = get_random_bytes(16)
171
cipher = AES.new(hash_key, AES.MODE_CBC, iv)
172
encrypted_data = cipher.encrypt(StreamingCompletion.__pad_data(data.encode()))
173
return iv.hex() + encrypted_data.hex()
174
175
@staticmethod
176
def __pad_data(data: bytes) -> bytes:
177
block_size = AES.block_size
178
padding_size = block_size - len(data) % block_size
179
padding = bytes([padding_size] * padding_size)
180
return data + padding
181
163
182
164
183
class Completion:
165
184
@staticmethod
166
185
def create(
167
token=None,
186
prompt: str,
187
account_data: AccountData,
168
188
chat_id=None,
169
prompt='',
170
189
action_type='new',
171
190
default_persona='607e41fe-95be-497e-8e97-010a59b2e2c0', # default
172
191
model='gpt-4',
@@ -175,7 +194,7 @@ class Completion:
175
194
text = ''
176
195
final_response = None
177
196
for response in StreamingCompletion.create(
178
token=token,
197
account_data=account_data,
179
198
chat_id=chat_id,
180
199
prompt=prompt,
181
200
action_type=action_type,
@@ -190,6 +209,6 @@ class Completion:
190
209
if final_response:
191
210
final_response.text = text
192
211
else:
193
raise Exception('Unable to get the response, Please try again')
212
raise RuntimeError('Unable to get the response, Please try again')
194
213
195
214
return final_response
@@ -24,3 +24,9 @@ class ForeFrontResponse(BaseModel):
24
24
choices: List[Choice]
25
25
usage: Usage
26
26
text: str
27
28
29
class AccountData(BaseModel):
30
token: str
31
user_id: str
32
session_id: str
@@ -13,6 +13,6 @@ https://github.com/AI-Yash/st-chat/archive/refs/pull/24/head.zip
13
13
pydantic
14
14
pymailtm
15
15
Levenshtein
16
xtempmail
17
faker
18
retrying
16
retrying
17
mailgw_temporary_email
18
pycryptodome