ChatgptDemoAi.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 ChatgptDemoAi(AsyncGeneratorProvider):
  8. url = "https://chat.chatgptdemo.ai"
  9. working = False
  10. supports_gpt_35_turbo = True
  11. supports_message_history = True
  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": "*/*",
  23. "Accept-Language": "de,en-US;q=0.7,en;q=0.3",
  24. "Accept-Encoding": "gzip, deflate, br",
  25. "Referer": f"{cls.url}/",
  26. "Content-Type": "application/json",
  27. "Origin": cls.url,
  28. "Connection": "keep-alive",
  29. "Sec-Fetch-Dest": "empty",
  30. "Sec-Fetch-Mode": "cors",
  31. "Sec-Fetch-Site": "same-origin",
  32. "TE": "trailers"
  33. }
  34. async with ClientSession(headers=headers) as session:
  35. data = {
  36. "botId": "default",
  37. "customId": "8824fe9bdb323a5d585a3223aaa0cb6e",
  38. "session": "N/A",
  39. "chatId": get_random_string(12),
  40. "contextId": 2,
  41. "messages": messages,
  42. "newMessage": messages[-1]["content"],
  43. "stream": True
  44. }
  45. async with session.post(f"{cls.url}/wp-json/mwai-ui/v1/chats/submit", json=data, proxy=proxy) as response:
  46. response.raise_for_status()
  47. async for chunk in response.content:
  48. if chunk.startswith(b"data: "):
  49. data = json.loads(chunk[6:])
  50. if data["type"] == "live":
  51. yield data["data"]