ChatGptEs.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from __future__ import annotations
  2. from aiohttp import ClientSession
  3. import os
  4. import json
  5. import re
  6. from ..typing import AsyncResult, Messages
  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 = True
  15. supports_message_history = True
  16. default_model = 'gpt-4o'
  17. models = ['gpt-4o', 'gpt-4o-mini', 'chatgpt-4o-latest']
  18. model_aliases = {
  19. "gpt-4o": "chatgpt-4o-latest",
  20. }
  21. @classmethod
  22. def get_model(cls, model: str) -> str:
  23. if model in cls.models:
  24. return model
  25. elif model in cls.model_aliases:
  26. return cls.model_aliases[model]
  27. else:
  28. return cls.default_model
  29. @classmethod
  30. async def create_async_generator(
  31. cls,
  32. model: str,
  33. messages: Messages,
  34. proxy: str = None,
  35. **kwargs
  36. ) -> AsyncResult:
  37. model = cls.get_model(model)
  38. headers = {
  39. "authority": "chatgpt.es",
  40. "accept": "application/json",
  41. "origin": cls.url,
  42. "referer": f"{cls.url}/chat",
  43. "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",
  44. }
  45. async with ClientSession(headers=headers) as session:
  46. initial_response = await session.get(cls.url)
  47. nonce_ = re.findall(r'data-nonce="(.+?)"', await initial_response.text())[0]
  48. post_id = re.findall(r'data-post-id="(.+?)"', await initial_response.text())[0]
  49. conversation_history = [
  50. "Human: You are a helpful AI assistant. Please respond in the same language that the user uses in their message. Provide accurate, relevant and helpful information while maintaining a friendly and professional tone. If you're not sure about something, please acknowledge that and provide the best information you can while noting any uncertainties. Focus on being helpful while respecting the user's choice of language."
  51. ]
  52. for message in messages[:-1]:
  53. if message['role'] == "user":
  54. conversation_history.append(f"Human: {message['content']}")
  55. else:
  56. conversation_history.append(f"AI: {message['content']}")
  57. payload = {
  58. '_wpnonce': nonce_,
  59. 'post_id': post_id,
  60. 'url': cls.url,
  61. 'action': 'wpaicg_chat_shortcode_message',
  62. 'message': messages[-1]['content'],
  63. 'bot_id': '0',
  64. 'chatbot_identity': 'shortcode',
  65. 'wpaicg_chat_client_id': os.urandom(5).hex(),
  66. 'wpaicg_chat_history': json.dumps(conversation_history)
  67. }
  68. async with session.post(cls.api_endpoint, headers=headers, data=payload) as response:
  69. response.raise_for_status()
  70. result = await response.json()
  71. yield result['data']