Yqcloud.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from __future__ import annotations
  2. import random
  3. from ...requests import StreamSession
  4. from ...typing import AsyncResult, Messages
  5. from ..base_provider import AsyncGeneratorProvider, format_prompt
  6. class Yqcloud(AsyncGeneratorProvider):
  7. url = "https://chat9.yqcloud.top/"
  8. working = True
  9. supports_gpt_35_turbo = True
  10. @staticmethod
  11. async def create_async_generator(
  12. model: str,
  13. messages: Messages,
  14. proxy: str = None,
  15. timeout: int = 120,
  16. **kwargs,
  17. ) -> AsyncResult:
  18. async with StreamSession(
  19. headers=_create_header(), proxies={"https": proxy}, timeout=timeout
  20. ) as session:
  21. payload = _create_payload(messages, **kwargs)
  22. async with session.post("https://api.aichatos.cloud/api/generateStream", json=payload) as response:
  23. response.raise_for_status()
  24. async for chunk in response.iter_content():
  25. if chunk:
  26. chunk = chunk.decode()
  27. if "sorry, 您的ip已由于触发防滥用检测而被封禁" in chunk:
  28. raise RuntimeError("IP address is blocked by abuse detection.")
  29. yield chunk
  30. def _create_header():
  31. return {
  32. "accept" : "application/json, text/plain, */*",
  33. "content-type" : "application/json",
  34. "origin" : "https://chat9.yqcloud.top",
  35. "referer" : "https://chat9.yqcloud.top/"
  36. }
  37. def _create_payload(
  38. messages: Messages,
  39. system_message: str = "",
  40. user_id: int = None,
  41. **kwargs
  42. ):
  43. if not user_id:
  44. user_id = random.randint(1690000544336, 2093025544336)
  45. return {
  46. "prompt": format_prompt(messages),
  47. "network": True,
  48. "system": system_message,
  49. "withoutContext": False,
  50. "stream": True,
  51. "userId": f"#/chat/{user_id}"
  52. }