Niansuh commited on
Commit
c0e5311
·
verified ·
1 Parent(s): b0790f1

Update api/utils.py

Browse files
Files changed (1) hide show
  1. api/utils.py +362 -103
api/utils.py CHANGED
@@ -1,36 +1,304 @@
1
- # api/utils.py
2
-
3
  from datetime import datetime
4
  import json
5
  import uuid
6
  import asyncio
7
  import random
 
 
 
8
  from typing import Any, Dict, Optional
9
 
 
 
10
  import httpx
11
  from fastapi import HTTPException
12
- from api.config import (
13
- MODEL_MAPPING,
14
- BASE_URL,
15
- AGENT_MODE,
16
- TRENDING_AGENT_MODE,
17
- MODEL_PREFIXES,
18
- api_headers,
19
- )
20
  from api.models import ChatRequest
21
  from api.logger import setup_logger
22
 
23
- # Import the validate module
24
- from api import validate
25
-
26
  logger = setup_logger(__name__)
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  # Helper function to create chat completion data
29
  def create_chat_completion_data(
30
- content: str,
31
- model: str,
32
- timestamp: int,
33
- finish_reason: Optional[str] = None
34
  ) -> Dict[str, Any]:
35
  return {
36
  "id": f"chatcmpl-{uuid.uuid4()}",
@@ -40,7 +308,7 @@ def create_chat_completion_data(
40
  "choices": [
41
  {
42
  "index": 0,
43
- "delta": {"content": content},
44
  "finish_reason": finish_reason,
45
  }
46
  ],
@@ -49,18 +317,16 @@ def create_chat_completion_data(
49
 
50
  # Function to convert message to dictionary format, ensuring base64 data and optional model prefix
51
  def message_to_dict(message, model_prefix: Optional[str] = None):
52
- content = (
53
- message.content
54
- if isinstance(message.content, str)
55
- else message.content[0]["text"]
56
- )
 
 
57
  if model_prefix:
58
  content = f"{model_prefix} {content}"
59
- if (
60
- isinstance(message.content, list)
61
- and len(message.content) == 2
62
- and "image_url" in message.content[1]
63
- ):
64
  # Ensure base64 images are always included for all models
65
  return {
66
  "role": message.role,
@@ -83,42 +349,44 @@ def strip_model_prefix(content: str, model_prefix: Optional[str] = None) -> str:
83
 
84
  # Process streaming response
85
  async def process_streaming_response(request: ChatRequest):
86
- logger.info(f"Model: {request.model}")
 
 
87
 
88
  agent_mode = AGENT_MODE.get(request.model, {})
89
  trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
90
  model_prefix = MODEL_PREFIXES.get(request.model, "")
91
 
 
 
 
92
  if request.model == 'o1-preview':
93
  delay_seconds = random.randint(1, 60)
94
- logger.info(
95
- f"Introducing a delay of {delay_seconds} seconds for model 'o1-preview'"
96
- )
97
  await asyncio.sleep(delay_seconds)
98
 
99
  json_data = {
100
- "messages": [
101
- message_to_dict(msg, model_prefix=model_prefix) for msg in request.messages
102
- ],
103
- "previewToken": None,
104
- "userId": None,
105
- "codeModelMode": True,
106
  "agentMode": agent_mode,
107
- "trendingAgentMode": trending_agent_mode,
108
- "isMicMode": False,
109
- "userSystemPrompt": None,
110
- "maxTokens": request.max_tokens,
111
- "playgroundTopP": request.top_p,
112
- "playgroundTemperature": request.temperature,
113
- "isChromeExt": False,
114
- "githubToken": None,
115
  "clickedAnswer2": False,
116
  "clickedAnswer3": False,
117
  "clickedForceWebSearch": False,
118
- "visitFromDelta": False,
 
 
 
 
 
 
119
  "mobileClient": False,
 
 
 
 
 
120
  "userSelectedModel": MODEL_MAPPING.get(request.model, request.model),
121
- "validated": validate.getHid(),
 
 
122
  }
123
 
124
  async with httpx.AsyncClient() as client:
@@ -126,102 +394,93 @@ async def process_streaming_response(request: ChatRequest):
126
  async with client.stream(
127
  "POST",
128
  f"{BASE_URL}/api/chat",
129
- headers=api_headers,
130
  json=json_data,
131
  timeout=100,
132
  ) as response:
133
  response.raise_for_status()
134
  async for line in response.aiter_lines():
 
135
  if line:
136
- # Process the line as per the Blackbox API response format
137
- if "https://www.blackbox.ai" in line:
138
- # Refresh hid and inform the user
139
- validate.getHid(True)
140
- content = "hid已刷新,重新对话即可"
141
- yield (
142
- f"data: {json.dumps(create_chat_completion_data(content, request.model, int(datetime.now().timestamp())))}\n\n"
143
- )
144
  break
145
- if line.startswith("$@$v=undefined-rv1$@$"):
146
- line = line[21:]
147
- cleaned_content = strip_model_prefix(line, model_prefix)
148
- # Yield the data in the format expected by the client
149
- yield (
150
- f"data: {json.dumps(create_chat_completion_data(cleaned_content, request.model, int(datetime.now().timestamp())))}\n\n"
151
- )
152
-
153
- # Signal that the response is complete
154
- yield (
155
- f"data: {json.dumps(create_chat_completion_data('', request.model, int(datetime.now().timestamp()), 'stop'))}\n\n"
156
- )
157
  yield "data: [DONE]\n\n"
158
  except httpx.HTTPStatusError as e:
159
- logger.error(f"HTTP error occurred: {e}")
160
  raise HTTPException(status_code=e.response.status_code, detail=str(e))
161
  except httpx.RequestError as e:
162
- logger.error(f"Error occurred during request: {e}")
163
  raise HTTPException(status_code=500, detail=str(e))
164
 
165
  # Process non-streaming response
166
  async def process_non_streaming_response(request: ChatRequest):
167
- logger.info(f"Model: {request.model}")
 
 
168
 
169
  agent_mode = AGENT_MODE.get(request.model, {})
170
  trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
171
  model_prefix = MODEL_PREFIXES.get(request.model, "")
172
 
 
 
 
173
  if request.model == 'o1-preview':
174
  delay_seconds = random.randint(20, 60)
175
- logger.info(
176
- f"Introducing a delay of {delay_seconds} seconds for model 'o1-preview'"
177
- )
178
  await asyncio.sleep(delay_seconds)
179
 
180
  json_data = {
181
- "messages": [
182
- message_to_dict(msg, model_prefix=model_prefix) for msg in request.messages
183
- ],
184
- "previewToken": None,
185
- "userId": None,
186
- "codeModelMode": True,
187
  "agentMode": agent_mode,
188
- "trendingAgentMode": trending_agent_mode,
189
- "isMicMode": False,
190
- "userSystemPrompt": None,
191
- "maxTokens": request.max_tokens,
192
- "playgroundTopP": request.top_p,
193
- "playgroundTemperature": request.temperature,
194
- "isChromeExt": False,
195
- "githubToken": None,
196
  "clickedAnswer2": False,
197
  "clickedAnswer3": False,
198
  "clickedForceWebSearch": False,
199
- "visitFromDelta": False,
 
 
 
 
 
 
200
  "mobileClient": False,
 
 
 
 
 
201
  "userSelectedModel": MODEL_MAPPING.get(request.model, request.model),
202
- "validated": validate.getHid(),
 
 
203
  }
204
 
205
  full_response = ""
206
  async with httpx.AsyncClient() as client:
207
  try:
208
- response = await client.post(
209
- url=f"{BASE_URL}/api/chat",
210
- headers=api_headers,
211
- json=json_data,
212
- )
213
- response.raise_for_status()
214
- full_response = response.text
215
  except httpx.HTTPStatusError as e:
216
- logger.error(f"HTTP error occurred: {e}")
217
  raise HTTPException(status_code=e.response.status_code, detail=str(e))
218
  except httpx.RequestError as e:
219
- logger.error(f"Error occurred during request: {e}")
220
  raise HTTPException(status_code=500, detail=str(e))
221
-
222
  if "https://www.blackbox.ai" in full_response:
223
- # Refresh hid and inform the user
224
- validate.getHid(True)
225
  full_response = "hid已刷新,重新对话即可"
226
  if full_response.startswith("$@$v=undefined-rv1$@$"):
227
  full_response = full_response[21:]
 
 
 
1
  from datetime import datetime
2
  import json
3
  import uuid
4
  import asyncio
5
  import random
6
+ import string
7
+ import time
8
+ import re
9
  from typing import Any, Dict, Optional
10
 
11
+ import os
12
+ import requests
13
  import httpx
14
  from fastapi import HTTPException
 
 
 
 
 
 
 
 
15
  from api.models import ChatRequest
16
  from api.logger import setup_logger
17
 
 
 
 
18
  logger = setup_logger(__name__)
19
 
20
+ # Load environment variables
21
+ from dotenv import load_dotenv
22
+
23
+ load_dotenv()
24
+
25
+ # Base URL
26
+ BASE_URL = "https://www.blackbox.ai"
27
+
28
+ APP_SECRET = os.getenv("APP_SECRET")
29
+
30
+ # Allowed models (copy from your config.py)
31
+ ALLOWED_MODELS = [
32
+ {"id": "blackboxai", "name": "blackboxai"},
33
+ {"id": "blackboxai-pro", "name": "blackboxai-pro"},
34
+ {"id": "flux", "name": "flux"},
35
+ {"id": "llama-3.1-8b", "name": "llama-3.1-8b"},
36
+ {"id": "llama-3.1-70b", "name": "llama-3.1-70b"},
37
+ {"id": "llama-3.1-405b", "name": "llama-3.1-405"},
38
+ {"id": "gpt-4o", "name": "gpt-4o"},
39
+ {"id": "gemini-pro", "name": "gemini-pro"},
40
+ {"id": "gemini-1.5-flash", "name": "gemini-1.5-flash"},
41
+ {"id": "claude-sonnet-3.5", "name": "claude-sonnet-3.5"},
42
+ {"id": "PythonAgent", "name": "PythonAgent"},
43
+ {"id": "JavaAgent", "name": "JavaAgent"},
44
+ {"id": "JavaScriptAgent", "name": "JavaScriptAgent"},
45
+ {"id": "HTMLAgent", "name": "HTMLAgent"},
46
+ {"id": "GoogleCloudAgent", "name": "GoogleCloudAgent"},
47
+ {"id": "AndroidDeveloper", "name": "AndroidDeveloper"},
48
+ {"id": "SwiftDeveloper", "name": "SwiftDeveloper"},
49
+ {"id": "Next.jsAgent", "name": "Next.jsAgent"},
50
+ {"id": "MongoDBAgent", "name": "MongoDBAgent"},
51
+ {"id": "PyTorchAgent", "name": "PyTorchAgent"},
52
+ {"id": "ReactAgent", "name": "ReactAgent"},
53
+ {"id": "XcodeAgent", "name": "XcodeAgent"},
54
+ {"id": "AngularJSAgent", "name": "AngularJSAgent"},
55
+ {"id": "HerokuAgent", "name": "HerokuAgent"},
56
+ {"id": "GodotAgent", "name": "GodotAgent"},
57
+ {"id": "GoAgent", "name": "GoAgent"},
58
+ {"id": "GitlabAgent", "name": "GitlabAgent"},
59
+ {"id": "GitAgent", "name": "GitAgent"},
60
+ {"id": "RepoMap", "name": "RepoMap"},
61
+ {"id": "gemini-1.5-pro-latest", "name": "gemini-pro"},
62
+ {"id": "gemini-1.5-pro", "name": "gemini-1.5-pro"},
63
+ {"id": "claude-3-5-sonnet-20240620", "name": "claude-sonnet-3.5"},
64
+ {"id": "claude-3-5-sonnet", "name": "claude-sonnet-3.5"},
65
+ {"id": "Niansuh", "name": "Niansuh"},
66
+ {"id": "o1-preview", "name": "o1-preview"},
67
+ {"id": "claude-3-5-sonnet-20241022", "name": "claude-3-5-sonnet-20241022"},
68
+ {"id": "claude-3-5-sonnet-x", "name": "claude-3-5-sonnet-x"},
69
+ # Added New Agents
70
+ {"id": "FlaskAgent", "name": "FlaskAgent"},
71
+ {"id": "FirebaseAgent", "name": "FirebaseAgent"},
72
+ {"id": "FastAPIAgent", "name": "FastAPIAgent"},
73
+ {"id": "ErlangAgent", "name": "ErlangAgent"},
74
+ {"id": "ElectronAgent", "name": "ElectronAgent"},
75
+ {"id": "DockerAgent", "name": "DockerAgent"},
76
+ {"id": "DigitalOceanAgent", "name": "DigitalOceanAgent"},
77
+ {"id": "BitbucketAgent", "name": "BitbucketAgent"},
78
+ {"id": "AzureAgent", "name": "AzureAgent"},
79
+ {"id": "FlutterAgent", "name": "FlutterAgent"},
80
+ {"id": "YoutubeAgent", "name": "YoutubeAgent"},
81
+ {"id": "builderAgent", "name": "builderAgent"},
82
+ ]
83
+
84
+ # Model mapping
85
+ MODEL_MAPPING = {
86
+ "blackboxai": "blackboxai",
87
+ "blackboxai-pro": "blackboxai-pro",
88
+ "flux": "flux",
89
+ "ImageGeneration": "flux",
90
+ "llama-3.1-8b": "llama-3.1-8b",
91
+ "llama-3.1-70b": "llama-3.1-70b",
92
+ "llama-3.1-405b": "llama-3.1-405",
93
+ "gpt-4o": "gpt-4o",
94
+ "gemini-pro": "gemini-pro",
95
+ "gemini-1.5-flash": "gemini-1.5-flash",
96
+ "claude-sonnet-3.5": "claude-sonnet-3.5",
97
+ "PythonAgent": "PythonAgent",
98
+ "JavaAgent": "JavaAgent",
99
+ "JavaScriptAgent": "JavaScriptAgent",
100
+ "HTMLAgent": "HTMLAgent",
101
+ "GoogleCloudAgent": "GoogleCloudAgent",
102
+ "AndroidDeveloper": "AndroidDeveloper",
103
+ "SwiftDeveloper": "SwiftDeveloper",
104
+ "Next.jsAgent": "Next.jsAgent",
105
+ "MongoDBAgent": "MongoDBAgent",
106
+ "PyTorchAgent": "PyTorchAgent",
107
+ "ReactAgent": "ReactAgent",
108
+ "XcodeAgent": "XcodeAgent",
109
+ "AngularJSAgent": "AngularJSAgent",
110
+ "HerokuAgent": "HerokuAgent",
111
+ "GodotAgent": "GodotAgent",
112
+ "GoAgent": "GoAgent",
113
+ "GitlabAgent": "GitlabAgent",
114
+ "GitAgent": "GitAgent",
115
+ "RepoMap": "RepoMap",
116
+ # Additional mappings
117
+ "gemini-flash": "gemini-1.5-flash",
118
+ "claude-3.5-sonnet": "claude-sonnet-3.5",
119
+ "gemini-1.5-pro-latest": "gemini-pro",
120
+ "gemini-1.5-pro": "gemini-1.5-pro",
121
+ "claude-3-5-sonnet-20240620": "claude-sonnet-3.5",
122
+ "claude-3-5-sonnet": "claude-sonnet-3.5",
123
+ "Niansuh": "Niansuh",
124
+ "o1-preview": "o1-preview",
125
+ "claude-3-5-sonnet-20241022": "claude-3-5-sonnet-20241022",
126
+ "claude-3-5-sonnet-x": "claude-3-5-sonnet-x",
127
+ # Added New Agents
128
+ "FlaskAgent": "FlaskAgent",
129
+ "FirebaseAgent": "FirebaseAgent",
130
+ "FastAPIAgent": "FastAPIAgent",
131
+ "ErlangAgent": "ErlangAgent",
132
+ "ElectronAgent": "ElectronAgent",
133
+ "DockerAgent": "DockerAgent",
134
+ "DigitalOceanAgent": "DigitalOceanAgent",
135
+ "BitbucketAgent": "BitbucketAgent",
136
+ "AzureAgent": "AzureAgent",
137
+ "FlutterAgent": "FlutterAgent",
138
+ "YoutubeAgent": "YoutubeAgent",
139
+ "builderAgent": "builderAgent",
140
+ }
141
+
142
+ # Agent modes
143
+ AGENT_MODE = {
144
+ 'flux': {'mode': True, 'id': "ImageGenerationLV45LJp", 'name': "flux"},
145
+ 'Niansuh': {'mode': True, 'id': "NiansuhAIk1HgESy", 'name': "Niansuh"},
146
+ 'o1-preview': {'mode': True, 'id': "o1Dst8La8", 'name': "o1-preview"},
147
+ 'claude-3-5-sonnet-20241022': {'mode': True, 'id': "Claude-Sonnet-3.5zO2HZSF", 'name': "claude-3-5-sonnet-20241022"},
148
+ 'claude-3-5-sonnet-x': {'mode': True, 'id': "Claude-Sonnet-3.52022JE0UdQ3", 'name': "claude-3-5-sonnet-x"},
149
+ }
150
+
151
+ # Trending agent modes
152
+ TRENDING_AGENT_MODE = {
153
+ "blackboxai": {},
154
+ "gemini-1.5-flash": {'mode': True, 'id': 'Gemini'},
155
+ "llama-3.1-8b": {'mode': True, 'id': "llama-3.1-8b"},
156
+ 'llama-3.1-70b': {'mode': True, 'id': "llama-3.1-70b"},
157
+ 'llama-3.1-405b': {'mode': True, 'id': "llama-3.1-405"},
158
+ 'blackboxai-pro': {'mode': True, 'id': "BLACKBOXAI-PRO"},
159
+ 'PythonAgent': {'mode': True, 'id': "Python Agent"},
160
+ 'JavaAgent': {'mode': True, 'id': "Java Agent"},
161
+ 'JavaScriptAgent': {'mode': True, 'id': "JavaScript Agent"},
162
+ 'HTMLAgent': {'mode': True, 'id': "HTML Agent"},
163
+ 'GoogleCloudAgent': {'mode': True, 'id': "Google Cloud Agent"},
164
+ 'AndroidDeveloper': {'mode': True, 'id': "Android Developer"},
165
+ 'SwiftDeveloper': {'mode': True, 'id': "Swift Developer"},
166
+ 'Next.jsAgent': {'mode': True, 'id': "Next.js Agent"},
167
+ 'MongoDBAgent': {'mode': True, 'id': "MongoDB Agent"},
168
+ 'PyTorchAgent': {'mode': True, 'id': "PyTorch Agent"},
169
+ 'ReactAgent': {'mode': True, 'id': "React Agent"},
170
+ 'XcodeAgent': {'mode': True, 'id': "Xcode Agent"},
171
+ 'AngularJSAgent': {'mode': True, 'id': "AngularJS Agent"},
172
+ 'HerokuAgent': {'mode': True, 'id': "HerokuAgent"},
173
+ 'GodotAgent': {'mode': True, 'id': "GodotAgent"},
174
+ 'GoAgent': {'mode': True, 'id': "GoAgent"},
175
+ 'GitlabAgent': {'mode': True, 'id': "GitlabAgent"},
176
+ 'GitAgent': {'mode': True, 'id': "GitAgent"},
177
+ 'RepoMap': {'mode': True, 'id': "repomap"},
178
+ # Added New Agents
179
+ 'FlaskAgent': {'mode': True, 'id': "FlaskAgent"},
180
+ 'FirebaseAgent': {'mode': True, 'id': "FirebaseAgent"},
181
+ 'FastAPIAgent': {'mode': True, 'id': "FastAPIAgent"},
182
+ 'ErlangAgent': {'mode': True, 'id': "ErlangAgent"},
183
+ 'ElectronAgent': {'mode': True, 'id': "ElectronAgent"},
184
+ 'DockerAgent': {'mode': True, 'id': "DockerAgent"},
185
+ 'DigitalOceanAgent': {'mode': True, 'id': "DigitalOceanAgent"},
186
+ 'BitbucketAgent': {'mode': True, 'id': "BitbucketAgent"},
187
+ 'AzureAgent': {'mode': True, 'id': "AzureAgent"},
188
+ 'FlutterAgent': {'mode': True, 'id': "FlutterAgent"},
189
+ 'YoutubeAgent': {'mode': True, 'id': "YoutubeAgent"},
190
+ 'builderAgent': {'mode': True, 'id': "builderAgent"},
191
+ }
192
+
193
+ # Model prefixes
194
+ MODEL_PREFIXES = {
195
+ 'gpt-4o': '@GPT-4o',
196
+ 'gemini-pro': '@Gemini-PRO',
197
+ 'PythonAgent': '@Python Agent',
198
+ 'JavaAgent': '@Java Agent',
199
+ 'JavaScriptAgent': '@JavaScript Agent',
200
+ 'HTMLAgent': '@HTML Agent',
201
+ 'GoogleCloudAgent': '@Google Cloud Agent',
202
+ 'AndroidDeveloper': '@Android Developer',
203
+ 'SwiftDeveloper': '@Swift Developer',
204
+ 'Next.jsAgent': '@Next.js Agent',
205
+ 'MongoDBAgent': '@MongoDB Agent',
206
+ 'PyTorchAgent': '@PyTorch Agent',
207
+ 'ReactAgent': '@React Agent',
208
+ 'XcodeAgent': '@Xcode Agent',
209
+ 'AngularJSAgent': '@AngularJS Agent',
210
+ 'HerokuAgent': '@Heroku Agent',
211
+ 'GodotAgent': '@Godot Agent',
212
+ 'GoAgent': '@Go Agent',
213
+ 'GitlabAgent': '@Gitlab Agent',
214
+ 'GitAgent': '@Gitlab Agent',
215
+ 'blackboxai-pro': '@BLACKBOXAI-PRO',
216
+ 'flux': '@Image Generation',
217
+ # Added New Agents
218
+ 'FlaskAgent': '@Flask Agent',
219
+ 'FirebaseAgent': '@Firebase Agent',
220
+ 'FastAPIAgent': '@FastAPI Agent',
221
+ 'ErlangAgent': '@Erlang Agent',
222
+ 'ElectronAgent': '@Electron Agent',
223
+ 'DockerAgent': '@Docker Agent',
224
+ 'DigitalOceanAgent': '@DigitalOcean Agent',
225
+ 'BitbucketAgent': '@Bitbucket Agent',
226
+ 'AzureAgent': '@Azure Agent',
227
+ 'FlutterAgent': '@Flutter Agent',
228
+ 'YoutubeAgent': '@Youtube Agent',
229
+ 'builderAgent': '@builder Agent',
230
+ }
231
+
232
+ # Cache variables for getHid function
233
+ cached_hid = None
234
+ cache_time = 0
235
+ CACHE_DURATION = 36000 # 10 hours
236
+
237
+ # Headers for HTTP requests
238
+ headers = {
239
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
240
+ 'AppleWebKit/537.36 (KHTML, like Gecko) '
241
+ 'Chrome/130.0.0.0 Safari/537.36',
242
+ 'Content-Type': 'application/json',
243
+ }
244
+
245
+ def getHid(force_refresh=False):
246
+ global cached_hid, cache_time
247
+ current_time = time.time()
248
+
249
+ # Check if we need to refresh or cache is valid
250
+ if not force_refresh and cached_hid and (current_time - cache_time) < CACHE_DURATION:
251
+ logger.info(f"Using cached_hid: {cached_hid}")
252
+ return cached_hid
253
+
254
+ try:
255
+ # Get initial HTML content
256
+ response = requests.get(BASE_URL, headers=headers)
257
+ response.raise_for_status()
258
+ content = response.text
259
+
260
+ # Use regex to find the specific static/chunks path
261
+ pattern = r"static/chunks/app/layout-[a-zA-Z0-9]+\.js"
262
+ match = re.search(pattern, content)
263
+
264
+ if match:
265
+ # Construct full URL of JS file
266
+ js_path = match.group()
267
+ full_url = f"{BASE_URL}/_next/{js_path}"
268
+
269
+ # Get JS file content
270
+ js_response = requests.get(full_url, headers=headers)
271
+ js_response.raise_for_status()
272
+
273
+ # Search for h-value in JS content
274
+ h_pattern = r'h="([0-9a-f-]+)"'
275
+ h_match = re.search(h_pattern, js_response.text)
276
+
277
+ if h_match:
278
+ h_value = h_match.group(1)
279
+ logger.info(f"Found h-value: {h_value}")
280
+ # Update cache
281
+ cached_hid = h_value
282
+ cache_time = current_time
283
+ return h_value
284
+ else:
285
+ logger.error("h-value not found in JS content")
286
+ return None
287
+ else:
288
+ logger.error("Specified JS file path not found in HTML content")
289
+ return None
290
+ except requests.exceptions.RequestException as e:
291
+ logger.error(f"An error occurred: {e}")
292
+ return None
293
+
294
+ # Helper function to create a random alphanumeric chat ID
295
+ def generate_chat_id(length: int = 7) -> str:
296
+ characters = string.ascii_letters + string.digits
297
+ return ''.join(random.choices(characters, k=length))
298
+
299
  # Helper function to create chat completion data
300
  def create_chat_completion_data(
301
+ content: str, model: str, timestamp: int, finish_reason: Optional[str] = None
 
 
 
302
  ) -> Dict[str, Any]:
303
  return {
304
  "id": f"chatcmpl-{uuid.uuid4()}",
 
308
  "choices": [
309
  {
310
  "index": 0,
311
+ "delta": {"content": content, "role": "assistant"},
312
  "finish_reason": finish_reason,
313
  }
314
  ],
 
317
 
318
  # Function to convert message to dictionary format, ensuring base64 data and optional model prefix
319
  def message_to_dict(message, model_prefix: Optional[str] = None):
320
+ if isinstance(message.content, str):
321
+ content = message.content
322
+ elif isinstance(message.content, list):
323
+ content = message.content[0]["text"]
324
+ else:
325
+ content = message.content
326
+
327
  if model_prefix:
328
  content = f"{model_prefix} {content}"
329
+ if isinstance(message.content, list) and len(message.content) == 2 and "image_url" in message.content[1]:
 
 
 
 
330
  # Ensure base64 images are always included for all models
331
  return {
332
  "role": message.role,
 
349
 
350
  # Process streaming response
351
  async def process_streaming_response(request: ChatRequest):
352
+ chat_id = generate_chat_id()
353
+ referer_url = BASE_URL # MODEL_REFERERS are removed
354
+ logger.info(f"Generated Chat ID: {chat_id} - Model: {request.model} - URL: {referer_url}")
355
 
356
  agent_mode = AGENT_MODE.get(request.model, {})
357
  trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
358
  model_prefix = MODEL_PREFIXES.get(request.model, "")
359
 
360
+ headers_api_chat = headers.copy()
361
+ headers_api_chat['Referer'] = referer_url
362
+
363
  if request.model == 'o1-preview':
364
  delay_seconds = random.randint(1, 60)
365
+ logger.info(f"Introducing a delay of {delay_seconds} seconds for model 'o1-preview' (Chat ID: {chat_id})")
 
 
366
  await asyncio.sleep(delay_seconds)
367
 
368
  json_data = {
 
 
 
 
 
 
369
  "agentMode": agent_mode,
 
 
 
 
 
 
 
 
370
  "clickedAnswer2": False,
371
  "clickedAnswer3": False,
372
  "clickedForceWebSearch": False,
373
+ "codeModelMode": True,
374
+ "githubToken": None,
375
+ "id": chat_id,
376
+ "isChromeExt": False,
377
+ "isMicMode": False,
378
+ "maxTokens": request.max_tokens,
379
+ "messages": [message_to_dict(msg, model_prefix=model_prefix) for msg in request.messages],
380
  "mobileClient": False,
381
+ "playgroundTemperature": request.temperature,
382
+ "playgroundTopP": request.top_p,
383
+ "previewToken": None,
384
+ "trendingAgentMode": trending_agent_mode,
385
+ "userId": None,
386
  "userSelectedModel": MODEL_MAPPING.get(request.model, request.model),
387
+ "userSystemPrompt": None,
388
+ "validated": getHid(),
389
+ "visitFromDelta": False,
390
  }
391
 
392
  async with httpx.AsyncClient() as client:
 
394
  async with client.stream(
395
  "POST",
396
  f"{BASE_URL}/api/chat",
397
+ headers=headers_api_chat,
398
  json=json_data,
399
  timeout=100,
400
  ) as response:
401
  response.raise_for_status()
402
  async for line in response.aiter_lines():
403
+ timestamp = int(datetime.now().timestamp())
404
  if line:
405
+ content = line
406
+ if "https://www.blackbox.ai" in content:
407
+ getHid(True)
408
+ content = "hid已刷新,重新对话即可\n"
409
+ yield f"data: {json.dumps(create_chat_completion_data(content, request.model, timestamp))}\n\n"
 
 
 
410
  break
411
+ if content.startswith("$@$v=undefined-rv1$@$"):
412
+ content = content[21:]
413
+ cleaned_content = strip_model_prefix(content, model_prefix)
414
+ yield f"data: {json.dumps(create_chat_completion_data(cleaned_content, request.model, timestamp))}\n\n"
415
+
416
+ yield f"data: {json.dumps(create_chat_completion_data('', request.model, timestamp, 'stop'))}\n\n"
 
 
 
 
 
 
417
  yield "data: [DONE]\n\n"
418
  except httpx.HTTPStatusError as e:
419
+ logger.error(f"HTTP error occurred for Chat ID {chat_id}: {e}")
420
  raise HTTPException(status_code=e.response.status_code, detail=str(e))
421
  except httpx.RequestError as e:
422
+ logger.error(f"Error occurred during request for Chat ID {chat_id}: {e}")
423
  raise HTTPException(status_code=500, detail=str(e))
424
 
425
  # Process non-streaming response
426
  async def process_non_streaming_response(request: ChatRequest):
427
+ chat_id = generate_chat_id()
428
+ referer_url = BASE_URL
429
+ logger.info(f"Generated Chat ID: {chat_id} - Model: {request.model} - URL: {referer_url}")
430
 
431
  agent_mode = AGENT_MODE.get(request.model, {})
432
  trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
433
  model_prefix = MODEL_PREFIXES.get(request.model, "")
434
 
435
+ headers_api_chat = headers.copy()
436
+ headers_api_chat['Referer'] = referer_url
437
+
438
  if request.model == 'o1-preview':
439
  delay_seconds = random.randint(20, 60)
440
+ logger.info(f"Introducing a delay of {delay_seconds} seconds for model 'o1-preview' (Chat ID: {chat_id})")
 
 
441
  await asyncio.sleep(delay_seconds)
442
 
443
  json_data = {
 
 
 
 
 
 
444
  "agentMode": agent_mode,
 
 
 
 
 
 
 
 
445
  "clickedAnswer2": False,
446
  "clickedAnswer3": False,
447
  "clickedForceWebSearch": False,
448
+ "codeModelMode": True,
449
+ "githubToken": None,
450
+ "id": chat_id,
451
+ "isChromeExt": False,
452
+ "isMicMode": False,
453
+ "maxTokens": request.max_tokens,
454
+ "messages": [message_to_dict(msg, model_prefix=model_prefix) for msg in request.messages],
455
  "mobileClient": False,
456
+ "playgroundTemperature": request.temperature,
457
+ "playgroundTopP": request.top_p,
458
+ "previewToken": None,
459
+ "trendingAgentMode": trending_agent_mode,
460
+ "userId": None,
461
  "userSelectedModel": MODEL_MAPPING.get(request.model, request.model),
462
+ "userSystemPrompt": None,
463
+ "validated": getHid(),
464
+ "visitFromDelta": False,
465
  }
466
 
467
  full_response = ""
468
  async with httpx.AsyncClient() as client:
469
  try:
470
+ async with client.stream(
471
+ method="POST", url=f"{BASE_URL}/api/chat", headers=headers_api_chat, json=json_data
472
+ ) as response:
473
+ response.raise_for_status()
474
+ async for chunk in response.aiter_text():
475
+ full_response += chunk
 
476
  except httpx.HTTPStatusError as e:
477
+ logger.error(f"HTTP error occurred for Chat ID {chat_id}: {e}")
478
  raise HTTPException(status_code=e.response.status_code, detail=str(e))
479
  except httpx.RequestError as e:
480
+ logger.error(f"Error occurred during request for Chat ID {chat_id}: {e}")
481
  raise HTTPException(status_code=500, detail=str(e))
 
482
  if "https://www.blackbox.ai" in full_response:
483
+ getHid(True)
 
484
  full_response = "hid已刷新,重新对话即可"
485
  if full_response.startswith("$@$v=undefined-rv1$@$"):
486
  full_response = full_response[21:]