開始使用免費開始

審核模型回應

即使聊天機器人已產生客製化回應,仍需要確認內容送給客戶是否安全。此步驟中,你將審核這段回應,確保所有分類分數都低於客戶安全門檻。

前一個練習中模型的回覆已提供為 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.")
編輯並執行程式碼