引領你了解
- ·如何評估系統生成的輸出。
- 確保在向用戶展示輸出之前,對其質量、相關性和安全性進行嚴格的檢查,以保證我們提供的反饋是準確和適用的。
- 如何運用審查(Moderation) API 來對輸出進行評估
- 如何通過額外的 Prompt 提升模型在展示輸出之前的質量評估。
一、檢查有害內容
我們主要通過 OpenAI 提供的 Moderation API 來實現對有害內容的檢查。
import openai
from tool import get_completion_from_messagesfinal_response_to_customer = f"""
SmartX ProPhone 有一個 6.1 英寸的顯示屏,128GB 存儲、\
1200 萬像素的雙攝像頭,以及 5G。FotoSnap 單反相機\
有一個 2420 萬像素的傳感器,1080p 視頻,3 英寸 LCD 和\
可更換的鏡頭。我們有各種電視,包括 CineView 4K 電視,\
55 英寸顯示屏,4K 分辨率、HDR,以及智能電視功能。\
我們也有 SoundMax 家庭影院系統,具有 5.1 聲道,\
1000W 輸出,無線重低音揚聲器和藍牙。關于這些產品或\
我們提供的任何其他產品您是否有任何具體問題?
"""
# Moderation 是 OpenAI 的內容審核函數,旨在評估并檢測文本內容中的潛在風險。
response = openai.Moderation.create(input=final_response_to_customer
)
moderation_output = response["results"][0]
print(moderation_output)
{"categories": {"harassment": false,"harassment/threatening": false,"hate": false,"hate/threatening": false,"self-harm": false,"self-harm/instructions": false,"self-harm/intent": false,"sexual": false,"sexual/minors": false,"violence": false,"violence/graphic": false},"category_scores": {"harassment": 4.2861907e-07,"harassment/threatening": 5.9538485e-09,"hate": 2.079682e-07,"hate/threatening": 5.6982725e-09,"self-harm": 2.3966843e-08,"self-harm/instructions": 1.5763412e-08,"self-harm/intent": 5.042827e-09,"sexual": 2.6989035e-06,"sexual/minors": 1.1349888e-06,"violence": 1.2788286e-06,"violence/graphic": 2.6259923e-07},"flagged": false
}
檢查輸出質量
檢查輸出的質量對于確保聊天機器人的表現至關重要。以下是一些檢查輸出質量的方法:
1. 標記輸出
- 如果輸出沒有被標記為任何特定類別,并且在所有類別中得分都很低,說明結果評判合理。
- 對于對內容敏感的受眾,可以設定更低的閾值來標記可能存在問題的輸出。
2. 采取適當措施
- 如果某些內容被標記為有問題,可以采取適當措施,如提供替代答案或生成新的響應。
- 隨著模型的不斷改進,有害輸出的可能性會降低。
3. 向模型詢問
- 通過將生成的輸出作為輸入的一部分再次提供給模型,并要求其評估輸出的質量,可以檢查輸出的質量。
- 這種操作可以通過多種方式完成,例如將生成的輸出提供給模型,并要求其評估輸出的質量。
總結
- 確保聊天機器人輸出的質量符合預期是非常重要的。
- 通過標記輸出、采取適當措施和向模型詢問來檢查輸出的質量。
- 隨著模型的持續改進,輸出質量應該會逐漸提高。
二、檢查是否符合產品信息
在下列示例中,我們要求 LLM 作為一個助理檢查回復是否充分回答了客戶問題,并驗證助理引用的事實是否正確。
# 這是一段電子產品相關的信息
system_message = f"""
您是一個助理,用于評估客服代理的回復是否充分回答了客戶問題,\
并驗證助理從產品信息中引用的所有事實是否正確。
產品信息、用戶和客服代理的信息將使用三個反引號(即 ```)\
進行分隔。
請以 Y 或 N 的字符形式進行回復,不要包含標點符號:\
Y - 如果輸出充分回答了問題并且回復正確地使用了產品信息\
N - 其他情況。僅輸出單個字母。
"""#這是顧客的提問
customer_message = f"""
告訴我有關 smartx pro 手機\
和 fotosnap 相機(單反相機)的信息。\
還有您電視的信息。
"""
product_information = """{ "name": "SmartX ProPhone", "category": "Smartphones and Accessories", "brand": "SmartX", "model_number": "SX-PP10", "warranty": "1 year", "rating": 4.6, "features": [ "6.1-inch display", "128GB storage", "12MP dual camera", "5G" ], "description": "A powerful smartphone with advanced camera features.", "price": 899.99 } { "name": "FotoSnap DSLR Camera", "category": "Cameras and Camcorders", "brand": "FotoSnap", "model_number": "FS-DSLR200", "warranty": "1 year", "rating": 4.7, "features": [ "24.2MP sensor", "1080p video", "3-inch LCD", "Interchangeable lenses" ], "description": "Capture stunning photos and videos with this versatile DSLR camera.", "price": 599.99 } { "name": "CineView 4K TV", "category": "Televisions and Home Theater Systems", "brand": "CineView", "model_number": "CV-4K55", "warranty": "2 years", "rating": 4.8, "features": [ "55-inch display", "4K resolution", "HDR", "Smart TV" ], "description": "A stunning 4K TV with vibrant colors and smart features.", "price": 599.99 } { "name": "SoundMax Home Theater", "category": "Televisions and Home Theater Systems", "brand": "SoundMax", "model_number": "SM-HT100", "warranty": "1 year", "rating": 4.4, "features": [ "5.1 channel", "1000W output", "Wireless subwoofer", "Bluetooth" ], "description": "A powerful home theater system for an immersive audio experience.", "price": 399.99 } { "name": "CineView 8K TV", "category": "Televisions and Home Theater Systems", "brand": "CineView", "model_number": "CV-8K65", "warranty": "2 years", "rating": 4.9, "features": [ "65-inch display", "8K resolution", "HDR", "Smart TV" ], "description": "Experience the future of television with this stunning 8K TV.", "price": 2999.99 } { "name": "SoundMax Soundbar", "category": "Televisions and Home Theater Systems", "brand": "SoundMax", "model_number": "SM-SB50", "warranty": "1 year", "rating": 4.3, "features": [ "2.1 channel", "300W output", "Wireless subwoofer", "Bluetooth" ], "description": "Upgrade your TV's audio with this sleek and powerful soundbar.", "price": 199.99 } { "name": "CineView OLED TV", "category": "Televisions and Home Theater Systems", "brand": "CineView", "model_number": "CV-OLED55", "warranty": "2 years", "rating": 4.7, "features": [ "55-inch display", "4K resolution", "HDR", "Smart TV" ], "description": "Experience true blacks and vibrant colors with this OLED TV.", "price": 1499.99 }"""q_a_pair = f"""
顧客的信息: ```{customer_message}```
產品信息: ```{product_information}```
代理的回復: ```{final_response_to_customer}```回復是否正確使用了檢索的信息?
回復是否充分地回答了問題?輸出 Y 或 N
"""
#判斷相關性
messages = [{'role': 'system', 'content': system_message},{'role': 'user', 'content': q_a_pair}
]response = get_completion_from_messages(messages, max_tokens=1)
print(response)
Y
在上一個示例中,我們給了一個正例,LLM 很好地做出了正確的檢查。而在下一個示例中,我們將提供一個負例,LLM 同樣能夠正確判斷。
another_response = "生活就像一盒巧克力"
q_a_pair = f"""
顧客的信息: ```{customer_message}```
產品信息: ```{product_information}```
代理的回復: ```{another_response}```回復是否正確使用了檢索的信息?
回復是否充分地回答了問題?輸出 Y 或 N
"""
messages = [{'role': 'system', 'content': system_message},{'role': 'user', 'content': q_a_pair}
]response = get_completion_from_messages(messages)
print(response)
N
審查 API 可以提供生成輸出質量的反饋,你可以根據這些反饋來決定是否展示輸出給用戶,或者生成新的回應。你還可以嘗試為每個用戶查詢多個模型回應,然后從中選擇最佳的回應呈現給用戶。因此,你有多種可能的嘗試方式。
然而,在大多數情況下,審查輸出并不是必需的,特別是當你使用更先進的模型,比如 GPT-4。實際上,在真實生產環境中,我們并未看到很多人采取這種方式。這種做法也會增加系統的延遲和成本,因為你需要等待額外的 API 調用,并且需要額外的 token。如果你的應用或產品的錯誤率僅為 0.0000001%,那么你可能可以嘗試這種策略。但總的來說,我們并不建議在實際應用中使用這種方式。
在接下來的章節中,我們將整合我們在評估輸入、處理輸出以及審查生成內容方面學到的知識,構建一個端到端的系統。