WhiteRabbitNeo.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from __future__ import annotations
  2. from aiohttp import ClientSession, BaseConnector
  3. from ...typing import AsyncResult, Messages, Cookies
  4. from ...requests.raise_for_status import raise_for_status
  5. from ..base_provider import AsyncGeneratorProvider
  6. from ..helper import get_cookies, get_connector, get_random_string
  7. class WhiteRabbitNeo(AsyncGeneratorProvider):
  8. url = "https://www.whiterabbitneo.com"
  9. working = True
  10. supports_message_history = True
  11. needs_auth = True
  12. @classmethod
  13. async def create_async_generator(
  14. cls,
  15. model: str,
  16. messages: Messages,
  17. cookies: Cookies = None,
  18. connector: BaseConnector = None,
  19. proxy: str = None,
  20. **kwargs
  21. ) -> AsyncResult:
  22. if cookies is None:
  23. cookies = get_cookies("www.whiterabbitneo.com")
  24. headers = {
  25. "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0",
  26. "Accept": "*/*",
  27. "Accept-Language": "de,en-US;q=0.7,en;q=0.3",
  28. "Accept-Encoding": "gzip, deflate, br",
  29. "Referer": f"{cls.url}/",
  30. "Content-Type": "text/plain;charset=UTF-8",
  31. "Origin": cls.url,
  32. "Connection": "keep-alive",
  33. "Sec-Fetch-Dest": "empty",
  34. "Sec-Fetch-Mode": "cors",
  35. "Sec-Fetch-Site": "same-origin",
  36. "TE": "trailers"
  37. }
  38. async with ClientSession(
  39. headers=headers,
  40. cookies=cookies,
  41. connector=get_connector(connector, proxy)
  42. ) as session:
  43. data = {
  44. "messages": messages,
  45. "id": get_random_string(6),
  46. "enhancePrompt": False,
  47. "useFunctions": False
  48. }
  49. async with session.post(f"{cls.url}/api/chat", json=data, proxy=proxy) as response:
  50. await raise_for_status(response)
  51. async for chunk in response.content.iter_any():
  52. if chunk:
  53. yield chunk.decode(errors="ignore")