Upload 2 files
Browse files- Dockerfile +3 -0
- retry_middleware.py +48 -0
Dockerfile
CHANGED
|
@@ -1,5 +1,8 @@
|
|
| 1 |
FROM hpyp/bbapi:latest
|
| 2 |
|
|
|
|
|
|
|
|
|
|
| 3 |
# ENV APP_SECRET=
|
| 4 |
|
| 5 |
EXPOSE 8001
|
|
|
|
| 1 |
FROM hpyp/bbapi:latest
|
| 2 |
|
| 3 |
+
# 复制重试中间件文件到容器中
|
| 4 |
+
COPY retry_middleware.py /app/retry_middleware.py
|
| 5 |
+
|
| 6 |
# ENV APP_SECRET=
|
| 7 |
|
| 8 |
EXPOSE 8001
|
retry_middleware.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
from functools import wraps
|
| 3 |
+
|
| 4 |
+
def retry_on_unsafe_content(max_retries=3, delay=1):
|
| 5 |
+
"""
|
| 6 |
+
装饰器函数,用于在收到 'content is not safe' 响应时自动重试
|
| 7 |
+
|
| 8 |
+
Args:
|
| 9 |
+
max_retries (int): 最大重试次数
|
| 10 |
+
delay (int): 重试之间的延迟时间(秒)
|
| 11 |
+
"""
|
| 12 |
+
def decorator(func):
|
| 13 |
+
@wraps(func)
|
| 14 |
+
async def wrapper(*args, **kwargs):
|
| 15 |
+
retries = 0
|
| 16 |
+
while retries < max_retries:
|
| 17 |
+
try:
|
| 18 |
+
response = await func(*args, **kwargs)
|
| 19 |
+
|
| 20 |
+
# 检查响应中是否包含 "content is not safe" 错误
|
| 21 |
+
if isinstance(response, dict) and response.get('error') and 'content is not safe' in response['error'].lower():
|
| 22 |
+
retries += 1
|
| 23 |
+
if retries < max_retries:
|
| 24 |
+
print(f"检测到内容安全问题,正在进行第 {retries} 次重试...")
|
| 25 |
+
time.sleep(delay)
|
| 26 |
+
continue
|
| 27 |
+
return response
|
| 28 |
+
except Exception as e:
|
| 29 |
+
if 'content is not safe' in str(e).lower():
|
| 30 |
+
retries += 1
|
| 31 |
+
if retries < max_retries:
|
| 32 |
+
print(f"检测到内容安全问题,正在进行第 {retries} 次重试...")
|
| 33 |
+
time.sleep(delay)
|
| 34 |
+
continue
|
| 35 |
+
raise e
|
| 36 |
+
|
| 37 |
+
# 如果所有重试都失败,返回最后一次的响应
|
| 38 |
+
return response
|
| 39 |
+
return wrapper
|
| 40 |
+
return decorator
|
| 41 |
+
|
| 42 |
+
# 使用示例:
|
| 43 |
+
"""
|
| 44 |
+
@retry_on_unsafe_content(max_retries=3, delay=1)
|
| 45 |
+
async def your_ai_chat_function(message):
|
| 46 |
+
# 原有的AI聊天处理逻辑
|
| 47 |
+
pass
|
| 48 |
+
"""
|