Upload retry_middleware.py
Browse files- retry_middleware.py +38 -27
retry_middleware.py
CHANGED
|
@@ -13,26 +13,41 @@ class RetryMiddleware(BaseHTTPMiddleware):
|
|
| 13 |
|
| 14 |
async def should_retry_response(self, response_data):
|
| 15 |
"""检查响应是否需要重试"""
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
# 检查响应内容
|
| 23 |
-
choices = response_data.get('choices', [])
|
| 24 |
-
if choices:
|
| 25 |
-
content = choices[0].get('message', {}).get('content', '')
|
| 26 |
-
if 'content is not safe' in content.lower():
|
| 27 |
return True
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
async def dispatch(self, request: Request, call_next):
|
| 38 |
# 只处理 /api/v1/chat/completions 路径的请求
|
|
@@ -42,6 +57,7 @@ class RetryMiddleware(BaseHTTPMiddleware):
|
|
| 42 |
# 读取原始请求体
|
| 43 |
body = await request.body()
|
| 44 |
retry_count = 0
|
|
|
|
| 45 |
|
| 46 |
while True: # 无限循环,直到获得正确响应
|
| 47 |
try:
|
|
@@ -65,7 +81,6 @@ class RetryMiddleware(BaseHTTPMiddleware):
|
|
| 65 |
# 检查响应是否需要重试
|
| 66 |
if await self.should_retry_response(response_data):
|
| 67 |
retry_count += 1
|
| 68 |
-
logger.info(f"检测到内容安全问题,立即进行第 {retry_count + 1} 次重试...")
|
| 69 |
continue
|
| 70 |
else:
|
| 71 |
# 如果响应正常,直接返回
|
|
@@ -77,16 +92,12 @@ class RetryMiddleware(BaseHTTPMiddleware):
|
|
| 77 |
)
|
| 78 |
|
| 79 |
except json.JSONDecodeError:
|
| 80 |
-
#
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
status_code=response.status_code,
|
| 84 |
-
headers=dict(response.headers),
|
| 85 |
-
media_type=response.media_type
|
| 86 |
-
)
|
| 87 |
|
| 88 |
except Exception as e:
|
| 89 |
-
|
| 90 |
retry_count += 1
|
| 91 |
continue
|
| 92 |
|
|
|
|
| 13 |
|
| 14 |
async def should_retry_response(self, response_data):
|
| 15 |
"""检查响应是否需要重试"""
|
| 16 |
+
try:
|
| 17 |
+
if isinstance(response_data, dict):
|
| 18 |
+
# 检查错误信息
|
| 19 |
+
error = response_data.get('error', '')
|
| 20 |
+
if isinstance(error, str) and 'content is not safe' in error.lower():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
return True
|
| 22 |
|
| 23 |
+
# 检查响应内容
|
| 24 |
+
choices = response_data.get('choices', [])
|
| 25 |
+
if choices:
|
| 26 |
+
content = choices[0].get('message', {}).get('content', '')
|
| 27 |
+
if not content or 'content is not safe' in content.lower():
|
| 28 |
+
return True
|
| 29 |
+
|
| 30 |
+
# 检查是否有部分内容包含错误信息
|
| 31 |
+
if isinstance(content, str):
|
| 32 |
+
lines = content.split('\n')
|
| 33 |
+
for line in lines:
|
| 34 |
+
if 'content is not safe' in line.lower():
|
| 35 |
+
return True
|
| 36 |
+
|
| 37 |
+
# 检查是否是不需要的响应
|
| 38 |
+
if "I'm Claude" in str(response_data) or "Anthropic" in str(response_data):
|
| 39 |
+
return True
|
| 40 |
+
if "Hello! How can I assist you today?" in str(response_data):
|
| 41 |
+
return True
|
| 42 |
+
|
| 43 |
+
# 检查响应格式
|
| 44 |
+
if not choices or not choices[0].get('message', {}).get('content'):
|
| 45 |
+
return True
|
| 46 |
+
|
| 47 |
+
return False
|
| 48 |
+
except Exception:
|
| 49 |
+
# 如果解析过程中出现任何错误,建议重试
|
| 50 |
+
return True
|
| 51 |
|
| 52 |
async def dispatch(self, request: Request, call_next):
|
| 53 |
# 只处理 /api/v1/chat/completions 路径的请求
|
|
|
|
| 57 |
# 读取原始请求体
|
| 58 |
body = await request.body()
|
| 59 |
retry_count = 0
|
| 60 |
+
last_error = None
|
| 61 |
|
| 62 |
while True: # 无限循环,直到获得正确响应
|
| 63 |
try:
|
|
|
|
| 81 |
# 检查响应是否需要重试
|
| 82 |
if await self.should_retry_response(response_data):
|
| 83 |
retry_count += 1
|
|
|
|
| 84 |
continue
|
| 85 |
else:
|
| 86 |
# 如果响应正常,直接返回
|
|
|
|
| 92 |
)
|
| 93 |
|
| 94 |
except json.JSONDecodeError:
|
| 95 |
+
# JSON解析错误,需要重试
|
| 96 |
+
retry_count += 1
|
| 97 |
+
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
except Exception as e:
|
| 100 |
+
last_error = str(e)
|
| 101 |
retry_count += 1
|
| 102 |
continue
|
| 103 |
|