Niansuh commited on
Commit
901e507
·
verified ·
1 Parent(s): 8d9643a

Update api/utils.py

Browse files
Files changed (1) hide show
  1. api/utils.py +27 -22
api/utils.py CHANGED
@@ -68,13 +68,15 @@ def message_to_dict(message, model_prefix: Optional[str] = None):
68
  return {"role": message.role, "content": content}
69
 
70
  async def process_streaming_response(request: ChatRequest):
71
- model = request.model # Use the requested model directly
72
- agent_mode = AGENT_MODE.get(model, {})
73
- trending_agent_mode = TRENDING_AGENT_MODE.get(model, {})
74
- model_prefix = MODEL_PREFIXES.get(model, "")
75
 
76
  hid = validate.getHid()
77
- logger.info(f"Using hid: {hid} for model: {model}")
 
 
 
78
 
79
  json_data = {
80
  "messages": [message_to_dict(msg, model_prefix=model_prefix) for msg in request.messages],
@@ -95,7 +97,7 @@ async def process_streaming_response(request: ChatRequest):
95
  "clickedForceWebSearch": False,
96
  "visitFromDelta": False,
97
  "mobileClient": False,
98
- "userSelectedModel": model, # Use the requested model
99
  "validated": hid
100
  }
101
 
@@ -114,17 +116,17 @@ async def process_streaming_response(request: ChatRequest):
114
  if line:
115
  content = line
116
  if "https://www.blackbox.ai" in content:
117
- validate.getHid(True)
118
- content = "hid已刷新,重新对话即可"
 
119
  logger.info(f"hid refreshed due to content: {content}")
120
- yield f"data: {json.dumps(create_chat_completion_data(content, model, timestamp))}\n\n"
121
  break
122
  if content.startswith("$@$v=undefined-rv1$@$"):
123
  content = content[21:]
124
- # Do not strip model prefix or modify content
125
- yield f"data: {json.dumps(create_chat_completion_data(content, model, timestamp))}\n\n"
126
 
127
- yield f"data: {json.dumps(create_chat_completion_data('', model, timestamp, 'stop'))}\n\n"
128
  yield "data: [DONE]\n\n"
129
  except httpx.HTTPStatusError as e:
130
  logger.error(f"HTTP error occurred: {e}")
@@ -134,13 +136,15 @@ async def process_streaming_response(request: ChatRequest):
134
  raise HTTPException(status_code=500, detail=str(e))
135
 
136
  async def process_non_streaming_response(request: ChatRequest):
137
- model = request.model # Use the requested model directly
138
- agent_mode = AGENT_MODE.get(model, {})
139
- trending_agent_mode = TRENDING_AGENT_MODE.get(model, {})
140
- model_prefix = MODEL_PREFIXES.get(model, "")
141
 
142
  hid = validate.getHid()
143
- logger.info(f"Using hid: {hid} for model: {model}")
 
 
 
144
 
145
  json_data = {
146
  "messages": [message_to_dict(msg, model_prefix=model_prefix) for msg in request.messages],
@@ -161,7 +165,7 @@ async def process_non_streaming_response(request: ChatRequest):
161
  "clickedForceWebSearch": False,
162
  "visitFromDelta": False,
163
  "mobileClient": False,
164
- "userSelectedModel": model, # Use the requested model
165
  "validated": hid
166
  }
167
  full_response = ""
@@ -181,18 +185,19 @@ async def process_non_streaming_response(request: ChatRequest):
181
  raise HTTPException(status_code=500, detail=str(e))
182
 
183
  if "https://www.blackbox.ai" in full_response:
184
- validate.getHid(True)
185
- full_response = "hid已刷新,重新对话即可"
 
186
  logger.info("hid refreshed due to response content")
187
 
188
  if full_response.startswith("$@$v=undefined-rv1$@$"):
189
  full_response = full_response[21:]
190
- # Do not strip model prefix or modify content
191
  return {
192
  "id": f"chatcmpl-{uuid.uuid4()}",
193
  "object": "chat.completion",
194
  "created": int(datetime.now().timestamp()),
195
- "model": model,
196
  "choices": [
197
  {
198
  "index": 0,
 
68
  return {"role": message.role, "content": content}
69
 
70
  async def process_streaming_response(request: ChatRequest):
71
+ agent_mode = AGENT_MODE.get(request.model, {})
72
+ trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
73
+ model_prefix = MODEL_PREFIXES.get(request.model, "")
 
74
 
75
  hid = validate.getHid()
76
+ logger.info(f"Using hid: {hid} for model: {request.model}")
77
+
78
+ user_selected_model = MODEL_MAPPING.get(request.model, request.model)
79
+ logger.info(f"User selected model: {user_selected_model}")
80
 
81
  json_data = {
82
  "messages": [message_to_dict(msg, model_prefix=model_prefix) for msg in request.messages],
 
97
  "clickedForceWebSearch": False,
98
  "visitFromDelta": False,
99
  "mobileClient": False,
100
+ "userSelectedModel": user_selected_model,
101
  "validated": hid
102
  }
103
 
 
116
  if line:
117
  content = line
118
  if "https://www.blackbox.ai" in content:
119
+ logger.warning("Invalid hid detected, refreshing hid")
120
+ hid = validate.getHid()
121
+ content = "hid has been refreshed, please retry"
122
  logger.info(f"hid refreshed due to content: {content}")
123
+ yield f"data: {json.dumps(create_chat_completion_data(content, request.model, timestamp))}\n\n"
124
  break
125
  if content.startswith("$@$v=undefined-rv1$@$"):
126
  content = content[21:]
127
+ yield f"data: {json.dumps(create_chat_completion_data(content, request.model, timestamp))}\n\n"
 
128
 
129
+ yield f"data: {json.dumps(create_chat_completion_data('', request.model, timestamp, 'stop'))}\n\n"
130
  yield "data: [DONE]\n\n"
131
  except httpx.HTTPStatusError as e:
132
  logger.error(f"HTTP error occurred: {e}")
 
136
  raise HTTPException(status_code=500, detail=str(e))
137
 
138
  async def process_non_streaming_response(request: ChatRequest):
139
+ agent_mode = AGENT_MODE.get(request.model, {})
140
+ trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
141
+ model_prefix = MODEL_PREFIXES.get(request.model, "")
 
142
 
143
  hid = validate.getHid()
144
+ logger.info(f"Using hid: {hid} for model: {request.model}")
145
+
146
+ user_selected_model = MODEL_MAPPING.get(request.model, request.model)
147
+ logger.info(f"User selected model: {user_selected_model}")
148
 
149
  json_data = {
150
  "messages": [message_to_dict(msg, model_prefix=model_prefix) for msg in request.messages],
 
165
  "clickedForceWebSearch": False,
166
  "visitFromDelta": False,
167
  "mobileClient": False,
168
+ "userSelectedModel": user_selected_model,
169
  "validated": hid
170
  }
171
  full_response = ""
 
185
  raise HTTPException(status_code=500, detail=str(e))
186
 
187
  if "https://www.blackbox.ai" in full_response:
188
+ logger.warning("Invalid hid detected in response, refreshing hid")
189
+ hid = validate.getHid()
190
+ full_response = "hid has been refreshed, please retry"
191
  logger.info("hid refreshed due to response content")
192
 
193
  if full_response.startswith("$@$v=undefined-rv1$@$"):
194
  full_response = full_response[21:]
195
+
196
  return {
197
  "id": f"chatcmpl-{uuid.uuid4()}",
198
  "object": "chat.completion",
199
  "created": int(datetime.now().timestamp()),
200
+ "model": request.model,
201
  "choices": [
202
  {
203
  "index": 0,