Ails.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from __future__ import annotations
  2. import hashlib
  3. import time
  4. import uuid
  5. import json
  6. from datetime import datetime
  7. from aiohttp import ClientSession
  8. from ...typing import SHA256, AsyncResult, Messages
  9. from ..base_provider import AsyncGeneratorProvider
  10. class Ails(AsyncGeneratorProvider):
  11. url = "https://ai.ls"
  12. working = False
  13. supports_message_history = True
  14. supports_gpt_35_turbo = True
  15. @staticmethod
  16. async def create_async_generator(
  17. model: str,
  18. messages: Messages,
  19. stream: bool,
  20. proxy: str = None,
  21. **kwargs
  22. ) -> AsyncResult:
  23. headers = {
  24. "authority": "api.caipacity.com",
  25. "accept": "*/*",
  26. "accept-language": "en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3",
  27. "authorization": "Bearer free",
  28. "client-id": str(uuid.uuid4()),
  29. "client-v": "0.1.278",
  30. "content-type": "application/json",
  31. "origin": "https://ai.ls",
  32. "referer": "https://ai.ls/",
  33. "sec-ch-ua": '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
  34. "sec-ch-ua-mobile": "?0",
  35. "sec-ch-ua-platform": '"Windows"',
  36. "sec-fetch-dest": "empty",
  37. "sec-fetch-mode": "cors",
  38. "sec-fetch-site": "cross-site",
  39. "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
  40. "from-url": "https://ai.ls/?chat=1"
  41. }
  42. async with ClientSession(
  43. headers=headers
  44. ) as session:
  45. timestamp = _format_timestamp(int(time.time() * 1000))
  46. json_data = {
  47. "model": "gpt-3.5-turbo",
  48. "temperature": kwargs.get("temperature", 0.6),
  49. "stream": True,
  50. "messages": messages,
  51. "d": datetime.now().strftime("%Y-%m-%d"),
  52. "t": timestamp,
  53. "s": _hash({"t": timestamp, "m": messages[-1]["content"]}),
  54. }
  55. async with session.post(
  56. "https://api.caipacity.com/v1/chat/completions",
  57. proxy=proxy,
  58. json=json_data
  59. ) as response:
  60. response.raise_for_status()
  61. start = "data: "
  62. async for line in response.content:
  63. line = line.decode('utf-8')
  64. if line.startswith(start) and line != "data: [DONE]":
  65. line = line[len(start):-1]
  66. line = json.loads(line)
  67. token = line["choices"][0]["delta"].get("content")
  68. if token:
  69. if "ai.ls" in token or "ai.ci" in token:
  70. raise Exception(f"Response Error: {token}")
  71. yield token
  72. def _hash(json_data: dict[str, str]) -> SHA256:
  73. base_string: str = f'{json_data["t"]}:{json_data["m"]}:WI,2rU#_r:r~aF4aJ36[.Z(/8Rv93Rf:{len(json_data["m"])}'
  74. return SHA256(hashlib.sha256(base_string.encode()).hexdigest())
  75. def _format_timestamp(timestamp: int) -> str:
  76. e = timestamp
  77. n = e % 10
  78. r = n + 1 if n % 2 == 0 else n
  79. return str(e - n + r)