ChatGptt.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 ChatGptt(AsyncGeneratorProvider, ProviderModelMixin):
  10. url = "https://chatgptt.me"
  11. api_endpoint = "https://chatgptt.me/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', default_model, 'gpt-4o-mini']
  18. @classmethod
  19. async def create_async_generator(
  20. cls,
  21. model: str,
  22. messages: Messages,
  23. proxy: str = None,
  24. **kwargs
  25. ) -> AsyncResult:
  26. model = cls.get_model(model)
  27. headers = {
  28. "authority": "chatgptt.me",
  29. "accept": "application/json",
  30. "origin": cls.url,
  31. "referer": f"{cls.url}/chat",
  32. "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
  33. }
  34. async with ClientSession(headers=headers) as session:
  35. initial_response = await session.get(cls.url)
  36. nonce_ = re.findall(r'data-nonce="(.+?)"', await initial_response.text())[0]
  37. post_id = re.findall(r'data-post-id="(.+?)"', await initial_response.text())[0]
  38. payload = {
  39. '_wpnonce': nonce_,
  40. 'post_id': post_id,
  41. 'url': cls.url,
  42. 'action': 'wpaicg_chat_shortcode_message',
  43. 'message': format_prompt(messages),
  44. 'bot_id': '0',
  45. 'chatbot_identity': 'shortcode',
  46. 'wpaicg_chat_client_id': os.urandom(5).hex(),
  47. 'wpaicg_chat_history': None
  48. }
  49. async with session.post(cls.api_endpoint, headers=headers, data=payload, proxy=proxy) as response:
  50. await raise_for_status(response)
  51. result = await response.json()
  52. yield result['data']