gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
Your task is to review pull requests. Instructions: - Provide the response in following…
- User
Your task is to review pull requests. Instructions:
- Provide the response in following JSON format: {"reviews": [{"line": <line_number>, "body": ""}]}
- Do not give positive comments or compliments.
- Provide comments and suggestions ONLY if there is something to improve, otherwise "reviews" should be an empty array.
- Write the comment in GitHub Markdown format.
- Use the given description only for the overall context and only comment the code.
- IMPORTANT: NEVER suggest adding comments to the code.
Review the following code diff in the file "g4f/Provider/ChatGpt.py" and take the pull request title and description into account when writing the response.
Pull request title: [pull] main from xtekky:main Pull request description:
See Commits and Changes for more details.
Created by pull[bot]
Can you help keep this open source service alive? 💖 Please sponsor : )
Each line is prefixed by its number. Code to review:
3: from ..typing import Messages, CreateResult 4: from ..providers.base_provider import AbstractProvider, ProviderModelMixin 5: -import time, uuid, random, json 6:+import time 7:+import uuid 8:+import random 9:+import json 10: from requests import Session 11: 12: from .openai.new import ( 75: 76: class ChatGpt(AbstractProvider, ProviderModelMixin): 77: label = "ChatGpt" 78:+ url = "https://chatgpt.com" 79: working = True 80: supports_message_history = True 81: supports_system_message = True 82: supports_stream = True 83:+ default_model = 'auto' 84: models = [ 85:+ default_model, 86:+ 'gpt-3.5-turbo', 87: 'gpt-4o', 88: 'gpt-4o-mini', 89: 'gpt-4', 90: 'gpt-4-turbo', 91: 'chatgpt-4o-latest', 92: ] 93:+ 94:+ model_aliases = { 95:+ "gpt-4o": "chatgpt-4o-latest", 96:+ } 97:+ 98:+ @classmethod 99:+ def get_model(cls, model: str) -> str: 100:+ if model in cls.models: 101:+ return model 102:+ elif model in cls.model_aliases: 103:+ return cls.model_aliases[model] 104:+ else: 105:+ return cls.default_model 106: 107: @classmethod 108: def create_completion( 112: stream: bool, 113: **kwargs 114: ) -> CreateResult: 115:+ model = cls.get_model(model) 116:+ if model not in cls.models: 117:+ raise ValueError(f"Model '{model}' is not available. Available models: {', '.join(cls.models)}") 118:+ 119: - if model in [ - 'gpt-4o', - 'gpt-4o-mini', - 'gpt-4', - 'gpt-4-turbo', - 'chatgpt-4o-latest' - ]: - model = 'auto' - - elif model in [ - 'gpt-3.5-turbo' - ]: - model = 'text-davinci-002-render-sha' - - else: - raise ValueError(f"Invalid model: {model}") - - user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36' 120:+ user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36' 121: session: Session = init_session(user_agent) 122: - config = get_config(user_agent) - pow_req = get_requirements_token(config) - headers = { 123:+ config = get_config(user_agent) 124:+ pow_req = get_requirements_token(config) 125:+ headers = { 126: 'accept': '*/*', 127: 'accept-language': 'en-US,en;q=0.8', 128: 'content-type': 'application/json', 141: } 142: 143: response = session.post('https://chatgpt.com/backend-anon/sentinel/chat-requirements', - headers=headers, json={'p': pow_req}) 144:+ headers=headers, json={'p': pow_req}) 145: 146: if response.status_code != 200: - print(f"Request failed with status: {response.status_code}") - print(f"Response content: {response.content}") 147: return 148: 149: response_data = response.json() 150: if "detail" in response_data and "Unusual activity" in response_data["detail"]: - print(f"Blocked due to unusual activity: {response_data['detail']}") 151: return 152: - turnstile = response_data.get('turnstile', {}) 153:+ turnstile = response_data.get('turnstile', {}) 154: turnstile_required = turnstile.get('required') - pow_conf = response_data.get('proofofwork', {}) 155:+ pow_conf = response_data.get('proofofwork', {}) 156: 157: if turnstile_required: - turnstile_dx = turnstile.get('dx') 158:+ turnstile_dx = turnstile.get('dx') 159: turnstile_token = process_turnstile(turnstile_dx, pow_req) 160: - headers = headers | { - 'openai-sentinel-turnstile-token' : turnstile_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 - ) - } - 161:+ headers = {**headers, 162:+ 'openai-sentinel-turnstile-token': turnstile_token, 163:+ 'openai-sentinel-chat-requirements-token': response_data.get('token'), 164:+ 'openai-sentinel-proof-token': get_answer_token( 165:+ pow_conf.get('seed'), pow_conf.get('difficulty'), config 166:+ )} 167:+ 168: json_data = { 169: 'action': 'next', 170: 'messages': format_conversation(messages), 171: 'parent_message_id': str(uuid.uuid4()), - 'model': 'auto', 172:+ 'model': model, 173: 'timezone_offset_min': -120, 174: 'suggestions': [ 175: 'Can you help me create a personalized morning routine that would help increase my productivity throughout the day? Start by asking me about my current habits and what activities energize me in the morning.', 192: 'conversation_origin': None, 193: 'client_contextual_info': { 194: 'is_dark_mode': True, - 'time_since_loaded': random.randint(22,33), 195:+ 'time_since_loaded': random.randint(22, 33), 196: 'page_height': random.randint(600, 900), 197: 'page_width': random.randint(500, 800), 198: 'pixel_ratio': 2, 204: time.sleep(2) 205: 206: response = session.post('https://chatgpt.com/backend-anon/conversation', - headers=headers, json=json_data, stream=True) 207:+ headers=headers, json=json_data, stream=True) 208: 209: replace = '' 210: for line in response.iter_lines(): 211: if line: 212: decoded_line = line.decode() - print(f"Received line: {decoded_line}") 213:+ 214: if decoded_line.startswith('data:'): - json_string = decoded_line[6:] - if json_string.strip(): 215:+ json_string = decoded_line[6:].strip() 216:+ 217:+ if json_string == '[DONE]': 218:+ break 219:+ 220:+ if json_string: 221: try: 222: data = json.loads(json_string) - except json.JSONDecodeError as e: - print(f"Error decoding JSON: {e}, content: {json_string}") 223:+ except json.JSONDecodeError: 224: continue 225: - if data.get('message').get('author').get('role') == 'assistant': - tokens = (data.get('message').get('content').get('parts')[0]) - - yield tokens.replace(replace, '') - - replace = tokens 226:+ if data.get('message') and data['message'].get('author'): 227:+ role = data['message']['author'].get('role') 228:+ if role == 'assistant': 229:+ tokens = data['message']['content'].get('parts', []) 230:+ if tokens: 231:+ yield tokens[0].replace(replace, '') 232:+ replace = tokens[0] - Assistant
{"reviews": [{"line": 5, "body": "- It's better to import only what you need directly instead of importing modules wholesale unless you have a specific reason to do so. Consider importing individual components when feasible."}, {"line": 75, "body": "- The URL provided for ChatGpt (`https://chatgpt.com`) might need to be verified to ensure it is correct and up-to-date."}, {"line": 80, "body": "- Given that `working` is always set to `True`, it's worthwhile to consider whether this attribute is necessary or if there needs to be a mechanism for changing its state."}, {"line": 103, "body": "- If the model assignment is related to model compatibility, assess if the distinction between `default_model` and actual models can be made clearer for maintenance purposes."}, {"line": 165, "body": "- Ensure that the keys used in the dictionary assignment are consistent and meaningful with regard to their intended functionality."}, {"line": 210, "body": "- The line printing received data has been commented out. If logging is beneficial, consider implementing a robust logging mechanism instead of using print statements."}]}