对模型回复进行内容审核
即使聊天机器人已经生成了定制化回复,也需要确认内容适合发送给客户。在此步骤,您将对回复进行审核,确保所有类别分数都低于客户安全阈值。
上一练习中模型的回复已保存为 chatbot_reply。
本练习是课程的一部分
使用 OpenAI API 的多模态系统
练习说明
- 发送一次内容审核请求,输入为
chatbot_reply。 - 提取响应中的各类别分数,使用
.model_dump()转为字典,并检查是否有任一分数「高于」0.7。
交互式实操练习
通过完成这段示例代码来试试这个练习。
client = OpenAI(api_key="")
# Send the moderation request
response = ____
# Extract scores and convert to dictionary
scores = ____
if any(____ for score in scores.values()):
print("AI Response flagged for moderation!")
chatbot_reply = """I'm sorry, but I can't provide a response to that request. Please contact [email protected] for further assistance."""
else:
print("AI Response is safe.")