NoowAi.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 NoowAi(AsyncGeneratorProvider):
  8. url = "https://noowai.com"
  9. supports_message_history = True
  10. supports_gpt_35_turbo = True
  11. working = 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/118.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. "Alt-Used": "noowai.com",
  29. "Connection": "keep-alive",
  30. "Sec-Fetch-Dest": "empty",
  31. "Sec-Fetch-Mode": "cors",
  32. "Sec-Fetch-Site": "same-origin",
  33. "Pragma": "no-cache",
  34. "Cache-Control": "no-cache",
  35. "TE": "trailers"
  36. }
  37. async with ClientSession(headers=headers) as session:
  38. data = {
  39. "botId": "default",
  40. "customId": "d49bc3670c3d858458576d75c8ea0f5d",
  41. "session": "N/A",
  42. "chatId": get_random_string(),
  43. "contextId": 25,
  44. "messages": messages,
  45. "newMessage": messages[-1]["content"],
  46. "stream": True
  47. }
  48. async with session.post(f"{cls.url}/wp-json/mwai-ui/v1/chats/submit", json=data, proxy=proxy) as response:
  49. response.raise_for_status()
  50. async for line in response.content:
  51. if line.startswith(b"data: "):
  52. try:
  53. line = json.loads(line[6:])
  54. assert "type" in line
  55. except:
  56. raise RuntimeError(f"Broken line: {line.decode()}")
  57. if line["type"] == "live":
  58. yield line["data"]
  59. elif line["type"] == "end":
  60. break
  61. elif line["type"] == "error":
  62. raise RuntimeError(line["data"])