Delete api
Browse files- api/__init__.py +0 -0
- api/__pycache__/dummy.txt +0 -1
- api/app.py +0 -41
- api/auth.py +0 -10
- api/config.py +0 -226
- api/logger.py +0 -20
- api/models.py +0 -14
- api/routes.py +0 -62
- api/utils.py +0 -252
- api/validate.py +0 -62
api/__init__.py
DELETED
File without changes
|
api/__pycache__/dummy.txt
DELETED
@@ -1 +0,0 @@
|
|
1 |
-
|
|
|
|
api/app.py
DELETED
@@ -1,41 +0,0 @@
|
|
1 |
-
from fastapi import FastAPI, Request
|
2 |
-
from starlette.middleware.cors import CORSMiddleware
|
3 |
-
from fastapi.responses import JSONResponse
|
4 |
-
from api.logger import setup_logger
|
5 |
-
from api.routes import router
|
6 |
-
|
7 |
-
|
8 |
-
logger = setup_logger(__name__)
|
9 |
-
|
10 |
-
def create_app():
|
11 |
-
app = FastAPI(
|
12 |
-
title="NiansuhAI API Gateway",
|
13 |
-
docs_url=None, # Disable Swagger UI
|
14 |
-
redoc_url=None, # Disable ReDoc
|
15 |
-
openapi_url=None, # Disable OpenAPI schema
|
16 |
-
)
|
17 |
-
|
18 |
-
# CORS settings
|
19 |
-
app.add_middleware(
|
20 |
-
CORSMiddleware,
|
21 |
-
allow_origins=["*"], # Adjust as needed for security
|
22 |
-
allow_credentials=True,
|
23 |
-
allow_methods=["*"],
|
24 |
-
allow_headers=["*"],
|
25 |
-
)
|
26 |
-
|
27 |
-
# Include routes
|
28 |
-
app.include_router(router)
|
29 |
-
|
30 |
-
# Global exception handler for better error reporting
|
31 |
-
@app.exception_handler(Exception)
|
32 |
-
async def global_exception_handler(request: Request, exc: Exception):
|
33 |
-
logger.error(f"An error occurred: {str(exc)}")
|
34 |
-
return JSONResponse(
|
35 |
-
status_code=500,
|
36 |
-
content={"message": "An internal server error occurred."},
|
37 |
-
)
|
38 |
-
|
39 |
-
return app
|
40 |
-
|
41 |
-
app = create_app()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
api/auth.py
DELETED
@@ -1,10 +0,0 @@
|
|
1 |
-
from fastapi import Depends, HTTPException
|
2 |
-
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
3 |
-
from api.config import APP_SECRET
|
4 |
-
|
5 |
-
security = HTTPBearer()
|
6 |
-
|
7 |
-
def verify_app_secret(credentials: HTTPAuthorizationCredentials = Depends(security)):
|
8 |
-
if credentials.credentials != APP_SECRET:
|
9 |
-
raise HTTPException(status_code=403, detail="Invalid APP_SECRET")
|
10 |
-
return credentials.credentials
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
api/config.py
DELETED
@@ -1,226 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
from dotenv import load_dotenv
|
3 |
-
|
4 |
-
load_dotenv()
|
5 |
-
|
6 |
-
BASE_URL = "https://www.blackbox.ai"
|
7 |
-
headers = {
|
8 |
-
'accept': '*/*',
|
9 |
-
'accept-language': 'zh-CN,zh;q=0.9',
|
10 |
-
'content-type': 'application/json',
|
11 |
-
'origin': 'https://www.blackbox.ai',
|
12 |
-
'priority': 'u=1, i',
|
13 |
-
'sec-ch-ua': '"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"',
|
14 |
-
'sec-ch-ua-mobile': '?0',
|
15 |
-
'sec-ch-ua-platform': '"Windows"',
|
16 |
-
'sec-fetch-dest': 'empty',
|
17 |
-
'sec-fetch-mode': 'cors',
|
18 |
-
'sec-fetch-site': 'same-origin',
|
19 |
-
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36',
|
20 |
-
}
|
21 |
-
APP_SECRET = os.getenv("APP_SECRET")
|
22 |
-
|
23 |
-
ALLOWED_MODELS = [
|
24 |
-
{"id": "blackboxai", "name": "blackboxai"},
|
25 |
-
{"id": "blackboxai-pro", "name": "blackboxai-pro"},
|
26 |
-
{"id": "flux", "name": "flux"},
|
27 |
-
{"id": "llama-3.1-8b", "name": "llama-3.1-8b"},
|
28 |
-
{"id": "llama-3.1-70b", "name": "llama-3.1-70b"},
|
29 |
-
{"id": "llama-3.1-405b", "name": "llama-3.1-405"},
|
30 |
-
{"id": "gpt-4o", "name": "gpt-4o"},
|
31 |
-
{"id": "gemini-pro", "name": "gemini-pro"},
|
32 |
-
{"id": "gemini-1.5-flash", "name": "gemini-1.5-flash"},
|
33 |
-
{"id": "claude-sonnet-3.5", "name": "claude-sonnet-3.5"},
|
34 |
-
{"id": "PythonAgent", "name": "PythonAgent"},
|
35 |
-
{"id": "JavaAgent", "name": "JavaAgent"},
|
36 |
-
{"id": "JavaScriptAgent", "name": "JavaScriptAgent"},
|
37 |
-
{"id": "HTMLAgent", "name": "HTMLAgent"},
|
38 |
-
{"id": "GoogleCloudAgent", "name": "GoogleCloudAgent"},
|
39 |
-
{"id": "AndroidDeveloper", "name": "AndroidDeveloper"},
|
40 |
-
{"id": "SwiftDeveloper", "name": "SwiftDeveloper"},
|
41 |
-
{"id": "Next.jsAgent", "name": "Next.jsAgent"},
|
42 |
-
{"id": "MongoDBAgent", "name": "MongoDBAgent"},
|
43 |
-
{"id": "PyTorchAgent", "name": "PyTorchAgent"},
|
44 |
-
{"id": "ReactAgent", "name": "ReactAgent"},
|
45 |
-
{"id": "XcodeAgent", "name": "XcodeAgent"},
|
46 |
-
{"id": "AngularJSAgent", "name": "AngularJSAgent"},
|
47 |
-
{"id": "HerokuAgent", "name": "HerokuAgent"},
|
48 |
-
{"id": "GodotAgent", "name": "GodotAgent"},
|
49 |
-
{"id": "GoAgent", "name": "GoAgent"},
|
50 |
-
{"id": "GitlabAgent", "name": "GitlabAgent"},
|
51 |
-
{"id": "GitAgent", "name": "GitAgent"},
|
52 |
-
{"id": "RepoMap", "name": "RepoMap"},
|
53 |
-
{"id": "gemini-1.5-pro-latest", "name": "gemini-pro"},
|
54 |
-
{"id": "gemini-1.5-pro", "name": "gemini-1.5-pro"},
|
55 |
-
{"id": "claude-3-5-sonnet-20240620", "name": "claude-sonnet-3.5"},
|
56 |
-
{"id": "claude-3-5-sonnet", "name": "claude-sonnet-3.5"},
|
57 |
-
{"id": "Niansuh", "name": "Niansuh"},
|
58 |
-
{"id": "o1-preview", "name": "o1-preview"},
|
59 |
-
{"id": "claude-3-5-sonnet-20241022", "name": "claude-3-5-sonnet-20241022"},
|
60 |
-
{"id": "claude-3-5-sonnet-x", "name": "claude-3-5-sonnet-x"},
|
61 |
-
|
62 |
-
# Added New Agents
|
63 |
-
{"id": "FlaskAgent", "name": "FlaskAgent"},
|
64 |
-
{"id": "FirebaseAgent", "name": "FirebaseAgent"},
|
65 |
-
{"id": "FastAPIAgent", "name": "FastAPIAgent"},
|
66 |
-
{"id": "ErlangAgent", "name": "ErlangAgent"},
|
67 |
-
{"id": "ElectronAgent", "name": "ElectronAgent"},
|
68 |
-
{"id": "DockerAgent", "name": "DockerAgent"},
|
69 |
-
{"id": "DigitalOceanAgent", "name": "DigitalOceanAgent"},
|
70 |
-
{"id": "BitbucketAgent", "name": "BitbucketAgent"},
|
71 |
-
{"id": "AzureAgent", "name": "AzureAgent"},
|
72 |
-
{"id": "FlutterAgent", "name": "FlutterAgent"},
|
73 |
-
{"id": "YoutubeAgent", "name": "YoutubeAgent"},
|
74 |
-
{"id": "builderAgent", "name": "builderAgent"},
|
75 |
-
]
|
76 |
-
|
77 |
-
MODEL_MAPPING = {
|
78 |
-
"blackboxai": "blackboxai",
|
79 |
-
"blackboxai-pro": "blackboxai-pro",
|
80 |
-
"flux": "flux",
|
81 |
-
"ImageGeneration": "flux",
|
82 |
-
"llama-3.1-8b": "llama-3.1-8b",
|
83 |
-
"llama-3.1-70b": "llama-3.1-70b",
|
84 |
-
"llama-3.1-405b": "llama-3.1-405",
|
85 |
-
"gpt-4o": "gpt-4o",
|
86 |
-
"gemini-pro": "gemini-pro",
|
87 |
-
"gemini-1.5-flash": "gemini-1.5-flash",
|
88 |
-
"claude-sonnet-3.5": "claude-sonnet-3.5",
|
89 |
-
"PythonAgent": "PythonAgent",
|
90 |
-
"JavaAgent": "JavaAgent",
|
91 |
-
"JavaScriptAgent": "JavaScriptAgent",
|
92 |
-
"HTMLAgent": "HTMLAgent",
|
93 |
-
"GoogleCloudAgent": "GoogleCloudAgent",
|
94 |
-
"AndroidDeveloper": "AndroidDeveloper",
|
95 |
-
"SwiftDeveloper": "SwiftDeveloper",
|
96 |
-
"Next.jsAgent": "Next.jsAgent",
|
97 |
-
"MongoDBAgent": "MongoDBAgent",
|
98 |
-
"PyTorchAgent": "PyTorchAgent",
|
99 |
-
"ReactAgent": "ReactAgent",
|
100 |
-
"XcodeAgent": "XcodeAgent",
|
101 |
-
"AngularJSAgent": "AngularJSAgent",
|
102 |
-
"HerokuAgent": "HerokuAgent",
|
103 |
-
"GodotAgent": "GodotAgent",
|
104 |
-
"GoAgent": "GoAgent",
|
105 |
-
"GitlabAgent": "GitlabAgent",
|
106 |
-
"GitAgent": "GitAgent",
|
107 |
-
"RepoMap": "RepoMap",
|
108 |
-
# Additional mappings
|
109 |
-
"gemini-flash": "gemini-1.5-flash",
|
110 |
-
"claude-3.5-sonnet": "claude-sonnet-3.5",
|
111 |
-
"flux": "flux",
|
112 |
-
"gemini-1.5-pro-latest": "gemini-pro",
|
113 |
-
"gemini-1.5-pro": "gemini-1.5-pro",
|
114 |
-
"claude-3-5-sonnet-20240620": "claude-sonnet-3.5",
|
115 |
-
"claude-3-5-sonnet": "claude-sonnet-3.5",
|
116 |
-
"Niansuh": "Niansuh",
|
117 |
-
"o1-preview": "o1-preview",
|
118 |
-
"claude-3-5-sonnet-20241022": "claude-3-5-sonnet-20241022",
|
119 |
-
"claude-3-5-sonnet-x": "claude-3-5-sonnet-x",
|
120 |
-
|
121 |
-
# Added New Agents
|
122 |
-
"FlaskAgent": "FlaskAgent",
|
123 |
-
"FirebaseAgent": "FirebaseAgent",
|
124 |
-
"FastAPIAgent": "FastAPIAgent",
|
125 |
-
"ErlangAgent": "ErlangAgent",
|
126 |
-
"ElectronAgent": "ElectronAgent",
|
127 |
-
"DockerAgent": "DockerAgent",
|
128 |
-
"DigitalOceanAgent": "DigitalOceanAgent",
|
129 |
-
"BitbucketAgent": "BitbucketAgent",
|
130 |
-
"AzureAgent": "AzureAgent",
|
131 |
-
"FlutterAgent": "FlutterAgent",
|
132 |
-
"YoutubeAgent": "YoutubeAgent",
|
133 |
-
"builderAgent": "builderAgent",
|
134 |
-
}
|
135 |
-
|
136 |
-
# Agent modes
|
137 |
-
AGENT_MODE = {
|
138 |
-
'flux': {'mode': True, 'id': "ImageGenerationLV45LJp", 'name': "flux"},
|
139 |
-
'Niansuh': {'mode': True, 'id': "NiansuhAIk1HgESy", 'name': "Niansuh"},
|
140 |
-
'o1-preview': {'mode': True, 'id': "o1Dst8La8", 'name': "o1-preview"},
|
141 |
-
'claude-3-5-sonnet-20241022': {'mode': True, 'id': "Claude-Sonnet-3.5zO2HZSF", 'name': "claude-3-5-sonnet-20241022"},
|
142 |
-
'claude-3-5-sonnet-x': {'mode': True, 'id': "Claude-Sonnet-3.52022JE0UdQ3", 'name': "claude-3-5-sonnet-x"},
|
143 |
-
}
|
144 |
-
|
145 |
-
TRENDING_AGENT_MODE = {
|
146 |
-
"blackboxai": {},
|
147 |
-
"gemini-1.5-flash": {'mode': True, 'id': 'Gemini'},
|
148 |
-
"llama-3.1-8b": {'mode': True, 'id': "llama-3.1-8b"},
|
149 |
-
'llama-3.1-70b': {'mode': True, 'id': "llama-3.1-70b"},
|
150 |
-
'llama-3.1-405b': {'mode': True, 'id': "llama-3.1-405"},
|
151 |
-
'blackboxai-pro': {'mode': True, 'id': "BLACKBOXAI-PRO"},
|
152 |
-
'PythonAgent': {'mode': True, 'id': "Python Agent"},
|
153 |
-
'JavaAgent': {'mode': True, 'id': "Java Agent"},
|
154 |
-
'JavaScriptAgent': {'mode': True, 'id': "JavaScript Agent"},
|
155 |
-
'HTMLAgent': {'mode': True, 'id': "HTML Agent"},
|
156 |
-
'GoogleCloudAgent': {'mode': True, 'id': "Google Cloud Agent"},
|
157 |
-
'AndroidDeveloper': {'mode': True, 'id': "Android Developer"},
|
158 |
-
'SwiftDeveloper': {'mode': True, 'id': "Swift Developer"},
|
159 |
-
'Next.jsAgent': {'mode': True, 'id': "Next.js Agent"},
|
160 |
-
'MongoDBAgent': {'mode': True, 'id': "MongoDB Agent"},
|
161 |
-
'PyTorchAgent': {'mode': True, 'id': "PyTorch Agent"},
|
162 |
-
'ReactAgent': {'mode': True, 'id': "React Agent"},
|
163 |
-
'XcodeAgent': {'mode': True, 'id': "Xcode Agent"},
|
164 |
-
'AngularJSAgent': {'mode': True, 'id': "AngularJS Agent"},
|
165 |
-
'HerokuAgent': {'mode': True, 'id': "HerokuAgent"},
|
166 |
-
'GodotAgent': {'mode': True, 'id': "GodotAgent"},
|
167 |
-
'GoAgent': {'mode': True, 'id': "GoAgent"},
|
168 |
-
'GitlabAgent': {'mode': True, 'id': "GitlabAgent"},
|
169 |
-
'GitAgent': {'mode': True, 'id': "GitAgent"},
|
170 |
-
'RepoMap': {'mode': True, 'id': "repomap"},
|
171 |
-
|
172 |
-
# Added New Agents
|
173 |
-
'FlaskAgent': {'mode': True, 'id': "FlaskAgent"},
|
174 |
-
'FirebaseAgent': {'mode': True, 'id': "FirebaseAgent"},
|
175 |
-
'FastAPIAgent': {'mode': True, 'id': "FastAPIAgent"},
|
176 |
-
'ErlangAgent': {'mode': True, 'id': "ErlangAgent"},
|
177 |
-
'ElectronAgent': {'mode': True, 'id': "ElectronAgent"},
|
178 |
-
'DockerAgent': {'mode': True, 'id': "DockerAgent"},
|
179 |
-
'DigitalOceanAgent': {'mode': True, 'id': "DigitalOceanAgent"},
|
180 |
-
'BitbucketAgent': {'mode': True, 'id': "BitbucketAgent"},
|
181 |
-
'AzureAgent': {'mode': True, 'id': "AzureAgent"},
|
182 |
-
'FlutterAgent': {'mode': True, 'id': "FlutterAgent"},
|
183 |
-
'YoutubeAgent': {'mode': True, 'id': "YoutubeAgent"},
|
184 |
-
'builderAgent': {'mode': True, 'id': "builderAgent"},
|
185 |
-
}
|
186 |
-
|
187 |
-
# Model prefixes
|
188 |
-
MODEL_PREFIXES = {
|
189 |
-
'gpt-4o': '@GPT-4o',
|
190 |
-
'gemini-pro': '@Gemini-PRO',
|
191 |
-
'PythonAgent': '@Python Agent',
|
192 |
-
'JavaAgent': '@Java Agent',
|
193 |
-
'JavaScriptAgent': '@JavaScript Agent',
|
194 |
-
'HTMLAgent': '@HTML Agent',
|
195 |
-
'GoogleCloudAgent': '@Google Cloud Agent',
|
196 |
-
'AndroidDeveloper': '@Android Developer',
|
197 |
-
'SwiftDeveloper': '@Swift Developer',
|
198 |
-
'Next.jsAgent': '@Next.js Agent',
|
199 |
-
'MongoDBAgent': '@MongoDB Agent',
|
200 |
-
'PyTorchAgent': '@PyTorch Agent',
|
201 |
-
'ReactAgent': '@React Agent',
|
202 |
-
'XcodeAgent': '@Xcode Agent',
|
203 |
-
'AngularJSAgent': '@AngularJS Agent',
|
204 |
-
'HerokuAgent': '@Heroku Agent',
|
205 |
-
'GodotAgent': '@Godot Agent',
|
206 |
-
'GoAgent': '@Go Agent',
|
207 |
-
'GitlabAgent': '@Gitlab Agent',
|
208 |
-
'GitAgent': '@Gitlab Agent',
|
209 |
-
'blackboxai-pro': '@BLACKBOXAI-PRO',
|
210 |
-
'flux': '@Image Generation',
|
211 |
-
# Add any additional prefixes if necessary
|
212 |
-
|
213 |
-
# Added New Agents
|
214 |
-
'FlaskAgent': '@Flask Agent',
|
215 |
-
'FirebaseAgent': '@Firebase Agent',
|
216 |
-
'FastAPIAgent': '@FastAPI Agent',
|
217 |
-
'ErlangAgent': '@Erlang Agent',
|
218 |
-
'ElectronAgent': '@Electron Agent',
|
219 |
-
'DockerAgent': '@Docker Agent',
|
220 |
-
'DigitalOceanAgent': '@DigitalOcean Agent',
|
221 |
-
'BitbucketAgent': '@Bitbucket Agent',
|
222 |
-
'AzureAgent': '@Azure Agent',
|
223 |
-
'FlutterAgent': '@Flutter Agent',
|
224 |
-
'YoutubeAgent': '@Youtube Agent',
|
225 |
-
'builderAgent': '@builder Agent',
|
226 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
api/logger.py
DELETED
@@ -1,20 +0,0 @@
|
|
1 |
-
import logging
|
2 |
-
|
3 |
-
def setup_logger(name):
|
4 |
-
logger = logging.getLogger(name)
|
5 |
-
if not logger.handlers:
|
6 |
-
logger.setLevel(logging.INFO) # Set to INFO to avoid DEBUG logs in production
|
7 |
-
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
8 |
-
|
9 |
-
# Console handler
|
10 |
-
console_handler = logging.StreamHandler()
|
11 |
-
console_handler.setFormatter(formatter)
|
12 |
-
logger.addHandler(console_handler)
|
13 |
-
|
14 |
-
# File handler for errors (optional)
|
15 |
-
# error_file_handler = logging.FileHandler('error.log')
|
16 |
-
# error_file_handler.setFormatter(formatter)
|
17 |
-
# error_file_handler.setLevel(logging.ERROR)
|
18 |
-
# logger.addHandler(error_file_handler)
|
19 |
-
|
20 |
-
return logger
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
api/models.py
DELETED
@@ -1,14 +0,0 @@
|
|
1 |
-
from typing import List, Optional
|
2 |
-
from pydantic import BaseModel
|
3 |
-
|
4 |
-
class Message(BaseModel):
|
5 |
-
role: str
|
6 |
-
content: str | list
|
7 |
-
|
8 |
-
class ChatRequest(BaseModel):
|
9 |
-
model: str
|
10 |
-
messages: List[Message]
|
11 |
-
stream: Optional[bool] = False
|
12 |
-
temperature: Optional[float] = 0.5
|
13 |
-
top_p: Optional[float] = 0.9
|
14 |
-
max_tokens: Optional[int] = 99999999
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
api/routes.py
DELETED
@@ -1,62 +0,0 @@
|
|
1 |
-
import json
|
2 |
-
from fastapi import APIRouter, Depends, HTTPException, Request, Response
|
3 |
-
from fastapi.responses import StreamingResponse
|
4 |
-
from api.auth import verify_app_secret
|
5 |
-
from api.config import ALLOWED_MODELS
|
6 |
-
from api.models import ChatRequest
|
7 |
-
from api.utils import process_non_streaming_response, process_streaming_response
|
8 |
-
from api.logger import setup_logger
|
9 |
-
|
10 |
-
logger = setup_logger(__name__)
|
11 |
-
|
12 |
-
router = APIRouter()
|
13 |
-
|
14 |
-
@router.options("/v1/chat/completions")
|
15 |
-
@router.options("/api/v1/chat/completions")
|
16 |
-
async def chat_completions_options():
|
17 |
-
return Response(
|
18 |
-
status_code=200,
|
19 |
-
headers={
|
20 |
-
"Access-Control-Allow-Origin": "*",
|
21 |
-
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
22 |
-
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
23 |
-
},
|
24 |
-
)
|
25 |
-
|
26 |
-
@router.get("/v1/models")
|
27 |
-
@router.get("/api/v1/models")
|
28 |
-
async def list_models():
|
29 |
-
return {"object": "list", "data": ALLOWED_MODELS}
|
30 |
-
|
31 |
-
@router.post("/v1/chat/completions")
|
32 |
-
@router.post("/api/v1/chat/completions")
|
33 |
-
async def chat_completions(
|
34 |
-
request: ChatRequest, app_secret: str = Depends(verify_app_secret)
|
35 |
-
):
|
36 |
-
logger.info("Entering chat_completions route")
|
37 |
-
logger.info(f"Received request: {request}")
|
38 |
-
logger.info(f"App secret: {app_secret}")
|
39 |
-
logger.info(f"Received chat completion request for model: {request.model}")
|
40 |
-
|
41 |
-
if request.model not in [model["id"] for model in ALLOWED_MODELS]:
|
42 |
-
raise HTTPException(
|
43 |
-
status_code=400,
|
44 |
-
detail=f"Model {request.model} is not allowed. Allowed models are: {', '.join(model['id'] for model in ALLOWED_MODELS)}",
|
45 |
-
)
|
46 |
-
|
47 |
-
if request.stream:
|
48 |
-
logger.info("Streaming response")
|
49 |
-
return StreamingResponse(process_streaming_response(request), media_type="text/event-stream")
|
50 |
-
else:
|
51 |
-
logger.info("Non-streaming response")
|
52 |
-
return await process_non_streaming_response(request)
|
53 |
-
|
54 |
-
|
55 |
-
@router.route('/')
|
56 |
-
@router.route('/healthz')
|
57 |
-
@router.route('/ready')
|
58 |
-
@router.route('/alive')
|
59 |
-
@router.route('/status')
|
60 |
-
@router.get("/health")
|
61 |
-
def health_check(request: Request):
|
62 |
-
return Response(content=json.dumps({"status": "ok"}), media_type="application/json")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
api/utils.py
DELETED
@@ -1,252 +0,0 @@
|
|
1 |
-
from datetime import datetime
|
2 |
-
from http.client import HTTPException
|
3 |
-
import json
|
4 |
-
from typing import Any, Dict, Optional
|
5 |
-
import uuid
|
6 |
-
|
7 |
-
import httpx
|
8 |
-
from api import validate
|
9 |
-
from api.config import MODEL_MAPPING, headers, AGENT_MODE, TRENDING_AGENT_MODE
|
10 |
-
from fastapi import Depends, HTTPException as FastAPIHTTPException
|
11 |
-
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
12 |
-
|
13 |
-
from api.config import APP_SECRET, BASE_URL
|
14 |
-
from api.models import ChatRequest
|
15 |
-
|
16 |
-
from api.logger import setup_logger
|
17 |
-
|
18 |
-
logger = setup_logger(__name__)
|
19 |
-
|
20 |
-
# Initialize HTTPBearer for security dependency
|
21 |
-
security = HTTPBearer()
|
22 |
-
|
23 |
-
def create_chat_completion_data(
|
24 |
-
content: str, model: str, timestamp: int, finish_reason: Optional[str] = None
|
25 |
-
) -> Dict[str, Any]:
|
26 |
-
"""
|
27 |
-
Create a dictionary representing a chat completion chunk.
|
28 |
-
|
29 |
-
Args:
|
30 |
-
content (str): The content of the message.
|
31 |
-
model (str): The model used for the chat.
|
32 |
-
timestamp (int): The timestamp of the creation.
|
33 |
-
finish_reason (Optional[str], optional): The reason for finishing. Defaults to None.
|
34 |
-
|
35 |
-
Returns:
|
36 |
-
Dict[str, Any]: A dictionary representing the chat completion chunk.
|
37 |
-
"""
|
38 |
-
return {
|
39 |
-
"id": f"chatcmpl-{uuid.uuid4()}",
|
40 |
-
"object": "chat.completion.chunk",
|
41 |
-
"created": timestamp,
|
42 |
-
"model": model,
|
43 |
-
"choices": [
|
44 |
-
{
|
45 |
-
"index": 0,
|
46 |
-
"delta": {"content": content, "role": "assistant"},
|
47 |
-
"finish_reason": finish_reason,
|
48 |
-
}
|
49 |
-
],
|
50 |
-
"usage": None,
|
51 |
-
}
|
52 |
-
|
53 |
-
def verify_app_secret(credentials: HTTPAuthorizationCredentials = Depends(security)):
|
54 |
-
"""
|
55 |
-
Verify the application secret from the HTTP authorization credentials.
|
56 |
-
|
57 |
-
Args:
|
58 |
-
credentials (HTTPAuthorizationCredentials, optional): The HTTP authorization credentials. Defaults to Depends(security).
|
59 |
-
|
60 |
-
Raises:
|
61 |
-
HTTPException: If the APP_SECRET does not match.
|
62 |
-
|
63 |
-
Returns:
|
64 |
-
str: The verified credentials.
|
65 |
-
"""
|
66 |
-
if credentials.credentials != APP_SECRET:
|
67 |
-
raise FastAPIHTTPException(status_code=403, detail="Invalid APP_SECRET")
|
68 |
-
return credentials.credentials
|
69 |
-
|
70 |
-
def message_to_dict(message):
|
71 |
-
"""
|
72 |
-
Convert a message object to a dictionary.
|
73 |
-
|
74 |
-
Args:
|
75 |
-
message: The message object to convert.
|
76 |
-
|
77 |
-
Returns:
|
78 |
-
Dict[str, Any]: The dictionary representation of the message.
|
79 |
-
"""
|
80 |
-
if isinstance(message.content, str):
|
81 |
-
return {"role": message.role, "content": message.content}
|
82 |
-
elif isinstance(message.content, list) and len(message.content) == 2:
|
83 |
-
return {
|
84 |
-
"role": message.role,
|
85 |
-
"content": message.content[0]["text"],
|
86 |
-
"data": {
|
87 |
-
"imageBase64": message.content[1]["image_url"]["url"],
|
88 |
-
"fileText": "",
|
89 |
-
"title": "snapshot",
|
90 |
-
},
|
91 |
-
}
|
92 |
-
else:
|
93 |
-
return {"role": message.role, "content": message.content}
|
94 |
-
|
95 |
-
async def process_streaming_response(request: ChatRequest):
|
96 |
-
"""
|
97 |
-
Process a streaming response from the chat API.
|
98 |
-
|
99 |
-
Args:
|
100 |
-
request (ChatRequest): The chat request containing all necessary information.
|
101 |
-
|
102 |
-
Yields:
|
103 |
-
str: The streaming data chunks formatted as server-sent events.
|
104 |
-
"""
|
105 |
-
agent_mode = AGENT_MODE.get(request.model, {})
|
106 |
-
trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
|
107 |
-
|
108 |
-
# Log reduced information
|
109 |
-
logger.info(
|
110 |
-
f"Streaming request for model: '{request.model}', "
|
111 |
-
f"agent mode: {agent_mode}, trending agent mode: {trending_agent_mode}"
|
112 |
-
)
|
113 |
-
|
114 |
-
json_data = {
|
115 |
-
"messages": [message_to_dict(msg) for msg in request.messages],
|
116 |
-
"previewToken": None,
|
117 |
-
"userId": None,
|
118 |
-
"codeModelMode": True,
|
119 |
-
"agentMode": agent_mode,
|
120 |
-
"trendingAgentMode": trending_agent_mode,
|
121 |
-
"isMicMode": False,
|
122 |
-
"userSystemPrompt": None,
|
123 |
-
"maxTokens": request.max_tokens,
|
124 |
-
"playgroundTopP": request.top_p,
|
125 |
-
"playgroundTemperature": request.temperature,
|
126 |
-
"isChromeExt": False,
|
127 |
-
"githubToken": None,
|
128 |
-
"clickedAnswer2": False,
|
129 |
-
"clickedAnswer3": False,
|
130 |
-
"clickedForceWebSearch": False,
|
131 |
-
"visitFromDelta": False,
|
132 |
-
"mobileClient": False,
|
133 |
-
"userSelectedModel": MODEL_MAPPING.get(request.model),
|
134 |
-
"validated": validate.getHid()
|
135 |
-
}
|
136 |
-
|
137 |
-
async with httpx.AsyncClient() as client:
|
138 |
-
try:
|
139 |
-
async with client.stream(
|
140 |
-
"POST",
|
141 |
-
f"{BASE_URL}/api/chat",
|
142 |
-
headers=headers,
|
143 |
-
json=json_data,
|
144 |
-
timeout=100,
|
145 |
-
) as response:
|
146 |
-
response.raise_for_status()
|
147 |
-
timestamp = int(datetime.now().timestamp())
|
148 |
-
async for line in response.aiter_lines():
|
149 |
-
if line:
|
150 |
-
content = line + "\n"
|
151 |
-
if "https://www.blackbox.ai" in content:
|
152 |
-
validate.getHid(True)
|
153 |
-
content = "Hid has been refreshed; feel free to restart the conversation.\n"
|
154 |
-
yield f"data: {json.dumps(create_chat_completion_data(content, request.model, timestamp))}\n\n"
|
155 |
-
break
|
156 |
-
# Remove the specific pattern without affecting markdown
|
157 |
-
content = content.replace("$@$v=undefined-rv1$@$", "")
|
158 |
-
yield f"data: {json.dumps(create_chat_completion_data(content, request.model, timestamp))}\n\n"
|
159 |
-
|
160 |
-
# Indicate the end of the stream
|
161 |
-
yield f"data: {json.dumps(create_chat_completion_data('', request.model, timestamp, 'stop'))}\n\n"
|
162 |
-
yield "data: [DONE]\n\n"
|
163 |
-
except httpx.HTTPStatusError as e:
|
164 |
-
logger.error(f"HTTP error occurred: {e}")
|
165 |
-
raise FastAPIHTTPException(status_code=e.response.status_code, detail=str(e))
|
166 |
-
except httpx.RequestError as e:
|
167 |
-
logger.error(f"Error occurred during request: {e}")
|
168 |
-
raise FastAPIHTTPException(status_code=500, detail=str(e))
|
169 |
-
|
170 |
-
async def process_non_streaming_response(request: ChatRequest):
|
171 |
-
"""
|
172 |
-
Process a non-streaming response from the chat API.
|
173 |
-
|
174 |
-
Args:
|
175 |
-
request (ChatRequest): The chat request containing all necessary information.
|
176 |
-
|
177 |
-
Returns:
|
178 |
-
Dict[str, Any]: The full response from the chat API formatted appropriately.
|
179 |
-
"""
|
180 |
-
agent_mode = AGENT_MODE.get(request.model, {})
|
181 |
-
trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
|
182 |
-
|
183 |
-
# Log reduced information
|
184 |
-
logger.info(
|
185 |
-
f"Non-streaming request for model: '{request.model}', "
|
186 |
-
f"agent mode: {agent_mode}, trending agent mode: {trending_agent_mode}"
|
187 |
-
)
|
188 |
-
|
189 |
-
json_data = {
|
190 |
-
"messages": [message_to_dict(msg) for msg in request.messages],
|
191 |
-
"previewToken": None,
|
192 |
-
"userId": None,
|
193 |
-
"codeModelMode": True,
|
194 |
-
"agentMode": agent_mode,
|
195 |
-
"trendingAgentMode": trending_agent_mode,
|
196 |
-
"isMicMode": False,
|
197 |
-
"userSystemPrompt": None,
|
198 |
-
"maxTokens": request.max_tokens,
|
199 |
-
"playgroundTopP": request.top_p,
|
200 |
-
"playgroundTemperature": request.temperature,
|
201 |
-
"isChromeExt": False,
|
202 |
-
"githubToken": None,
|
203 |
-
"clickedAnswer2": False,
|
204 |
-
"clickedAnswer3": False,
|
205 |
-
"clickedForceWebSearch": False,
|
206 |
-
"visitFromDelta": False,
|
207 |
-
"mobileClient": False,
|
208 |
-
"userSelectedModel": MODEL_MAPPING.get(request.model),
|
209 |
-
"validated": validate.getHid()
|
210 |
-
}
|
211 |
-
|
212 |
-
full_response = ""
|
213 |
-
async with httpx.AsyncClient() as client:
|
214 |
-
try:
|
215 |
-
async with client.stream(
|
216 |
-
method="POST",
|
217 |
-
url=f"{BASE_URL}/api/chat",
|
218 |
-
headers=headers,
|
219 |
-
json=json_data,
|
220 |
-
timeout=100,
|
221 |
-
) as response:
|
222 |
-
response.raise_for_status()
|
223 |
-
async for chunk in response.aiter_text():
|
224 |
-
full_response += chunk
|
225 |
-
|
226 |
-
if "https://www.blackbox.ai" in full_response:
|
227 |
-
validate.getHid(True)
|
228 |
-
full_response = "Hid has been refreshed; feel free to restart the conversation."
|
229 |
-
|
230 |
-
# Remove the specific pattern without affecting markdown
|
231 |
-
full_response = full_response.replace("$@$v=undefined-rv1$@$", "")
|
232 |
-
|
233 |
-
return {
|
234 |
-
"id": f"chatcmpl-{uuid.uuid4()}",
|
235 |
-
"object": "chat.completion",
|
236 |
-
"created": int(datetime.now().timestamp()),
|
237 |
-
"model": request.model,
|
238 |
-
"choices": [
|
239 |
-
{
|
240 |
-
"index": 0,
|
241 |
-
"message": {"role": "assistant", "content": full_response},
|
242 |
-
"finish_reason": "stop",
|
243 |
-
}
|
244 |
-
],
|
245 |
-
"usage": None,
|
246 |
-
}
|
247 |
-
except httpx.HTTPStatusError as e:
|
248 |
-
logger.error(f"HTTP error occurred: {e}")
|
249 |
-
raise FastAPIHTTPException(status_code=e.response.status_code, detail=str(e))
|
250 |
-
except httpx.RequestError as e:
|
251 |
-
logger.error(f"Error occurred during request: {e}")
|
252 |
-
raise FastAPIHTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
api/validate.py
DELETED
@@ -1,62 +0,0 @@
|
|
1 |
-
import requests
|
2 |
-
import re
|
3 |
-
import time
|
4 |
-
|
5 |
-
base_url = "https://www.blackbox.ai"
|
6 |
-
headers = {
|
7 |
-
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
8 |
-
}
|
9 |
-
|
10 |
-
# Cache variables
|
11 |
-
cached_hid = None
|
12 |
-
cache_time = 0
|
13 |
-
CACHE_DURATION = 36000 # Cache duration in seconds (10 hour)
|
14 |
-
|
15 |
-
def getHid(force_refresh=False):
|
16 |
-
global cached_hid, cache_time
|
17 |
-
current_time = time.time()
|
18 |
-
|
19 |
-
# Check if a forced refresh is needed or if the cached values are still valid.
|
20 |
-
if not force_refresh and cached_hid and (current_time - cache_time) < CACHE_DURATION:
|
21 |
-
print("using cached_hid:", cached_hid)
|
22 |
-
return cached_hid
|
23 |
-
|
24 |
-
try:
|
25 |
-
# Retrieve the initial HTML content.
|
26 |
-
response = requests.get(base_url, headers=headers)
|
27 |
-
response.raise_for_status()
|
28 |
-
content = response.text
|
29 |
-
|
30 |
-
# Use a regular expression to find specific `static/chunks` paths.
|
31 |
-
pattern = r"static/chunks/app/layout-[a-zA-Z0-9]+\.js"
|
32 |
-
match = re.search(pattern, content)
|
33 |
-
|
34 |
-
if match:
|
35 |
-
# Construct the full URL of the JS file.
|
36 |
-
js_path = match.group()
|
37 |
-
full_url = f"{base_url}/_next/{js_path}"
|
38 |
-
|
39 |
-
# Request the content of the JS file.
|
40 |
-
js_response = requests.get(full_url, headers=headers)
|
41 |
-
js_response.raise_for_status()
|
42 |
-
|
43 |
-
# In JS content, use a regular expression to search for h-value.
|
44 |
-
h_pattern = r'h="([0-9a-f-]+)"'
|
45 |
-
h_match = re.search(h_pattern, js_response.text)
|
46 |
-
|
47 |
-
if h_match:
|
48 |
-
h_value = h_match.group(1)
|
49 |
-
print("Found the h-value.", h_value)
|
50 |
-
# Update the cache.
|
51 |
-
cached_hid = h_value
|
52 |
-
cache_time = current_time
|
53 |
-
return h_value
|
54 |
-
else:
|
55 |
-
print("The h-value was not found in the JS content.")
|
56 |
-
return None
|
57 |
-
else:
|
58 |
-
print("The specified JS file path was not found in the HTML content.")
|
59 |
-
return None
|
60 |
-
except requests.exceptions.RequestException as e:
|
61 |
-
print(f"An error occurred. {e}")
|
62 |
-
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|