Prompt工程学习之自我一致性
自我一致性 (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