AiChatOnline.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 get_random_string
  7. class AiChatOnline(AsyncGeneratorProvider):
  8. url = "https://aichatonline.org"
  9. working = False
  10. supports_gpt_35_turbo = True
  11. supports_message_history = False
  12. @classmethod
  13. async def create_async_generator(
  14. cls,
  15. model: str,
  16. messages: Messages,
  17. proxy: str = None,
  18. **kwargs
  19. ) -> AsyncResult:
  20. headers = {
  21. "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/119.0",
  22. "Accept": "text/event-stream",
  23. "Accept-Language": "de,en-US;q=0.7,en;q=0.3",
  24. "Accept-Encoding": "gzip, deflate, br",
  25. "Referer": f"{cls.url}/chatgpt/chat/",
  26. "Content-Type": "application/json",
  27. "Origin": cls.url,
  28. "Alt-Used": "aichatonline.org",
  29. "Connection": "keep-alive",
  30. "Sec-Fetch-Dest": "empty",
  31. "Sec-Fetch-Mode": "cors",
  32. "Sec-Fetch-Site": "same-origin",
  33. "TE": "trailers"
  34. }
  35. async with ClientSession(headers=headers) as session:
  36. data = {
  37. "botId": "default",
  38. "customId": None,
  39. "session": get_random_string(16),
  40. "chatId": get_random_string(),
  41. "contextId": 7,
  42. "messages": messages,
  43. "newMessage": messages[-1]["content"],
  44. "newImageId": None,
  45. "stream": True
  46. }
  47. async with session.post(f"{cls.url}/chatgpt/wp-json/mwai-ui/v1/chats/submit", json=data, proxy=proxy) as response:
  48. response.raise_for_status()
  49. async for chunk in response.content:
  50. if chunk.startswith(b"data: "):
  51. data = json.loads(chunk[6:])
  52. if data["type"] == "live":
  53. yield data["data"]
  54. elif data["type"] == "end":
  55. break