ChatGptEs.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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-4', 'gpt-4o', 'gpt-4o-mini']
  18. @classmethod
  19. def get_model(cls, model: str) -> str:
  20. if model in cls.models:
  21. return model
  22. elif model in cls.model_aliases:
  23. return cls.model_aliases[model]
  24. else:
  25. return cls.default_model
  26. @classmethod
  27. async def create_async_generator(
  28. cls,
  29. model: str,
  30. messages: Messages,
  31. proxy: str = None,
  32. **kwargs
  33. ) -> AsyncResult:
  34. model = cls.get_model(model)
  35. headers = {
  36. "authority": "chatgpt.es",
  37. "accept": "application/json",
  38. "origin": cls.url,
  39. "referer": f"{cls.url}/chat",
  40. "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",
  41. }
  42. async with ClientSession(headers=headers) as session:
  43. initial_response = await session.get(cls.url)
  44. nonce_ = re.findall(r'data-nonce="(.+?)"', await initial_response.text())[0]
  45. post_id = re.findall(r'data-post-id="(.+?)"', await initial_response.text())[0]
  46. formatted_prompt = format_prompt(messages)
  47. conversation_history = [
  48. "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."
  49. ]
  50. for message in messages[:-1]:
  51. if message['role'] == "user":
  52. conversation_history.append(f"Human: {message['content']}")
  53. else:
  54. conversation_history.append(f"AI: {message['content']}")
  55. payload = {
  56. '_wpnonce': nonce_,
  57. 'post_id': post_id,
  58. 'url': cls.url,
  59. 'action': 'wpaicg_chat_shortcode_message',
  60. 'message': formatted_prompt,
  61. 'bot_id': '0',
  62. 'chatbot_identity': 'shortcode',
  63. 'wpaicg_chat_client_id': os.urandom(5).hex(),
  64. 'wpaicg_chat_history': json.dumps(conversation_history)
  65. }
  66. async with session.post(cls.api_endpoint, headers=headers, data=payload) as response:
  67. response.raise_for_status()
  68. result = await response.json()
  69. yield result['data']