Chatgpt4Online.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from __future__ import annotations
  2. import re
  3. from aiohttp import ClientSession
  4. from ..typing import Messages
  5. from .base_provider import AsyncProvider
  6. from .helper import format_prompt
  7. class Chatgpt4Online(AsyncProvider):
  8. url = "https://chatgpt4online.org"
  9. supports_message_history = True
  10. supports_gpt_35_turbo = True
  11. working = True
  12. _wpnonce = None
  13. @classmethod
  14. async def create_async(
  15. cls,
  16. model: str,
  17. messages: Messages,
  18. proxy: str = None,
  19. **kwargs
  20. ) -> str:
  21. async with ClientSession() as session:
  22. if not cls._wpnonce:
  23. async with session.get(f"{cls.url}/", proxy=proxy) as response:
  24. response.raise_for_status()
  25. response = await response.text()
  26. result = re.search(r'data-nonce="(.*?)"', response)
  27. if result:
  28. cls._wpnonce = result.group(1)
  29. else:
  30. raise RuntimeError("No nonce found")
  31. data = {
  32. "_wpnonce": cls._wpnonce,
  33. "post_id": 58,
  34. "url": "https://chatgpt4online.org",
  35. "action": "wpaicg_chat_shortcode_message",
  36. "message": format_prompt(messages),
  37. "bot_id": 3405
  38. }
  39. async with session.post(f"{cls.url}/rizq", data=data, proxy=proxy) as response:
  40. response.raise_for_status()
  41. return (await response.json())["data"]