Blackbox.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. from __future__ import annotations
  2. from aiohttp import ClientSession
  3. import random
  4. import string
  5. import json
  6. import re
  7. import aiohttp
  8. from ..typing import AsyncResult, Messages, ImageType
  9. from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
  10. from ..image import ImageResponse, to_data_uri
  11. class Blackbox(AsyncGeneratorProvider, ProviderModelMixin):
  12. label = "Blackbox AI"
  13. url = "https://www.blackbox.ai"
  14. api_endpoint = "https://www.blackbox.ai/api/chat"
  15. working = True
  16. supports_stream = True
  17. supports_system_message = True
  18. supports_message_history = True
  19. _last_validated_value = None
  20. default_model = 'blackboxai'
  21. default_vision_model = default_model
  22. default_image_model = 'Image Generation'
  23. image_models = ['Image Generation', 'repomap']
  24. vision_models = [default_model, 'gpt-4o', 'gemini-pro', 'gemini-1.5-flash', 'llama-3.1-8b', 'llama-3.1-70b', 'llama-3.1-405b']
  25. userSelectedModel = ['gpt-4o', 'gemini-pro', 'claude-sonnet-3.5', 'blackboxai-pro']
  26. agentMode = {
  27. 'Image Generation': {'mode': True, 'id': "ImageGenerationLV45LJp", 'name': "Image Generation"},
  28. }
  29. trendingAgentMode = {
  30. "gemini-1.5-flash": {'mode': True, 'id': 'Gemini'},
  31. "llama-3.1-8b": {'mode': True, 'id': "llama-3.1-8b"},
  32. 'llama-3.1-70b': {'mode': True, 'id': "llama-3.1-70b"},
  33. 'llama-3.1-405b': {'mode': True, 'id': "llama-3.1-405"},
  34. #
  35. 'Python Agent': {'mode': True, 'id': "Python Agent"},
  36. 'Java Agent': {'mode': True, 'id': "Java Agent"},
  37. 'JavaScript Agent': {'mode': True, 'id': "JavaScript Agent"},
  38. 'HTML Agent': {'mode': True, 'id': "HTML Agent"},
  39. 'Google Cloud Agent': {'mode': True, 'id': "Google Cloud Agent"},
  40. 'Android Developer': {'mode': True, 'id': "Android Developer"},
  41. 'Swift Developer': {'mode': True, 'id': "Swift Developer"},
  42. 'Next.js Agent': {'mode': True, 'id': "Next.js Agent"},
  43. 'MongoDB Agent': {'mode': True, 'id': "MongoDB Agent"},
  44. 'PyTorch Agent': {'mode': True, 'id': "PyTorch Agent"},
  45. 'React Agent': {'mode': True, 'id': "React Agent"},
  46. 'Xcode Agent': {'mode': True, 'id': "Xcode Agent"},
  47. 'AngularJS Agent': {'mode': True, 'id': "AngularJS Agent"},
  48. #
  49. 'blackboxai-pro': {'mode': True, 'id': "BLACKBOXAI-PRO"},
  50. #
  51. 'repomap': {'mode': True, 'id': "repomap"},
  52. #
  53. 'Heroku Agent': {'mode': True, 'id': "Heroku Agent"},
  54. 'Godot Agent': {'mode': True, 'id': "Godot Agent"},
  55. 'Go Agent': {'mode': True, 'id': "Go Agent"},
  56. 'Gitlab Agent': {'mode': True, 'id': "Gitlab Agent"},
  57. 'Git Agent': {'mode': True, 'id': "Git Agent"},
  58. 'Flask Agent': {'mode': True, 'id': "Flask Agent"},
  59. 'Firebase Agent': {'mode': True, 'id': "Firebase Agent"},
  60. 'FastAPI Agent': {'mode': True, 'id': "FastAPI Agent"},
  61. 'Erlang Agent': {'mode': True, 'id': "Erlang Agent"},
  62. 'Electron Agent': {'mode': True, 'id': "Electron Agent"},
  63. 'Docker Agent': {'mode': True, 'id': "Docker Agent"},
  64. 'DigitalOcean Agent': {'mode': True, 'id': "DigitalOcean Agent"},
  65. 'Bitbucket Agent': {'mode': True, 'id': "Bitbucket Agent"},
  66. 'Azure Agent': {'mode': True, 'id': "Azure Agent"},
  67. 'Flutter Agent': {'mode': True, 'id': "Flutter Agent"},
  68. 'Youtube Agent': {'mode': True, 'id': "Youtube Agent"},
  69. 'builder Agent': {'mode': True, 'id': "builder Agent"},
  70. }
  71. additional_prefixes = {
  72. 'gpt-4o': '@gpt-4o',
  73. 'gemini-pro': '@gemini-pro',
  74. 'claude-sonnet-3.5': '@claude-sonnet'
  75. }
  76. model_prefixes = {
  77. **{
  78. mode: f"@{value['id']}" for mode, value in trendingAgentMode.items()
  79. if mode not in ["gemini-1.5-flash", "llama-3.1-8b", "llama-3.1-70b", "llama-3.1-405b", "repomap"]
  80. },
  81. **additional_prefixes
  82. }
  83. models = list(dict.fromkeys([default_model, *userSelectedModel, *list(agentMode.keys()), *list(trendingAgentMode.keys())]))
  84. model_aliases = {
  85. "gemini-flash": "gemini-1.5-flash",
  86. "claude-3.5-sonnet": "claude-sonnet-3.5",
  87. "flux": "Image Generation",
  88. }
  89. @classmethod
  90. async def fetch_validated(cls):
  91. if cls._last_validated_value:
  92. return cls._last_validated_value
  93. async with aiohttp.ClientSession() as session:
  94. try:
  95. async with session.get(cls.url) as response:
  96. if response.status != 200:
  97. print("Failed to load the page.")
  98. return cls._last_validated_value
  99. page_content = await response.text()
  100. js_files = re.findall(r'static/chunks/\d{4}-[a-fA-F0-9]+\.js', page_content)
  101. key_pattern = re.compile(r'w="([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})"')
  102. for js_file in js_files:
  103. js_url = f"{cls.url}/_next/{js_file}"
  104. async with session.get(js_url) as js_response:
  105. if js_response.status == 200:
  106. js_content = await js_response.text()
  107. match = key_pattern.search(js_content)
  108. if match:
  109. validated_value = match.group(1)
  110. cls._last_validated_value = validated_value
  111. return validated_value
  112. except Exception as e:
  113. print(f"Error fetching validated value: {e}")
  114. return cls._last_validated_value
  115. @staticmethod
  116. def generate_id(length=7):
  117. characters = string.ascii_letters + string.digits
  118. return ''.join(random.choice(characters) for _ in range(length))
  119. @classmethod
  120. def add_prefix_to_messages(cls, messages: Messages, model: str) -> Messages:
  121. prefix = cls.model_prefixes.get(model, "")
  122. if not prefix:
  123. return messages
  124. new_messages = []
  125. for message in messages:
  126. new_message = message.copy()
  127. if message['role'] == 'user':
  128. new_message['content'] = (prefix + " " + message['content']).strip()
  129. new_messages.append(new_message)
  130. return new_messages
  131. @classmethod
  132. async def create_async_generator(
  133. cls,
  134. model: str,
  135. messages: Messages,
  136. prompt: str = None,
  137. proxy: str = None,
  138. web_search: bool = False,
  139. image: ImageType = None,
  140. image_name: str = None,
  141. **kwargs
  142. ) -> AsyncResult:
  143. message_id = cls.generate_id()
  144. messages = cls.add_prefix_to_messages(messages, model)
  145. validated_value = await cls.fetch_validated()
  146. if image is not None:
  147. messages[-1]['data'] = {
  148. 'fileText': '',
  149. 'imageBase64': to_data_uri(image),
  150. 'title': image_name
  151. }
  152. headers = {
  153. 'accept': '*/*',
  154. 'accept-language': 'en-US,en;q=0.9',
  155. 'cache-control': 'no-cache',
  156. 'content-type': 'application/json',
  157. 'origin': cls.url,
  158. 'pragma': 'no-cache',
  159. 'priority': 'u=1, i',
  160. 'referer': f'{cls.url}/',
  161. 'sec-ch-ua': '"Not?A_Brand";v="99", "Chromium";v="130"',
  162. 'sec-ch-ua-mobile': '?0',
  163. 'sec-ch-ua-platform': '"Linux"',
  164. 'sec-fetch-dest': 'empty',
  165. 'sec-fetch-mode': 'cors',
  166. 'sec-fetch-site': 'same-origin',
  167. 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36'
  168. }
  169. data = {
  170. "messages": messages,
  171. "id": message_id,
  172. "previewToken": None,
  173. "userId": None,
  174. "codeModelMode": True,
  175. "agentMode": cls.agentMode.get(model, {}) if model in cls.agentMode else {},
  176. "trendingAgentMode": cls.trendingAgentMode.get(model, {}) if model in cls.trendingAgentMode else {},
  177. "isMicMode": False,
  178. "userSystemPrompt": None,
  179. "maxTokens": 1024,
  180. "playgroundTopP": 0.9,
  181. "playgroundTemperature": 0.5,
  182. "isChromeExt": False,
  183. "githubToken": None,
  184. "clickedAnswer2": False,
  185. "clickedAnswer3": False,
  186. "clickedForceWebSearch": False,
  187. "visitFromDelta": False,
  188. "mobileClient": False,
  189. "userSelectedModel": model if model in cls.userSelectedModel else None,
  190. "webSearchMode": web_search,
  191. "validated": validated_value,
  192. }
  193. async with ClientSession(headers=headers) as session:
  194. async with session.post(cls.api_endpoint, json=data, proxy=proxy) as response:
  195. response.raise_for_status()
  196. response_text = await response.text()
  197. if model in cls.image_models:
  198. image_matches = re.findall(r'!\[.*?\]\((https?://[^\)]+)\)', response_text)
  199. if image_matches:
  200. image_url = image_matches[0]
  201. yield ImageResponse(image_url, prompt)
  202. return
  203. response_text = re.sub(r'Generated by BLACKBOX.AI, try unlimited chat https://www.blackbox.ai', '', response_text, flags=re.DOTALL)
  204. json_match = re.search(r'\$~~~\$(.*?)\$~~~\$', response_text, re.DOTALL)
  205. if json_match:
  206. search_results = json.loads(json_match.group(1))
  207. answer = response_text.split('$~~~$')[-1].strip()
  208. formatted_response = f"{answer}\n\n**Source:**"
  209. for i, result in enumerate(search_results, 1):
  210. formatted_response += f"\n{i}. {result['title']}: {result['link']}"
  211. yield formatted_response
  212. else:
  213. yield response_text.strip()