from __future__ import annotations import os import re import json try: from curl_cffi.requests import Session has_curl_cffi = True except ImportError: has_curl_cffi = False from ..typing import AsyncResult, Messages from .base_provider import AsyncGeneratorProvider, ProviderModelMixin from .helper import format_prompt from ..errors import MissingRequirementsError class ChatGptEs(AsyncGeneratorProvider, ProviderModelMixin): url = "https://chatgpt.es" api_endpoint = "https://chatgpt.es/wp-admin/admin-ajax.php" working = True supports_stream = True supports_system_message = False supports_message_history = False default_model = 'gpt-4o' models = ['gpt-4', default_model, 'gpt-4o-mini'] SYSTEM_PROMPT = "Your default language is English. Always respond in English unless the user's message is in a different language. If the user's message is not in English, respond in the language of the user's message. Maintain this language behavior throughout the conversation unless explicitly instructed otherwise. User input:" @classmethod async def create_async_generator( cls, model: str, messages: Messages, proxy: str = None, **kwargs ) -> AsyncResult: if not has_curl_cffi: raise MissingRequirementsError('Install or update "curl_cffi" package | pip install -U curl_cffi') model = cls.get_model(model) prompt = f"{cls.SYSTEM_PROMPT} {format_prompt(messages)}" # Use curl_cffi with automatic Cloudflare bypass session = Session() session.headers.update({ "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36", "referer": cls.url, "origin": cls.url, "accept": "*/*", "accept-language": "en-US,en;q=0.9", "content-type": "application/x-www-form-urlencoded; charset=UTF-8", "x-requested-with": "XMLHttpRequest", }) if proxy: session.proxies = {"https": proxy, "http": proxy} # First request to get nonce and post_id initial_response = session.get(cls.url, impersonate="chrome110") initial_text = initial_response.text # More comprehensive nonce extraction nonce_patterns = [ r'