gpt-4o-2024-08-06· 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/Cloudflare.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: import asyncio 4: import json 5: import uuid -import cloudscraper -from typing import AsyncGenerator -from ..typing import AsyncResult, Messages -from .base_provider import AsyncGeneratorProvider, ProviderModelMixin -from .helper import format_prompt 6:+ 7:+from ..typing import AsyncResult, Messages, Cookies 8:+from .base_provider import AsyncGeneratorProvider, ProviderModelMixin, get_running_loop 9:+from ..requests import Session, StreamSession, get_args_from_nodriver, raise_for_status, merge_cookies 10: 11: class Cloudflare(AsyncGeneratorProvider, ProviderModelMixin): 12:+ label = "Cloudflare AI" 13: url = "https://playground.ai.cloudflare.com" 14: api_endpoint = "https://playground.ai.cloudflare.com/api/inference" 15:+ models_url = "https://playground.ai.cloudflare.com/api/models" 16: working = True 17: supports_stream = True 18: supports_system_message = True 19: supports_message_history = True - - default_model = '@cf/meta/llama-3.1-8b-instruct' - models = [ - '@cf/deepseek-ai/deepseek-math-7b-instruct', # Specific answer - - - '@cf/thebloke/discolm-german-7b-v1-awq', - - - '@cf/tiiuae/falcon-7b-instruct', # Specific answer - - - '@hf/google/gemma-7b-it', - - - '@cf/meta/llama-2-7b-chat-fp16', - '@cf/meta/llama-2-7b-chat-int8', - - '@cf/meta/llama-3-8b-instruct', - '@cf/meta/llama-3-8b-instruct-awq', - default_model, - '@hf/meta-llama/meta-llama-3-8b-instruct', - - '@cf/meta/llama-3.1-8b-instruct-awq', - '@cf/meta/llama-3.1-8b-instruct-fp8', - '@cf/meta/llama-3.2-11b-vision-instruct', - '@cf/meta/llama-3.2-1b-instruct', - '@cf/meta/llama-3.2-3b-instruct', - - '@cf/mistral/mistral-7b-instruct-v0.1', - '@hf/mistral/mistral-7b-instruct-v0.2', - - '@cf/openchat/openchat-3.5-0106', - - '@cf/microsoft/phi-2', - - '@cf/qwen/qwen1.5-0.5b-chat', - '@cf/qwen/qwen1.5-1.8b-chat', - '@cf/qwen/qwen1.5-14b-chat-awq', - '@cf/qwen/qwen1.5-7b-chat-awq', - - '@cf/defog/sqlcoder-7b-2', # Specific answer - - '@cf/tinyllama/tinyllama-1.1b-chat-v1.0', - - '@cf/fblgit/una-cybertron-7b-v2-bf16', - ] - 20:+ default_model = "@cf/meta/llama-3.1-8b-instruct" 21: model_aliases = { - "german-7b-v1": "@cf/thebloke/discolm-german-7b-v1-awq", - - - "gemma-7b": "@hf/google/gemma-7b-it", - - 22: "llama-2-7b": "@cf/meta/llama-2-7b-chat-fp16", 23: "llama-2-7b": "@cf/meta/llama-2-7b-chat-int8", - 24: "llama-3-8b": "@cf/meta/llama-3-8b-instruct", 25: "llama-3-8b": "@cf/meta/llama-3-8b-instruct-awq", - "llama-3-8b": "@cf/meta/llama-3.1-8b-instruct", 26: "llama-3-8b": "@hf/meta-llama/meta-llama-3-8b-instruct", - 27: "llama-3.1-8b": "@cf/meta/llama-3.1-8b-instruct-awq", 28: "llama-3.1-8b": "@cf/meta/llama-3.1-8b-instruct-fp8", - "llama-3.1-8b": "@cf/meta/llama-3.1-8b-instruct-fp8", - - "llama-3.2-11b": "@cf/meta/llama-3.2-11b-vision-instruct", 29: "llama-3.2-1b": "@cf/meta/llama-3.2-1b-instruct", - "llama-3.2-3b": "@cf/meta/llama-3.2-3b-instruct", - - - "mistral-7b": "@cf/mistral/mistral-7b-instruct-v0.1", - "mistral-7b": "@hf/mistral/mistral-7b-instruct-v0.2", - - - "openchat-3.5": "@cf/openchat/openchat-3.5-0106", - - - "phi-2": "@cf/microsoft/phi-2", - - - "qwen-1.5-0.5b": "@cf/qwen/qwen1.5-0.5b-chat", - "qwen-1.5-1.8b": "@cf/qwen/qwen1.5-1.8b-chat", - "qwen-1.5-14b": "@cf/qwen/qwen1.5-14b-chat-awq", 30: "qwen-1.5-7b": "@cf/qwen/qwen1.5-7b-chat-awq", - - - "tinyllama-1.1b": "@cf/tinyllama/tinyllama-1.1b-chat-v1.0", - - - "cybertron-7b": "@cf/fblgit/una-cybertron-7b-v2-bf16", 31: } 32:+ _args: dict = None 33: 34: @classmethod - def get_model(cls, model: str) -> str: - if model in cls.models: - return model - elif model in cls.model_aliases: - return cls.model_aliases[model] - else: - return cls.default_model 35:+ def get_models(cls) -> str: 36:+ if not cls.models: 37:+ if cls._args is None: 38:+ get_running_loop(check_nested=True) 39:+ args = get_args_from_nodriver(cls.url, cookies={ 40:+ '__cf_bm': uuid.uuid4().hex, 41:+ }) 42:+ cls._args = asyncio.run(args) 43:+ with Session(**cls._args) as session: 44:+ response = session.get(cls.models_url) 45:+ raise_for_status(response) 46:+ json_data = response.json() 47:+ cls.models = [model.get("name") for model in json_data.get("models")] 48:+ cls._args["cookies"] = merge_cookies(cls._args["cookies"] , response) 49:+ return cls.models 50: 51: @classmethod 52: async def create_async_generator( 54: model: str, 55: messages: Messages, 56: proxy: str = None, - max_tokens: str = 2048, - stream: bool = True, 57:+ max_tokens: int = 2048, 58:+ cookies: Cookies = None, 59:+ timeout: int = 300, 60: **kwargs 61: ) -> AsyncResult: 62: model = cls.get_model(model) - - headers = { - 'Accept': 'text/event-stream', - 'Accept-Language': 'en-US,en;q=0.9', - 'Cache-Control': 'no-cache', - 'Content-Type': 'application/json', - 'Origin': cls.url, - 'Pragma': 'no-cache', - 'Referer': f'{cls.url}/', - 'Sec-Ch-Ua': '"Chromium";v="129", "Not=A?Brand";v="8"', - 'Sec-Ch-Ua-Mobile': '?0', - 'Sec-Ch-Ua-Platform': '"Linux"', - 'Sec-Fetch-Dest': 'empty', - 'Sec-Fetch-Mode': 'cors', - 'Sec-Fetch-Site': 'same-origin', - 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36', - } - - cookies = { - '__cf_bm': uuid.uuid4().hex, - } - - scraper = cloudscraper.create_scraper() - - prompt = format_prompt(messages) 63:+ if cls._args is None: 64:+ cls._args = await get_args_from_nodriver(cls.url, proxy, timeout, cookies) 65: data = { - "messages": [ - {"role": "system", "content": "You are a helpful assistant"}, - {"role": "user", "content": prompt} - ], 66:+ "messages": messages, 67: "lora": None, 68: "model": model, 69: "max_tokens": max_tokens, - "stream": stream 70:+ "stream": True 71: } - - max_retries = 3 - for attempt in range(max_retries): - try: - response = scraper.post( - cls.api_endpoint, - headers=headers, - cookies=cookies, - json=data, - stream=True, - proxies={'http': proxy, 'https': proxy} if proxy else None - ) - - if response.status_code == 403: - await asyncio.sleep(2 ** attempt) - continue - - response.raise_for_status() - - for line in response.iter_lines(): 72:+ async with StreamSession(**cls._args) as session: 73:+ async with session.post( 74:+ cls.api_endpoint, 75:+ json=data, 76:+ ) as response: 77:+ await raise_for_status(response) 78:+ cls._args["cookies"] = merge_cookies(cls._args["cookies"] , response) 79:+ async for line in response.iter_lines(): 80: if line.startswith(b'data: '): 81: if line == b'data: [DONE]': 82: break 83: try: - content = json.loads(line[6:].decode('utf-8'))['response'] - yield content 84:+ content = json.loads(line[6:].decode()) 85:+ if content.get("response") and content.get("response") != '</s>': 86:+ yield content['response'] 87: except Exception: - continue - break - except Exception as e: - if attempt == max_retries - 1: - raise - - @classmethod - async def create_async( - cls, - model: str, - messages: Messages, - proxy: str = None, - **kwargs - ) -> str: - full_response = "" - async for response in cls.create_async_generator(model, messages, proxy, **kwargs): - full_response += response - return full_response 88:+ continue - Assistant
{ "reviews": [ { "line": 23, "body": "Having two entries with the same key `llama-2-7b` in `model_aliases` is redundant and could lead to unintended behavior. Consider removing one of the duplicate entries." }, { "line": 25, "body": "Having two entries with the same key `llama-3-8b` in `model_aliases` is redundant and could lead to unintended behavior. Consider removing one of the duplicate entries." }, { "line": 28, "body": "Having two entries with the same key `llama-3.1-8b` in `model_aliases` is redundant and could lead to unintended behavior. Consider removing one of the duplicate entries." }, { "line": 29, "body": "It's unusual to re-assign keys like `mistral-7b` to different values in `model_aliases`. Make sure this is the intended behavior, or consider using different keys to avoid confusion." } ] }