Blackbox.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. from __future__ import annotations
  2. from aiohttp import ClientSession
  3. import re
  4. import json
  5. import random
  6. import string
  7. from pathlib import Path
  8. from typing import Optional
  9. from datetime import datetime, timezone
  10. from ..typing import AsyncResult, Messages, ImagesType
  11. from ..requests.raise_for_status import raise_for_status
  12. from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
  13. from ..image import to_data_uri
  14. from ..cookies import get_cookies_dir
  15. from .helper import format_prompt, format_image_prompt
  16. from ..providers.response import JsonConversation, ImageResponse
  17. from ..errors import ModelNotSupportedError
  18. class Conversation(JsonConversation):
  19. validated_value: str = None
  20. chat_id: str = None
  21. message_history: Messages = []
  22. def __init__(self, model: str):
  23. self.model = model
  24. class Blackbox(AsyncGeneratorProvider, ProviderModelMixin):
  25. label = "Blackbox AI"
  26. url = "https://www.blackbox.ai"
  27. api_endpoint = "https://www.blackbox.ai/api/chat"
  28. working = True
  29. supports_stream = True
  30. supports_system_message = True
  31. supports_message_history = True
  32. default_model = "blackboxai"
  33. default_vision_model = default_model
  34. default_image_model = 'ImageGeneration'
  35. image_models = [default_image_model]
  36. vision_models = [default_vision_model, 'GPT-4o', 'o3-mini', 'Gemini-PRO', 'gemini-1.5-flash', 'llama-3.1-8b', 'llama-3.1-70b', 'llama-3.1-405b', 'Gemini-Flash-2.0']
  37. userSelectedModel = ['GPT-4o', 'o3-mini', 'Gemini-PRO', 'Claude-Sonnet-3.5', 'DeepSeek-V3', 'DeepSeek-R1', 'blackboxai-pro', 'Meta-Llama-3.3-70B-Instruct-Turbo', 'Mistral-Small-24B-Instruct-2501', 'DeepSeek-LLM-Chat-(67B)', 'DBRX-Instruct', 'Qwen-QwQ-32B-Preview', 'Nous-Hermes-2-Mixtral-8x7B-DPO', 'Gemini-Flash-2.0']
  38. agentMode = {
  39. 'DeepSeek-V3': {'mode': True, 'id': "deepseek-chat", 'name': "DeepSeek-V3"},
  40. 'DeepSeek-R1': {'mode': True, 'id': "deepseek-reasoner", 'name': "DeepSeek-R1"},
  41. 'Meta-Llama-3.3-70B-Instruct-Turbo': {'mode': True, 'id': "meta-llama/Llama-3.3-70B-Instruct-Turbo", 'name': "Meta-Llama-3.3-70B-Instruct-Turbo"},
  42. 'Mistral-Small-24B-Instruct-2501': {'mode': True, 'id': "mistralai/Mistral-Small-24B-Instruct-2501", 'name': "Mistral-Small-24B-Instruct-2501"},
  43. 'DeepSeek-LLM-Chat-(67B)': {'mode': True, 'id': "deepseek-ai/deepseek-llm-67b-chat", 'name': "DeepSeek-LLM-Chat-(67B)"},
  44. 'DBRX-Instruct': {'mode': True, 'id': "databricks/dbrx-instruct", 'name': "DBRX-Instruct"},
  45. 'Qwen-QwQ-32B-Preview': {'mode': True, 'id': "Qwen/QwQ-32B-Preview", 'name': "Qwen-QwQ-32B-Preview"},
  46. 'Nous-Hermes-2-Mixtral-8x7B-DPO': {'mode': True, 'id': "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO", 'name': "Nous-Hermes-2-Mixtral-8x7B-DPO"},
  47. 'Gemini-Flash-2.0': {'mode': True, 'id': "Gemini/Gemini-Flash-2.0", 'name': "Gemini-Flash-2.0"},
  48. }
  49. trendingAgentMode = {
  50. "o3-mini": {'mode': True, 'id': 'o3-mini'},
  51. "gemini-1.5-flash": {'mode': True, 'id': 'Gemini'},
  52. "llama-3.1-8b": {'mode': True, 'id': "llama-3.1-8b"},
  53. 'llama-3.1-70b': {'mode': True, 'id': "llama-3.1-70b"},
  54. 'llama-3.1-405b': {'mode': True, 'id': "llama-3.1-405"},
  55. 'Python Agent': {'mode': True, 'id': "Python Agent"},
  56. 'Java Agent': {'mode': True, 'id': "Java Agent"},
  57. 'JavaScript Agent': {'mode': True, 'id': "JavaScript Agent"},
  58. 'HTML Agent': {'mode': True, 'id': "HTML Agent"},
  59. 'Google Cloud Agent': {'mode': True, 'id': "Google Cloud Agent"},
  60. 'Android Developer': {'mode': True, 'id': "Android Developer"},
  61. 'Swift Developer': {'mode': True, 'id': "Swift Developer"},
  62. 'Next.js Agent': {'mode': True, 'id': "Next.js Agent"},
  63. 'MongoDB Agent': {'mode': True, 'id': "MongoDB Agent"},
  64. 'PyTorch Agent': {'mode': True, 'id': "PyTorch Agent"},
  65. 'React Agent': {'mode': True, 'id': "React Agent"},
  66. 'Xcode Agent': {'mode': True, 'id': "Xcode Agent"},
  67. 'AngularJS Agent': {'mode': True, 'id': "AngularJS Agent"},
  68. 'blackboxai-pro': {'mode': True, 'id': "BLACKBOXAI-PRO"},
  69. 'repomap': {'mode': True, 'id': "repomap"},
  70. 'Heroku Agent': {'mode': True, 'id': "Heroku Agent"},
  71. 'Godot Agent': {'mode': True, 'id': "Godot Agent"},
  72. 'Go Agent': {'mode': True, 'id': "Go Agent"},
  73. 'Gitlab Agent': {'mode': True, 'id': "Gitlab Agent"},
  74. 'Git Agent': {'mode': True, 'id': "Git Agent"},
  75. 'Flask Agent': {'mode': True, 'id': "Flask Agent"},
  76. 'Firebase Agent': {'mode': True, 'id': "Firebase Agent"},
  77. 'FastAPI Agent': {'mode': True, 'id': "FastAPI Agent"},
  78. 'Erlang Agent': {'mode': True, 'id': "Erlang Agent"},
  79. 'Electron Agent': {'mode': True, 'id': "Electron Agent"},
  80. 'Docker Agent': {'mode': True, 'id': "Docker Agent"},
  81. 'DigitalOcean Agent': {'mode': True, 'id': "DigitalOcean Agent"},
  82. 'Bitbucket Agent': {'mode': True, 'id': "Bitbucket Agent"},
  83. 'Azure Agent': {'mode': True, 'id': "Azure Agent"},
  84. 'Flutter Agent': {'mode': True, 'id': "Flutter Agent"},
  85. 'Youtube Agent': {'mode': True, 'id': "Youtube Agent"},
  86. 'builder Agent': {'mode': True, 'id': "builder Agent"},
  87. }
  88. premium_models = ['Claude-Sonnet-3.5']
  89. models = list(dict.fromkeys([default_model, *userSelectedModel, *image_models, *list(agentMode.keys()), *list(trendingAgentMode.keys())]))
  90. model_aliases = {
  91. "gpt-4": "GPT-4o",
  92. "gpt-4o": "GPT-4o",
  93. "claude-3.5-sonnet": "Claude-Sonnet-3.5", # Premium
  94. "gemini-1.5-flash": "gemini-1.5-flash",
  95. "gemini-1.5-pro": "Gemini-PRO",
  96. "deepseek-v3": "DeepSeek-V3",
  97. "deepseek-r1": "DeepSeek-R1",
  98. "llama-3.3-70b": "Meta-Llama-3.3-70B-Instruct-Turbo",
  99. "mixtral-small-28b": "Mistral-Small-24B-Instruct-2501",
  100. "deepseek-chat": "DeepSeek-LLM-Chat-(67B)",
  101. "dbrx-instruct": "DBRX-Instruct",
  102. "qwq-32b": "Qwen-QwQ-32B-Preview",
  103. "hermes-2-dpo": "Nous-Hermes-2-Mixtral-8x7B-DPO",
  104. "gemini-2.0-flash": "Gemini-Flash-2.0",
  105. "flux": "ImageGeneration",
  106. }
  107. @classmethod
  108. def get_models(cls) -> list[str]:
  109. models = super().get_models()
  110. filtered = [m for m in models if m not in cls.premium_models]
  111. filtered += [f"{m} (Premium)" for m in cls.premium_models]
  112. return filtered
  113. @classmethod
  114. def get_model(cls, model: str, **kwargs) -> str:
  115. try:
  116. model = super().get_model(model, **kwargs)
  117. return model.split(" (Premium)")[0]
  118. except ModelNotSupportedError:
  119. base_model = model.split(" (Premium)")[0]
  120. if base_model in cls.premium_models:
  121. return base_model
  122. raise
  123. @classmethod
  124. async def fetch_validated(cls, url: str = "https://www.blackbox.ai", force_refresh: bool = False) -> Optional[str]:
  125. cache_file = Path(get_cookies_dir()) / 'blackbox.json'
  126. if not force_refresh and cache_file.exists():
  127. try:
  128. with open(cache_file, 'r') as f:
  129. data = json.load(f)
  130. if data.get('validated_value'):
  131. return data['validated_value']
  132. except Exception as e:
  133. print(f"Error reading cache: {e}")
  134. js_file_pattern = r'static/chunks/\d{4}-[a-fA-F0-9]+\.js'
  135. uuid_pattern = r'["\']([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})["\']'
  136. def is_valid_context(text: str) -> bool:
  137. return any(char + '=' in text for char in 'abcdefghijklmnopqrstuvwxyz')
  138. async with ClientSession() as session:
  139. try:
  140. async with session.get(url) as response:
  141. if response.status != 200:
  142. return None
  143. page_content = await response.text()
  144. js_files = re.findall(js_file_pattern, page_content)
  145. for js_file in js_files:
  146. js_url = f"{url}/_next/{js_file}"
  147. async with session.get(js_url) as js_response:
  148. if js_response.status == 200:
  149. js_content = await js_response.text()
  150. for match in re.finditer(uuid_pattern, js_content):
  151. start = max(0, match.start() - 10)
  152. end = min(len(js_content), match.end() + 10)
  153. context = js_content[start:end]
  154. if is_valid_context(context):
  155. validated_value = match.group(1)
  156. cache_file.parent.mkdir(exist_ok=True)
  157. try:
  158. with open(cache_file, 'w') as f:
  159. json.dump({'validated_value': validated_value}, f)
  160. except Exception as e:
  161. print(f"Error writing cache: {e}")
  162. return validated_value
  163. except Exception as e:
  164. print(f"Error retrieving validated_value: {e}")
  165. return None
  166. @classmethod
  167. def generate_id(cls, length: int = 7) -> str:
  168. chars = string.ascii_letters + string.digits
  169. return ''.join(random.choice(chars) for _ in range(length))
  170. @classmethod
  171. async def create_async_generator(
  172. cls,
  173. model: str,
  174. messages: Messages,
  175. prompt: str = None,
  176. proxy: str = None,
  177. images: ImagesType = None,
  178. top_p: float = None,
  179. temperature: float = None,
  180. max_tokens: int = None,
  181. conversation: Conversation = None,
  182. return_conversation: bool = False,
  183. **kwargs
  184. ) -> AsyncResult:
  185. model = cls.get_model(model)
  186. headers = {
  187. 'accept': '*/*',
  188. 'accept-language': 'en-US,en;q=0.9',
  189. 'content-type': 'application/json',
  190. 'origin': 'https://www.blackbox.ai',
  191. 'referer': 'https://www.blackbox.ai/',
  192. 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
  193. }
  194. async with ClientSession(headers=headers) as session:
  195. if model in "ImageGeneration":
  196. prompt = format_image_prompt(messages, prompt)
  197. data = {
  198. "query": format_image_prompt(messages, prompt),
  199. "agentMode": True
  200. }
  201. headers['content-type'] = 'text/plain;charset=UTF-8'
  202. async with session.post(
  203. "https://www.blackbox.ai/api/image-generator",
  204. json=data,
  205. proxy=proxy,
  206. headers=headers
  207. ) as response:
  208. await raise_for_status(response)
  209. response_json = await response.json()
  210. if "markdown" in response_json:
  211. image_url_match = re.search(r'!\[.*?\]\((.*?)\)', response_json["markdown"])
  212. if image_url_match:
  213. image_url = image_url_match.group(1)
  214. yield ImageResponse(images=[image_url], alt=format_image_prompt(messages, prompt))
  215. return
  216. if conversation is None or not hasattr(conversation, "chat_id"):
  217. conversation = Conversation(model)
  218. conversation.validated_value = await cls.fetch_validated()
  219. conversation.chat_id = cls.generate_id()
  220. conversation.message_history = []
  221. current_messages = []
  222. for i, msg in enumerate(messages):
  223. msg_id = conversation.chat_id if i == 0 and msg["role"] == "user" else cls.generate_id()
  224. current_msg = {
  225. "id": msg_id,
  226. "content": msg["content"],
  227. "role": msg["role"]
  228. }
  229. if msg["role"] == "assistant" and i == len(messages)-1:
  230. current_time = datetime.now(timezone.utc).isoformat(timespec='milliseconds').replace('+00:00', 'Z')
  231. current_msg["createdAt"] = current_time
  232. current_messages.append(current_msg)
  233. if images is not None:
  234. current_messages[-1]['data'] = {
  235. "imagesData": [
  236. {
  237. "filePath": f"/{image_name}",
  238. "contents": to_data_uri(image)
  239. }
  240. for image, image_name in images
  241. ],
  242. "fileText": "",
  243. "title": ""
  244. }
  245. # Calculate the value for expires + lastChecked
  246. expires_iso = datetime.now(timezone.utc).isoformat(timespec='milliseconds').replace('+00:00', 'Z')
  247. last_checked_millis = int(datetime.now().timestamp() * 1000)
  248. # Fake data of a premium user (temporarily working)
  249. fake_session = {"user":{"name":"John Doe","email":"john.doe@gmail.com","image":"https://lh3.googleusercontent.com/a/ACg8ocK9X7mNpQ2vR4jH3tY8wL5nB1xM6fDS9JW2kLpTn4Vy3hR2xN4m=s96-c"},"expires":expires_iso}
  250. fake_subscriptionCache = {"status":"PREMIUM", "expiryTimestamp":None,"lastChecked":last_checked_millis}
  251. data = {
  252. "messages": current_messages,
  253. "agentMode": cls.agentMode.get(model, {}) if model in cls.agentMode else {},
  254. "id": conversation.chat_id,
  255. "previewToken": None,
  256. "userId": None,
  257. "codeModelMode": True,
  258. "trendingAgentMode": cls.trendingAgentMode.get(model, {}) if model in cls.trendingAgentMode else {},
  259. "isMicMode": False,
  260. "userSystemPrompt": None,
  261. "maxTokens": max_tokens,
  262. "playgroundTopP": top_p,
  263. "playgroundTemperature": temperature,
  264. "isChromeExt": False,
  265. "githubToken": "",
  266. "clickedAnswer2": False,
  267. "clickedAnswer3": False,
  268. "clickedForceWebSearch": False,
  269. "visitFromDelta": False,
  270. "isMemoryEnabled": False,
  271. "mobileClient": False,
  272. "userSelectedModel": model if model in cls.userSelectedModel else None,
  273. "validated": conversation.validated_value,
  274. "imageGenerationMode": False,
  275. "webSearchModePrompt": False,
  276. "deepSearchMode": False,
  277. "domains": None,
  278. "vscodeClient": False,
  279. "codeInterpreterMode": False,
  280. "customProfile": {"name": "", "occupation": "", "traits": [], "additionalInfo": "", "enableNewChats": False},
  281. "session": fake_session,
  282. "isPremium": True,
  283. "subscriptionCache": fake_subscriptionCache,
  284. "webSearchMode": False
  285. }
  286. async with session.post(cls.api_endpoint, json=data, proxy=proxy) as response:
  287. await raise_for_status(response)
  288. full_response = []
  289. async for chunk in response.content.iter_any():
  290. if chunk:
  291. chunk_text = chunk.decode()
  292. full_response.append(chunk_text)
  293. yield chunk_text
  294. if return_conversation:
  295. full_response_text = ''.join(full_response)
  296. conversation.message_history.append({"role": "assistant", "content": full_response_text})
  297. yield conversation