Liaobots.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from __future__ import annotations
  2. import uuid
  3. from aiohttp import ClientSession
  4. from ..typing import AsyncResult, Messages
  5. from .base_provider import AsyncGeneratorProvider
  6. models = {
  7. "gpt-4": {
  8. "id": "gpt-4",
  9. "name": "GPT-4",
  10. "maxLength": 24000,
  11. "tokenLimit": 8000,
  12. },
  13. "gpt-4-0613": {
  14. "id": "gpt-4-0613",
  15. "name": "GPT-4",
  16. "maxLength": 32000,
  17. "tokenLimit": 8000,
  18. },
  19. "gpt-3.5-turbo": {
  20. "id": "gpt-3.5-turbo",
  21. "name": "GPT-3.5",
  22. "maxLength": 12000,
  23. "tokenLimit": 4000,
  24. },
  25. "gpt-3.5-turbo-16k": {
  26. "id": "gpt-3.5-turbo-16k",
  27. "name": "GPT-3.5-16k",
  28. "maxLength": 48000,
  29. "tokenLimit": 16000,
  30. },
  31. }
  32. class Liaobots(AsyncGeneratorProvider):
  33. url = "https://liaobots.site"
  34. working = True
  35. supports_message_history = True
  36. supports_gpt_35_turbo = True
  37. supports_gpt_4 = True
  38. _auth_code = None
  39. @classmethod
  40. async def create_async_generator(
  41. cls,
  42. model: str,
  43. messages: Messages,
  44. auth: str = None,
  45. proxy: str = None,
  46. **kwargs
  47. ) -> AsyncResult:
  48. model = model if model in models else "gpt-3.5-turbo"
  49. headers = {
  50. "authority": "liaobots.com",
  51. "content-type": "application/json",
  52. "origin": cls.url,
  53. "referer": f"{cls.url}/",
  54. "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36",
  55. }
  56. async with ClientSession(
  57. headers=headers
  58. ) as session:
  59. cls._auth_code = auth if isinstance(auth, str) else cls._auth_code
  60. if not cls._auth_code:
  61. async with session.post(
  62. "https://liaobots.work/recaptcha/api/login",
  63. proxy=proxy,
  64. data={"token": "abcdefghijklmnopqrst"},
  65. verify_ssl=False
  66. ) as response:
  67. response.raise_for_status()
  68. async with session.post(
  69. "https://liaobots.work/api/user",
  70. proxy=proxy,
  71. json={"authcode": ""},
  72. verify_ssl=False
  73. ) as response:
  74. response.raise_for_status()
  75. cls._auth_code = (await response.json(content_type=None))["authCode"]
  76. data = {
  77. "conversationId": str(uuid.uuid4()),
  78. "model": models[model],
  79. "messages": messages,
  80. "key": "",
  81. "prompt": kwargs.get("system_message", "You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully."),
  82. }
  83. async with session.post(
  84. "https://liaobots.work/api/chat",
  85. proxy=proxy,
  86. json=data,
  87. headers={"x-auth-code": cls._auth_code},
  88. verify_ssl=False
  89. ) as response:
  90. response.raise_for_status()
  91. async for stream in response.content.iter_any():
  92. if stream:
  93. yield stream.decode()