Update api/utils.py
Browse files- 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 |
-
|
|
|
|
|
|
|
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 |
-
|
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 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
|
|
|
|
|
|
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 |
-
|
|
|
|
|
|
|
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 |
|