Chatgpt4Online.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from __future__ import annotations
  2. import json
  3. from aiohttp import ClientSession
  4. from ...typing import AsyncResult, Messages
  5. from ..base_provider import AsyncGeneratorProvider
  6. from ..helper import format_prompt
  7. class Chatgpt4Online(AsyncGeneratorProvider):
  8. url = "https://chatgpt4online.org"
  9. api_endpoint = "/wp-json/mwai-ui/v1/chats/submit"
  10. working = False
  11. default_model = 'gpt-4'
  12. models = [default_model]
  13. async def get_nonce(headers: dict) -> str:
  14. async with ClientSession(headers=headers) as session:
  15. async with session.post(f"https://chatgpt4online.org/wp-json/mwai/v1/start_session") as response:
  16. return (await response.json())["restNonce"]
  17. @classmethod
  18. async def create_async_generator(
  19. cls,
  20. model: str,
  21. messages: Messages,
  22. proxy: str = None,
  23. **kwargs
  24. ) -> AsyncResult:
  25. headers = {
  26. "accept": "text/event-stream",
  27. "accept-language": "en-US,en;q=0.9",
  28. "content-type": "application/json",
  29. "dnt": "1",
  30. "origin": cls.url,
  31. "priority": "u=1, i",
  32. "referer": f"{cls.url}/",
  33. "sec-ch-ua": '"Not/A)Brand";v="8", "Chromium";v="126"',
  34. "sec-ch-ua-mobile": "?0",
  35. "sec-ch-ua-platform": '"Linux"',
  36. "sec-fetch-dest": "empty",
  37. "sec-fetch-mode": "cors",
  38. "sec-fetch-site": "same-origin",
  39. "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
  40. }
  41. headers['x-wp-nonce'] = await cls.get_nonce(headers)
  42. async with ClientSession(headers=headers) as session:
  43. prompt = format_prompt(messages)
  44. data = {
  45. "botId": "default",
  46. "newMessage": prompt,
  47. "stream": True,
  48. }
  49. async with session.post(f"{cls.url}{cls.api_endpoint}", json=data, proxy=proxy) as response:
  50. response.raise_for_status()
  51. full_response = ""
  52. async for chunk in response.content.iter_any():
  53. if chunk:
  54. try:
  55. # Extract the JSON object from the chunk
  56. for line in chunk.decode().splitlines():
  57. if line.startswith("data: "):
  58. json_data = json.loads(line[6:])
  59. if json_data["type"] == "live":
  60. full_response += json_data["data"]
  61. elif json_data["type"] == "end":
  62. final_data = json.loads(json_data["data"])
  63. full_response = final_data["reply"]
  64. break
  65. except json.JSONDecodeError:
  66. continue
  67. yield full_response