Spaces:
Paused
Paused
Update api/utils.py
Browse files- api/utils.py +29 -20
api/utils.py
CHANGED
|
@@ -103,29 +103,38 @@ async def process_streaming_response(request: ChatRequest):
|
|
| 103 |
json=json_data,
|
| 104 |
timeout=100,
|
| 105 |
) as response:
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
timestamp = int(datetime.now().timestamp())
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
validate.getHid(True)
|
| 113 |
-
content = "hid已刷新,重新对话即可\n"
|
| 114 |
-
yield f"data: {json.dumps(create_chat_completion_data(content, request.model, timestamp))}\n\n"
|
| 115 |
-
break
|
| 116 |
-
if content.startswith("$@$v=undefined-rv1$@$"):
|
| 117 |
-
yield f"data: {json.dumps(create_chat_completion_data(content[21:], request.model, timestamp))}\n\n"
|
| 118 |
-
else:
|
| 119 |
-
yield f"data: {json.dumps(create_chat_completion_data(content, request.model, timestamp))}\n\n"
|
| 120 |
-
|
| 121 |
-
yield f"data: {json.dumps(create_chat_completion_data('', request.model, timestamp, 'stop'))}\n\n"
|
| 122 |
-
yield "data: [DONE]\n\n"
|
| 123 |
-
except httpx.HTTPStatusError as e:
|
| 124 |
-
logger.error(f"HTTP error occurred: {e}")
|
| 125 |
-
raise HTTPException(status_code=e.response.status_code, detail=str(e))
|
| 126 |
except httpx.RequestError as e:
|
| 127 |
logger.error(f"Error occurred during request: {e}")
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
|
| 131 |
async def process_non_streaming_response(request: ChatRequest):
|
|
|
|
| 103 |
json=json_data,
|
| 104 |
timeout=100,
|
| 105 |
) as response:
|
| 106 |
+
try:
|
| 107 |
+
response.raise_for_status()
|
| 108 |
+
async for line in response.aiter_lines():
|
| 109 |
+
timestamp = int(datetime.now().timestamp())
|
| 110 |
+
if line:
|
| 111 |
+
content = line + "\n"
|
| 112 |
+
if "https://www.blackbox.ai" in content:
|
| 113 |
+
validate.getHid(True)
|
| 114 |
+
content = "hid已刷新,重新对话即可\n"
|
| 115 |
+
yield f"data: {json.dumps(create_chat_completion_data(content, request.model, timestamp))}\n\n"
|
| 116 |
+
break
|
| 117 |
+
if content.startswith("$@$v=undefined-rv1$@$"):
|
| 118 |
+
yield f"data: {json.dumps(create_chat_completion_data(content[21:], request.model, timestamp))}\n\n"
|
| 119 |
+
else:
|
| 120 |
+
yield f"data: {json.dumps(create_chat_completion_data(content, request.model, timestamp))}\n\n"
|
| 121 |
+
|
| 122 |
+
yield f"data: {json.dumps(create_chat_completion_data('', request.model, timestamp, 'stop'))}\n\n"
|
| 123 |
+
yield "data: [DONE]\n\n"
|
| 124 |
+
except httpx.HTTPStatusError as e:
|
| 125 |
+
logger.error(f"HTTP error occurred: {e}")
|
| 126 |
+
error_message = "抱歉,服务器暂时无法处理您的请求。请稍后重试。"
|
| 127 |
timestamp = int(datetime.now().timestamp())
|
| 128 |
+
yield f"data: {json.dumps(create_chat_completion_data(error_message, request.model, timestamp))}\n\n"
|
| 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.RequestError as e:
|
| 132 |
logger.error(f"Error occurred during request: {e}")
|
| 133 |
+
error_message = "抱歉,连接服务器时出现错误。请检查网络连接并重试。"
|
| 134 |
+
timestamp = int(datetime.now().timestamp())
|
| 135 |
+
yield f"data: {json.dumps(create_chat_completion_data(error_message, request.model, timestamp))}\n\n"
|
| 136 |
+
yield f"data: {json.dumps(create_chat_completion_data('', request.model, timestamp, 'stop'))}\n\n"
|
| 137 |
+
yield "data: [DONE]\n\n"
|
| 138 |
|
| 139 |
|
| 140 |
async def process_non_streaming_response(request: ChatRequest):
|