自我一致性 (Self-consistency)
- 概念:該技術通過對同一問題采樣不同的推理路徑,并通過多數投票選擇最一致的答案,來解決大語言模型(LLM)輸出的可變性問題。通過使用不同的溫度(temperature)或采樣設置生成多條推理路徑,然后聚合最終答案,自洽性能夠提高復雜推理任務的準確性。從本質上講,這是一種針對大語言模型輸出的集成方法。
- 原理:由于復雜問題存在多種合理推理路徑,不同路徑可通過不同思考過程抵達同一正確答案。自洽性通過聚合這些路徑,抵消單一路徑的偏差,從而提升準確性。
- 目標:通過采樣多樣化推理路徑并聚合結果,提升模型推理的準確性和可靠性。
- 總結:用于替代鏈式思維提示(chain-of-thought prompting)中的貪心解碼。該方法通過采樣多樣化推理路徑并聚合最一致答案,提升復雜推理任務表現。
關鍵步驟
自洽性方法核心邏輯
- 步驟 1:對同一問題,使用不同采樣設置(如溫度參數)生成多條不同的推理路徑,而非僅采用貪心解碼的單一路徑。
- 步驟 2:通過邊緣化(marginalizing)采樣的推理路徑,選擇最一致的答案(如通過多數投票等聚合方式)。
復雜推理問題通常存在多種合理的思考方式,最終指向唯一正確答案,多樣化路徑可提升答案的可靠性。
案例
這是一個測試方法,用于測試郵件分類的一致性(self-consistency)。主要通過對同一封郵件進行多次分類,采用多數投票的方式來確定最終分類結果。
設置溫度參數 temperature(1.0) 以增加輸出的隨機性,采用多種不同的推理路徑得到罪過,最終通過投票的方式聚合結果得到一致的答案
@Testpublic void testSelfConsistency() throws Exception{String email = """Hi,I have seen you use Wordpress for your website. A great opensource content management system. I have used it in the pasttoo. It comes with lots of great user plugins. And it's prettyeasy to set up.I did notice a bug in the contact form, which happens whenyou select the name field. See the attached screenshot of meentering text in the name field. Notice the JavaScript alertbox that I inv0k3d.But for the rest it's a great website. I enjoy reading it. Feelfree to leave the bug in the website, because it gives me moreinteresting things to read.Cheers,Harry the Hacker.""";int importantCount = 0;int notImportantCount = 0;// Run the model 5 times with the same inputfor (int i = 0; i < 5; i++) {EmailClassification output = openAiChatClient.prompt().user(u -> u.text("""Email: {email}Classify the above email as IMPORTANT or NOT IMPORTANT. Let'sthink step by step and explain why.""").param("email", email)).options(ChatOptions.builder().temperature(1.0) // Higher temperature for more variation.build()).call().entity(EmailClassification.class);System.out.println(output.reasoning);// Count resultsif (output.classification() == EmailClassification.Classification.IMPORTANT) {importantCount++;} else {notImportantCount++;}}// Determine the final classification by majority voteString finalClassification = importantCount > notImportantCount ?"IMPORTANT" : "NOT IMPORTANT";System.out.println(finalClassification);}record EmailClassification(Classification classification, String reasoning) {enum Classification {IMPORTANT, NOT_IMPORTANT}}
參考
1.spring-ai self consistency: https://docs.spring.io/spring-ai/reference/api/chat/prompt-engineering-patterns.html#_2_6_self_consistency
2.Self-Consistency Improves Chain of Thought Reasoning in Language Models,https://arxiv.org/abs/2203.11171