当前位置: 首页 > news >正文

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

http://www.xdnf.cn/news/937801.html

相关文章:

  • 6.8 note
  • Python学习——排序
  • Linux 内存管理调试分析:ftrace、perf、crash 的系统化使用
  • 3.机器学习-分类模型-线性模型
  • 《深入理解 Nacos 集群与 Raft 协议》系列四:日志复制机制:Raft 如何确保提交可靠且幂等
  • 《Spring Boot 微服务架构下的高并发活动系统设计与实践》
  • CQF预备知识:Python相关库 -- SciPy 安装
  • 会计-合并-5- 处置交易在合报与个报会计处理
  • 由汇编代码确定switch语句
  • 第13次01:广告及商品数据呈现
  • (LeetCode 每日一题)386. 字典序排数(递归、深度优先搜索dfs || 递推)
  • 动态生成 PV 的机制:使用 NFS-Client Provisione
  • 深入解析Golang反射机制与高效文件操作实践
  • TCP相关问题 第一篇
  • Windows权限提升篇数据库篇MYSQLMSSQLORACLE自动化项目
  • 管理数据洪流:自动化处理与归档每日数据文件的策略与实践
  • BOM(Browser Object Model)核心对象手册
  • CNN核心机制深度解析:卷积池化原理 PyTorch实现经典网络
  • MSYS2 环境配置与 Python 项目依赖管理笔记
  • Z-FOLD: A Frustratingly Easy Post-Training Quantization Scheme for LLMs
  • MS39531N 是一款正弦驱动的三相无感直流电机驱动器,具有最小振动和高效率的特点
  • 深入理解 Java 的反射、注解与动态代理
  • 基于Python学习《Head First设计模式》第十章 状态模式
  • JavaScript数组扁平化(Array Flattening)全解析:从基础到进阶的9种实现方式及深度对比
  • C++.OpenGL (17/64)深度测试(Depth Testing)
  • Python Wheel 打包基本原理详解
  • LangChain工具集成实战:构建智能问答系统完整指南
  • RoboDK 自定义机器人
  • 当前市场环境下,软件行业的突围之道:技术演进与商业模式重构
  • 工厂方法模式和抽象工厂方法模式的battle