Niansuh commited on
Commit
5f9e187
·
verified ·
1 Parent(s): 06a06e5

Update api/utils.py

Browse files
Files changed (1) hide show
  1. api/utils.py +19 -9
api/utils.py CHANGED
@@ -134,12 +134,15 @@ async def process_streaming_response(request: ChatRequest):
134
  timestamp = int(datetime.now().timestamp())
135
  if line:
136
  content = line
137
- if "https://www.blackbox.ai" in content:
 
 
 
138
  validate.getHid(True)
139
  content = "hid已刷新,重新对话即可\n"
140
  yield f"data: {json.dumps(create_chat_completion_data(content, request.model, timestamp))}\n\n"
141
  break
142
- if content.startswith("$@$v=undefined-rv1$@$"):
143
  content = content[21:]
144
  cleaned_content = strip_model_prefix(content, model_prefix)
145
  yield f"data: {json.dumps(create_chat_completion_data(cleaned_content, request.model, timestamp))}\n\n"
@@ -198,21 +201,28 @@ async def process_non_streaming_response(request: ChatRequest):
198
  full_response = ""
199
  async with httpx.AsyncClient() as client:
200
  try:
201
- async with client.stream(
202
- method="POST", url=f"{BASE_URL}/api/chat", headers=headers_api_chat, json=json_data
203
- ) as response:
204
- response.raise_for_status()
205
- async for chunk in response.aiter_text():
206
- full_response += chunk
 
 
 
207
  except httpx.HTTPStatusError as e:
208
  logger.error(f"HTTP error occurred for Chat ID {chat_id}: {e}")
209
  raise HTTPException(status_code=e.response.status_code, detail=str(e))
210
  except httpx.RequestError as e:
211
  logger.error(f"Error occurred during request for Chat ID {chat_id}: {e}")
212
  raise HTTPException(status_code=500, detail=str(e))
213
- if "https://www.blackbox.ai" in full_response:
 
 
 
214
  validate.getHid(True)
215
  full_response = "hid已刷新,重新对话即可"
 
216
  if full_response.startswith("$@$v=undefined-rv1$@$"):
217
  full_response = full_response[21:]
218
 
 
134
  timestamp = int(datetime.now().timestamp())
135
  if line:
136
  content = line
137
+ logger.debug(f"Received content: {content}")
138
+ # Modify the condition to detect specific error message
139
+ if "Invalid or expired 'hid'" in content:
140
+ logger.warning("Invalid or expired 'hid' detected. Refreshing 'hid'.")
141
  validate.getHid(True)
142
  content = "hid已刷新,重新对话即可\n"
143
  yield f"data: {json.dumps(create_chat_completion_data(content, request.model, timestamp))}\n\n"
144
  break
145
+ elif content.startswith("$@$v=undefined-rv1$@$"):
146
  content = content[21:]
147
  cleaned_content = strip_model_prefix(content, model_prefix)
148
  yield f"data: {json.dumps(create_chat_completion_data(cleaned_content, request.model, timestamp))}\n\n"
 
201
  full_response = ""
202
  async with httpx.AsyncClient() as client:
203
  try:
204
+ response = await client.post(
205
+ f"{BASE_URL}/api/chat",
206
+ headers=headers_api_chat,
207
+ json=json_data,
208
+ timeout=100,
209
+ )
210
+ response.raise_for_status()
211
+ full_response = response.text
212
+ logger.debug(f"Full response received: {full_response}")
213
  except httpx.HTTPStatusError as e:
214
  logger.error(f"HTTP error occurred for Chat ID {chat_id}: {e}")
215
  raise HTTPException(status_code=e.response.status_code, detail=str(e))
216
  except httpx.RequestError as e:
217
  logger.error(f"Error occurred during request for Chat ID {chat_id}: {e}")
218
  raise HTTPException(status_code=500, detail=str(e))
219
+
220
+ # Modify the condition to detect specific error message
221
+ if "Invalid or expired 'hid'" in full_response:
222
+ logger.warning("Invalid or expired 'hid' detected. Refreshing 'hid'.")
223
  validate.getHid(True)
224
  full_response = "hid已刷新,重新对话即可"
225
+
226
  if full_response.startswith("$@$v=undefined-rv1$@$"):
227
  full_response = full_response[21:]
228