summaryrefslogtreecommitdiffstats
path: root/g4f/Provider
diff options
context:
space:
mode:
authorkqlio67 <kqlio67@users.noreply.github.com>2024-10-02 23:31:38 +0200
committerkqlio67 <kqlio67@users.noreply.github.com>2024-10-02 23:31:38 +0200
commitd9e71dad7bb6b738bc7529c7d76bd9c0abb5e8a9 (patch)
tree0388b99afe92b9f27b268a68ca1c104c35997c2e /g4f/Provider
parentfix(g4f/Provider/Allyfy.py): update API endpoint to full URL (diff)
downloadgpt4free-d9e71dad7bb6b738bc7529c7d76bd9c0abb5e8a9.tar
gpt4free-d9e71dad7bb6b738bc7529c7d76bd9c0abb5e8a9.tar.gz
gpt4free-d9e71dad7bb6b738bc7529c7d76bd9c0abb5e8a9.tar.bz2
gpt4free-d9e71dad7bb6b738bc7529c7d76bd9c0abb5e8a9.tar.lz
gpt4free-d9e71dad7bb6b738bc7529c7d76bd9c0abb5e8a9.tar.xz
gpt4free-d9e71dad7bb6b738bc7529c7d76bd9c0abb5e8a9.tar.zst
gpt4free-d9e71dad7bb6b738bc7529c7d76bd9c0abb5e8a9.zip
Diffstat (limited to 'g4f/Provider')
-rw-r--r--g4f/Provider/ChatGpt.py51
1 files changed, 36 insertions, 15 deletions
diff --git a/g4f/Provider/ChatGpt.py b/g4f/Provider/ChatGpt.py
index fc34fc2b..37ad17d8 100644
--- a/g4f/Provider/ChatGpt.py
+++ b/g4f/Provider/ChatGpt.py
@@ -134,11 +134,22 @@ class ChatGpt(AbstractProvider, ProviderModelMixin):
}
response = session.post('https://chatgpt.com/backend-anon/sentinel/chat-requirements',
- headers=headers, json={'p': pow_req}).json()
+ headers=headers, json={'p': pow_req})
- turnstile = response.get('turnstile', {})
+ # Додана перевірка на статус відповідей, якщо "незвична активність"
+ if response.status_code != 200:
+ print(f"Request failed with status: {response.status_code}")
+ print(f"Response content: {response.content}")
+ return
+
+ response_data = response.json()
+ if "detail" in response_data and "Unusual activity" in response_data["detail"]:
+ print(f"Blocked due to unusual activity: {response_data['detail']}")
+ return
+
+ turnstile = response_data.get('turnstile', {})
turnstile_required = turnstile.get('required')
- pow_conf = response.get('proofofwork', {})
+ pow_conf = response_data.get('proofofwork', {})
if turnstile_required:
turnstile_dx = turnstile.get('dx')
@@ -146,7 +157,7 @@ class ChatGpt(AbstractProvider, ProviderModelMixin):
headers = headers | {
'openai-sentinel-turnstile-token' : turnstile_token,
- 'openai-sentinel-chat-requirements-token': response.get('token'),
+ 'openai-sentinel-chat-requirements-token': response_data.get('token'),
'openai-sentinel-proof-token' : get_answer_token(
pow_conf.get('seed'), pow_conf.get('difficulty'), config
)
@@ -187,20 +198,30 @@ class ChatGpt(AbstractProvider, ProviderModelMixin):
'screen_width': random.randint(1200, 2000),
},
}
+
+ # Додав паузу між запитами для уникнення блокувань через частоту
+ time.sleep(2)
response = session.post('https://chatgpt.com/backend-anon/conversation',
headers=headers, json=json_data, stream=True)
-
+
replace = ''
for line in response.iter_lines():
if line:
- if 'DONE' in line.decode():
- break
-
- data = json.loads(line.decode()[6:])
- if data.get('message').get('author').get('role') == 'assistant':
- tokens = (data.get('message').get('content').get('parts')[0])
-
- yield tokens.replace(replace, '')
-
- replace = tokens
+ decoded_line = line.decode()
+ print(f"Received line: {decoded_line}") # Debugging output
+ if decoded_line.startswith('data:'):
+ json_string = decoded_line[6:] # Extract JSON part after 'data:'
+ if json_string.strip(): # Check if there's actual content
+ try:
+ data = json.loads(json_string)
+ except json.JSONDecodeError as e:
+ print(f"Error decoding JSON: {e}, content: {json_string}")
+ continue
+
+ if data.get('message').get('author').get('role') == 'assistant':
+ tokens = (data.get('message').get('content').get('parts')[0])
+
+ yield tokens.replace(replace, '')
+
+ replace = tokens