Create blackboxai.py
Browse files- api/provider/blackboxai.py +128 -0
api/provider/blackboxai.py
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from __future__ import annotations
|
2 |
+
|
3 |
+
import json
|
4 |
+
from aiohttp import ClientSession
|
5 |
+
|
6 |
+
from ..typing import AsyncResult, Messages
|
7 |
+
from ..image import ImageResponse
|
8 |
+
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
|
9 |
+
from .helper import format_prompt
|
10 |
+
|
11 |
+
class BlackBoxAI(AsyncGeneratorProvider, ProviderModelMixin):
|
12 |
+
url = "https://api.blackboxai.com/assistant/"
|
13 |
+
api_endpoint = "https://api.blackboxai.com/api/data/users/inferenceServer.infer"
|
14 |
+
working = True
|
15 |
+
|
16 |
+
supports_system_message = True
|
17 |
+
supports_message_history = True
|
18 |
+
|
19 |
+
# Chat models
|
20 |
+
default_model = 'chat-blackboxai-basic'
|
21 |
+
chat_models = [
|
22 |
+
default_model,
|
23 |
+
# Add other chat models as needed
|
24 |
+
]
|
25 |
+
|
26 |
+
# Image models
|
27 |
+
image_models = [
|
28 |
+
'bbai-sdxl',
|
29 |
+
# Add other image models as needed
|
30 |
+
]
|
31 |
+
|
32 |
+
models = [*chat_models, *image_models]
|
33 |
+
|
34 |
+
model_aliases = {
|
35 |
+
# Define aliases as needed
|
36 |
+
}
|
37 |
+
|
38 |
+
@classmethod
|
39 |
+
def get_model(cls, model: str) -> str:
|
40 |
+
if model in cls.models:
|
41 |
+
return model
|
42 |
+
elif model in cls.model_aliases:
|
43 |
+
return cls.model_aliases[model]
|
44 |
+
else:
|
45 |
+
return cls.default_model
|
46 |
+
|
47 |
+
@classmethod
|
48 |
+
def is_image_model(cls, model: str) -> bool:
|
49 |
+
return model in cls.image_models
|
50 |
+
|
51 |
+
@classmethod
|
52 |
+
async def create_async_generator(
|
53 |
+
cls,
|
54 |
+
model: str,
|
55 |
+
messages: Messages,
|
56 |
+
proxy: str = None,
|
57 |
+
**kwargs
|
58 |
+
) -> AsyncResult:
|
59 |
+
model = cls.get_model(model)
|
60 |
+
|
61 |
+
headers = {
|
62 |
+
'Accept': 'application/json, text/plain, */*',
|
63 |
+
'Accept-Language': 'en-US,en;q=0.9',
|
64 |
+
'Cache-Control': 'no-cache',
|
65 |
+
'Connection': 'keep-alive',
|
66 |
+
'Content-Type': 'application/json',
|
67 |
+
'Origin': 'https://api.blackboxai.com',
|
68 |
+
'Pragma': 'no-cache',
|
69 |
+
'Sec-Fetch-Dest': 'empty',
|
70 |
+
'Sec-Fetch-Mode': 'cors',
|
71 |
+
'Sec-Fetch-Site': 'same-origin',
|
72 |
+
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36',
|
73 |
+
'sec-ch-ua': '"Not?A_Brand";v="99", "Chromium";v="130"',
|
74 |
+
'sec-ch-ua-mobile': '?0',
|
75 |
+
'sec-ch-ua-platform': '"Linux"'
|
76 |
+
}
|
77 |
+
|
78 |
+
async with ClientSession() as session:
|
79 |
+
if cls.is_image_model(model):
|
80 |
+
# Image generation
|
81 |
+
prompt = messages[-1]["content"]
|
82 |
+
data = {
|
83 |
+
"model": model,
|
84 |
+
"input": {
|
85 |
+
"width": "1024",
|
86 |
+
"height": "1024",
|
87 |
+
"steps": 4,
|
88 |
+
"output_format": "webp",
|
89 |
+
"batch_size": 1,
|
90 |
+
"mode": "plan",
|
91 |
+
"prompt": prompt
|
92 |
+
}
|
93 |
+
}
|
94 |
+
async with session.post(
|
95 |
+
cls.api_endpoint,
|
96 |
+
headers=headers,
|
97 |
+
data=json.dumps(data),
|
98 |
+
proxy=proxy
|
99 |
+
) as response:
|
100 |
+
response.raise_for_status()
|
101 |
+
response_data = await response.json()
|
102 |
+
if response_data.get('status') == 'completed' and response_data.get('output'):
|
103 |
+
for url in response_data['output']:
|
104 |
+
yield ImageResponse(images=url, alt="Generated Image")
|
105 |
+
else:
|
106 |
+
# Chat completion
|
107 |
+
data = {
|
108 |
+
"model": model,
|
109 |
+
"input": {
|
110 |
+
"messages": [
|
111 |
+
{
|
112 |
+
"type": "human",
|
113 |
+
"content": format_prompt(messages)
|
114 |
+
}
|
115 |
+
],
|
116 |
+
"mode": "plan"
|
117 |
+
},
|
118 |
+
"noStream": True
|
119 |
+
}
|
120 |
+
async with session.post(
|
121 |
+
cls.api_endpoint,
|
122 |
+
headers=headers,
|
123 |
+
data=json.dumps(data),
|
124 |
+
proxy=proxy
|
125 |
+
) as response:
|
126 |
+
response.raise_for_status()
|
127 |
+
result = await response.json()
|
128 |
+
yield result.get('output', '')
|