gpt4free/g4f/requests/raise_for_status.py
hlohaus 90ef870345 fix: update provider integrations, recipient logic, and auth handling
- In **FreeRouter.py**, change the `working` flag from `False` to `True`.
- In **LMArenaProvider.py**, replace the `.rstrip("▌")` call with a manual check that, if the content ends with `▌`, slices off the trailing characters.
- In **hf_space/__init__.py**, update the async generator call to pass the `media` parameter instead of `images`.
- In **OpenaiChat.py**:
  - Modify the citation replacement regex to use `[0-9]+` (supporting any turn number) instead of a hardcoded `0`.
  - Replace `fields.is_recipient` boolean checks with comparisons against `fields.recipient == "all"` for processing text and metadata.
  - Add a new branch to process `/message/metadata/content_references` for adding source links.
  - Update the conversation initialization by replacing `self.is_recipient` with setting `self.recipient` to `"all"`.
  - Change the auth check from using `cls._api_key` to checking `cls.request_config.access_token`.
- In **chat.v1.js**, adjust the QR code URL assignment to use `window.conversation_id` if available, else default to `/qrcode`.
- In **raise_for_status.py**, update error handling by replacing `ResponseStatusError` with `MissingAuthError` for 403 responses detected as OpenAI Bot.
2025-04-17 03:26:50 +02:00

80 lines
No EOL
3.8 KiB
Python

from __future__ import annotations
from typing import Union
from aiohttp import ClientResponse
from requests import Response as RequestsResponse
from ..errors import ResponseStatusError, RateLimitError, MissingAuthError
from . import Response, StreamResponse
class CloudflareError(ResponseStatusError):
...
def is_cloudflare(text: str) -> bool:
if "Generated by cloudfront" in text or '<p id="cf-spinner-please-wait">' in text:
return True
elif "<title>Attention Required! | Cloudflare</title>" in text or 'id="cf-cloudflare-status"' in text:
return True
return '<div id="cf-please-wait">' in text or "<title>Just a moment...</title>" in text
def is_openai(text: str) -> bool:
return "<p>Unable to load site</p>" in text or 'id="challenge-error-text"' in text
async def raise_for_status_async(response: Union[StreamResponse, ClientResponse], message: str = None):
if response.ok:
return
is_html = False
if message is None:
content_type = response.headers.get("content-type", "")
if content_type.startswith("application/json"):
message = await response.json()
message = message.get("error", message)
if isinstance(message, dict):
message = message.get("message", message)
else:
message = (await response.text()).strip()
is_html = content_type.startswith("text/html") or message.startswith("<!DOCTYPE")
if message is None or is_html:
if response.status == 520:
message = "Unknown error (Cloudflare)"
elif response.status in (429, 402):
message = "Rate limit"
if response.status == 401:
raise MissingAuthError(f"Response {response.status}: {message}")
if response.status == 403 and is_cloudflare(message):
raise CloudflareError(f"Response {response.status}: Cloudflare detected")
elif response.status == 403 and is_openai(message):
raise MissingAuthError(f"Response {response.status}: OpenAI Bot detected")
elif response.status == 502:
raise ResponseStatusError(f"Response {response.status}: Bad Gateway")
elif response.status == 504:
raise RateLimitError(f"Response {response.status}: Gateway Timeout ")
else:
raise ResponseStatusError(f"Response {response.status}: {'HTML content' if is_html else message}")
def raise_for_status(response: Union[Response, StreamResponse, ClientResponse, RequestsResponse], message: str = None):
if hasattr(response, "status"):
return raise_for_status_async(response, message)
if response.ok:
return
is_html = False
if message is None:
is_html = response.headers.get("content-type", "").startswith("text/html") or response.text.startswith("<!DOCTYPE")
message = response.text
if message is None or is_html:
if response.status_code == 520:
message = "Unknown error (Cloudflare)"
elif response.status_code in (429, 402):
raise RateLimitError(f"Response {response.status_code}: Rate Limit")
if response.status_code == 401:
raise MissingAuthError(f"Response {response.status_code}: {message}")
if response.status_code == 403 and is_cloudflare(response.text):
raise CloudflareError(f"Response {response.status_code}: Cloudflare detected")
elif response.status_code == 403 and is_openai(response.text):
raise MissingAuthError(f"Response {response.status_code}: OpenAI Bot detected")
elif response.status_code == 502:
raise ResponseStatusError(f"Response {response.status_code}: Bad Gateway")
elif response.status_code == 504:
raise RateLimitError(f"Response {response.status_code}: Gateway Timeout ")
else:
raise ResponseStatusError(f"Response {response.status_code}: {'HTML content' if is_html else message}")