Ails.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import os
  2. import time
  3. import json
  4. import uuid
  5. import random
  6. import hashlib
  7. import requests
  8. from ...typing import sha256, Dict, get_type_hints
  9. from datetime import datetime
  10. url: str = 'https://ai.ls'
  11. model: str = 'gpt-3.5-turbo'
  12. supports_stream = True
  13. needs_auth = False
  14. class Utils:
  15. def hash(json_data: Dict[str, str]) -> sha256:
  16. secretKey: bytearray = bytearray([79, 86, 98, 105, 91, 84, 80, 78, 123, 83,
  17. 35, 41, 99, 123, 51, 54, 37, 57, 63, 103, 59, 117, 115, 108, 41, 67, 76])
  18. base_string: str = '%s:%s:%s:%s' % (
  19. json_data['t'],
  20. json_data['m'],
  21. 'WI,2rU#_r:r~aF4aJ36[.Z(/8Rv93Rf',
  22. len(json_data['m'])
  23. )
  24. return hashlib.sha256(base_string.encode()).hexdigest()
  25. def format_timestamp(timestamp: int) -> str:
  26. e = timestamp
  27. n = e % 10
  28. r = n + 1 if n % 2 == 0 else n
  29. return str(e - n + r)
  30. def _create_completion(model: str, messages: list, temperature: float = 0.6, stream: bool = False, **kwargs):
  31. headers = {
  32. 'authority': 'api.caipacity.com',
  33. 'accept': '*/*',
  34. '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',
  35. 'authorization': 'Bearer free',
  36. 'client-id': str(uuid.uuid4()),
  37. 'client-v': '0.1.217',
  38. 'content-type': 'application/json',
  39. 'origin': 'https://ai.ls',
  40. 'referer': 'https://ai.ls/',
  41. 'sec-ch-ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
  42. 'sec-ch-ua-mobile': '?0',
  43. 'sec-ch-ua-platform': '"Windows"',
  44. 'sec-fetch-dest': 'empty',
  45. 'sec-fetch-mode': 'cors',
  46. 'sec-fetch-site': 'cross-site',
  47. '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',
  48. }
  49. params = {
  50. 'full': 'false',
  51. }
  52. timestamp = Utils.format_timestamp(int(time.time() * 1000))
  53. sig = {
  54. 'd': datetime.now().strftime('%Y-%m-%d'),
  55. 't': timestamp,
  56. 's': Utils.hash({
  57. 't': timestamp,
  58. 'm': messages[-1]['content']})}
  59. json_data = json.dumps(separators=(',', ':'), obj={
  60. 'model': 'gpt-3.5-turbo',
  61. 'temperature': 0.6,
  62. 'stream': True,
  63. 'messages': messages} | sig)
  64. response = requests.post('https://api.caipacity.com/v1/chat/completions',
  65. headers=headers, data=json_data, stream=True)
  66. for token in response.iter_lines():
  67. if b'content' in token:
  68. completion_chunk = json.loads(token.decode().replace('data: ', ''))
  69. token = completion_chunk['choices'][0]['delta'].get('content')
  70. if token != None:
  71. yield token
  72. params = f'g4f.Providers.{os.path.basename(__file__)[:-3]} supports: ' + \
  73. '(%s)' % ', '.join([f"{name}: {get_type_hints(_create_completion)[name].__name__}" for name in _create_completion.__code__.co_varnames[:_create_completion.__code__.co_argcount]])