Chatgpt4o.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from __future__ import annotations
  2. import re
  3. from ...requests import StreamSession, raise_for_status
  4. from ...typing import Messages
  5. from ..base_provider import AsyncProvider, ProviderModelMixin
  6. from ..helper import format_prompt
  7. class Chatgpt4o(AsyncProvider, ProviderModelMixin):
  8. url = "https://chatgpt4o.one"
  9. working = False
  10. _post_id = None
  11. _nonce = None
  12. default_model = 'gpt-4o-mini-2024-07-18'
  13. models = [
  14. 'gpt-4o-mini-2024-07-18',
  15. ]
  16. model_aliases = {
  17. "gpt-4o-mini": "gpt-4o-mini-2024-07-18",
  18. }
  19. @classmethod
  20. async def create_async(
  21. cls,
  22. model: str,
  23. messages: Messages,
  24. proxy: str = None,
  25. timeout: int = 120,
  26. cookies: dict = None,
  27. **kwargs
  28. ) -> str:
  29. headers = {
  30. 'authority': 'chatgpt4o.one',
  31. 'accept': '*/*',
  32. 'accept-language': 'en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3',
  33. 'origin': 'https://chatgpt4o.one',
  34. 'referer': 'https://chatgpt4o.one',
  35. 'sec-ch-ua': '"Chromium";v="118", "Google Chrome";v="118", "Not=A?Brand";v="99"',
  36. 'sec-ch-ua-mobile': '?0',
  37. 'sec-ch-ua-platform': '"macOS"',
  38. 'sec-fetch-dest': 'empty',
  39. 'sec-fetch-mode': 'cors',
  40. 'sec-fetch-site': 'same-origin',
  41. 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
  42. }
  43. async with StreamSession(
  44. headers=headers,
  45. cookies=cookies,
  46. impersonate="chrome",
  47. proxies={"all": proxy},
  48. timeout=timeout
  49. ) as session:
  50. if not cls._post_id or not cls._nonce:
  51. async with session.get(f"{cls.url}/") as response:
  52. await raise_for_status(response)
  53. response_text = await response.text()
  54. post_id_match = re.search(r'data-post-id="([0-9]+)"', response_text)
  55. nonce_match = re.search(r'data-nonce="(.*?)"', response_text)
  56. if not post_id_match:
  57. raise RuntimeError("No post ID found")
  58. cls._post_id = post_id_match.group(1)
  59. if not nonce_match:
  60. raise RuntimeError("No nonce found")
  61. cls._nonce = nonce_match.group(1)
  62. prompt = format_prompt(messages)
  63. data = {
  64. "_wpnonce": cls._nonce,
  65. "post_id": cls._post_id,
  66. "url": cls.url,
  67. "action": "wpaicg_chat_shortcode_message",
  68. "message": prompt,
  69. "bot_id": "0"
  70. }
  71. async with session.post(f"{cls.url}/wp-admin/admin-ajax.php", data=data, cookies=cookies) as response:
  72. await raise_for_status(response)
  73. response_json = await response.json()
  74. if "data" not in response_json:
  75. raise RuntimeError("Unexpected response structure: 'data' field missing")
  76. return response_json["data"]