Reka.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. from __future__ import annotations
  2. import os, requests, time, json
  3. from ...typing import CreateResult, Messages, ImageType
  4. from ..base_provider import AbstractProvider
  5. from ...cookies import get_cookies
  6. from ...image import to_bytes
  7. class Reka(AbstractProvider):
  8. url = "https://chat.reka.ai/"
  9. working = True
  10. needs_auth = True
  11. supports_stream = True
  12. default_vision_model = "reka"
  13. cookies = {}
  14. @classmethod
  15. def create_completion(
  16. cls,
  17. model: str,
  18. messages: Messages,
  19. stream: bool,
  20. proxy: str = None,
  21. api_key: str = None,
  22. image: ImageType = None,
  23. **kwargs
  24. ) -> CreateResult:
  25. cls.proxy = proxy
  26. if not api_key:
  27. cls.cookies = get_cookies("chat.reka.ai")
  28. if not cls.cookies:
  29. raise ValueError("No cookies found for chat.reka.ai")
  30. elif "appSession" not in cls.cookies:
  31. raise ValueError("No appSession found in cookies for chat.reka.ai, log in or provide bearer_auth")
  32. api_key = cls.get_access_token(cls)
  33. conversation = []
  34. for message in messages:
  35. conversation.append({
  36. "type": "human",
  37. "text": message["content"],
  38. })
  39. if image:
  40. image_url = cls.upload_image(cls, api_key, image)
  41. conversation[-1]["image_url"] = image_url
  42. conversation[-1]["media_type"] = "image"
  43. headers = {
  44. 'accept': '*/*',
  45. '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',
  46. 'authorization': f'Bearer {api_key}',
  47. 'cache-control': 'no-cache',
  48. 'content-type': 'application/json',
  49. 'origin': 'https://chat.reka.ai',
  50. 'pragma': 'no-cache',
  51. 'priority': 'u=1, i',
  52. 'sec-ch-ua': '"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',
  53. 'sec-ch-ua-mobile': '?0',
  54. 'sec-ch-ua-platform': '"macOS"',
  55. 'sec-fetch-dest': 'empty',
  56. 'sec-fetch-mode': 'cors',
  57. 'sec-fetch-site': 'same-origin',
  58. 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
  59. }
  60. json_data = {
  61. 'conversation_history': conversation,
  62. 'stream': True,
  63. 'use_search_engine': False,
  64. 'use_code_interpreter': False,
  65. 'model_name': 'reka-core',
  66. 'random_seed': int(time.time() * 1000),
  67. }
  68. tokens = ''
  69. response = requests.post('https://chat.reka.ai/api/chat',
  70. cookies=cls.cookies, headers=headers, json=json_data, proxies=cls.proxy, stream=True)
  71. for completion in response.iter_lines():
  72. if b'data' in completion:
  73. token_data = json.loads(completion.decode('utf-8')[5:])['text']
  74. yield (token_data.replace(tokens, ''))
  75. tokens = token_data
  76. def upload_image(cls, access_token, image: ImageType) -> str:
  77. boundary_token = os.urandom(8).hex()
  78. headers = {
  79. 'accept': '*/*',
  80. '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',
  81. 'cache-control': 'no-cache',
  82. 'authorization': f'Bearer {access_token}',
  83. 'content-type': f'multipart/form-data; boundary=----WebKitFormBoundary{boundary_token}',
  84. 'origin': 'https://chat.reka.ai',
  85. 'pragma': 'no-cache',
  86. 'priority': 'u=1, i',
  87. 'referer': 'https://chat.reka.ai/chat/hPReZExtDOPvUfF8vCPC',
  88. 'sec-ch-ua': '"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',
  89. 'sec-ch-ua-mobile': '?0',
  90. 'sec-ch-ua-platform': '"macOS"',
  91. 'sec-fetch-dest': 'empty',
  92. 'sec-fetch-mode': 'cors',
  93. 'sec-fetch-site': 'same-origin',
  94. 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
  95. }
  96. image_data = to_bytes(image)
  97. boundary = f'----WebKitFormBoundary{boundary_token}'
  98. data = f'--{boundary}\r\nContent-Disposition: form-data; name="image"; filename="image.png"\r\nContent-Type: image/png\r\n\r\n'
  99. data += image_data.decode('latin-1')
  100. data += f'\r\n--{boundary}--\r\n'
  101. response = requests.post('https://chat.reka.ai/api/upload-image',
  102. cookies=cls.cookies, headers=headers, proxies=cls.proxy, data=data.encode('latin-1'))
  103. return response.json()['media_url']
  104. def get_access_token(cls):
  105. headers = {
  106. 'accept': '*/*',
  107. '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',
  108. 'cache-control': 'no-cache',
  109. 'pragma': 'no-cache',
  110. 'priority': 'u=1, i',
  111. 'referer': 'https://chat.reka.ai/chat',
  112. 'sec-ch-ua': '"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',
  113. 'sec-ch-ua-mobile': '?0',
  114. 'sec-ch-ua-platform': '"macOS"',
  115. 'sec-fetch-dest': 'empty',
  116. 'sec-fetch-mode': 'cors',
  117. 'sec-fetch-site': 'same-origin',
  118. 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
  119. }
  120. try:
  121. response = requests.get('https://chat.reka.ai/bff/auth/access_token',
  122. cookies=cls.cookies, headers=headers, proxies=cls.proxy)
  123. return response.json()['accessToken']
  124. except Exception as e:
  125. raise ValueError(f"Failed to get access token: {e}, refresh your cookies / log in into chat.reka.ai")