ChatGptEs.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from __future__ import annotations
  2. import os
  3. import re
  4. from aiohttp import ClientSession
  5. from ..typing import AsyncResult, Messages
  6. from ..requests.raise_for_status import raise_for_status
  7. from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
  8. from .helper import format_prompt
  9. class ChatGptEs(AsyncGeneratorProvider, ProviderModelMixin):
  10. url = "https://chatgpt.es"
  11. api_endpoint = "https://chatgpt.es/wp-admin/admin-ajax.php"
  12. working = True
  13. supports_stream = True
  14. supports_system_message = False
  15. supports_message_history = False
  16. default_model = 'gpt-4o'
  17. models = ['gpt-4', default_model, 'gpt-4o-mini']
  18. 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:"
  19. @classmethod
  20. async def create_async_generator(
  21. cls,
  22. model: str,
  23. messages: Messages,
  24. proxy: str = None,
  25. **kwargs
  26. ) -> AsyncResult:
  27. model = cls.get_model(model)
  28. headers = {
  29. "authority": "chatgpt.es",
  30. "accept": "application/json",
  31. "origin": cls.url,
  32. "referer": f"{cls.url}/chat",
  33. "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
  34. }
  35. async with ClientSession(headers=headers) as session:
  36. initial_response = await session.get(cls.url)
  37. nonce_ = re.findall(r'data-nonce="(.+?)"', await initial_response.text())[0]
  38. post_id = re.findall(r'data-post-id="(.+?)"', await initial_response.text())[0]
  39. prompt = f"{cls.SYSTEM_PROMPT} {format_prompt(messages)}"
  40. payload = {
  41. 'check_51710191': '1',
  42. '_wpnonce': nonce_,
  43. 'post_id': post_id,
  44. 'url': cls.url,
  45. 'action': 'wpaicg_chat_shortcode_message',
  46. 'message': prompt,
  47. 'bot_id': '0',
  48. 'chatbot_identity': 'shortcode',
  49. 'wpaicg_chat_client_id': os.urandom(5).hex(),
  50. 'wpaicg_chat_history': None
  51. }
  52. async with session.post(cls.api_endpoint, headers=headers, data=payload) as response:
  53. await raise_for_status(response)
  54. result = await response.json()
  55. if "Du musst das Kästchen anklicken!" in result['data']:
  56. raise ValueError(result['data'])
  57. yield result['data']