Skip to content

Commit

Permalink
🐛 Bug: Fix the bug that makes moral checks unusable.
Browse files Browse the repository at this point in the history
  • Loading branch information
yym68686 committed Sep 29, 2024
1 parent 9c2088c commit 179cefd
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,7 @@ async def dispatch(self, request: Request, call_next):

if enable_moderation and moderated_content:
moderation_response = await self.moderate_content(moderated_content, token)
moderation_result = moderation_response.body
moderation_data = json.loads(moderation_result)
is_flagged = moderation_data.get('results', [{}])[0].get('flagged', False)
is_flagged = moderation_response.get('results', [{}])[0].get('flagged', False)

if is_flagged:
logger.error(f"Content did not pass the moral check: %s", moderated_content)
Expand Down Expand Up @@ -447,7 +445,18 @@ async def moderate_content(self, content, token):
# 直接调用 moderations 函数
response = await moderations(moderation_request, token)

return response
# 读取流式响应的内容
moderation_result = b""
async for chunk in response.body_iterator:
if isinstance(chunk, str):
moderation_result += chunk.encode('utf-8')
else:
moderation_result += chunk

# 解码并解析 JSON
moderation_data = json.loads(moderation_result.decode('utf-8'))

return moderation_data

# 配置 CORS 中间件
app.add_middleware(
Expand Down

0 comments on commit 179cefd

Please sign in to comment.