From d9e71dad7bb6b738bc7529c7d76bd9c0abb5e8a9 Mon Sep 17 00:00:00 2001 From: kqlio67 Date: Thu, 3 Oct 2024 00:31:38 +0300 Subject: feat(g4f/Provider/ChatGpt.py): enhance error handling and add debugging --- g4f/Provider/ChatGpt.py | 51 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 15 deletions(-) (limited to 'g4f/Provider/ChatGpt.py') 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 -- cgit v1.2.3