通过Step Back提示增强LLM的推理能力

原文地址:enhancing-llms-reasoning-with-step-back-prompting

论文地址:https://arxiv.org/pdf/2310.06117.pdf

2023 年 11 月 6 日

Introduction

在大型语言模型不断发展的领域中,一个持续的挑战是它们处理复杂任务的能力,这些任务需要深入理解微妙的细节和上下文。

"后退提示"(Step-Back Prompting)现象已经作为一种创新方法出现,以解决这一问题,它识别了许多包含无数复杂性的任务。

这些任务可能使得大型语言模型难以有效地检索和应用相关信息。

Step Back Prompting

后退提示是一种用于增强语言模型的推理和问题解决能力的技巧,特别是LLMs。它涉及鼓励LLM从一个给定的问题或问题后退一步,提出一个更抽象、更高级的问题,涵盖原始查询的本质。

后退提示背后的概念是,许多复杂的问题或任务包含许多复杂的细节和约束,这使得LLMs难以直接检索和应用相关信息。

通过引入一个后退问题,这个问题通常更容易回答,并且围绕一个更广泛的概念或原则,LLMs 可以更有效地构建它们的推理。

Process of Step-Back Prompting

后退提示的典型过程包括两个主要步骤:

  • 抽象化:这时大型语言模型(LLM)不会立即尝试回答原始问题。相反,它会提出一个关于更大想法或规则的一般性问题。这有助于它思考和寻找事实。
  • 推理:在得到一般性问题的答案后,LLM使用这些信息来思考和回答原始问题。这称为“基于抽象的推理”。它使用来自更大想法的信息来对原始的、更难的问题给出好的答案。

Implementation with LangChain

提供一些少样本示例,这将展示后退提示是如何工作的。然后我们将转换这些示例信息。

# Few Shot Examples
examples = [{"input": "What is the birthplace of Albert Einstein?","output": "what is Albert Einstein's personal history?",},{"input": "Can a Tesla car drive itself?","output": "what can a Tesla car do?",},{"input": "Did Queen Elizabeth II ever visit Canada?","output": "what is Queen Elizabeth II's travel history?",},{"input": "Can a SpaceX rocket land itself?","output": "what can a SpaceX rocket do?",}
]
# We now transform these to example messages
example_prompt = ChatPromptTemplate.from_messages([("human", "{input}"),("ai", "{output}"),]
)
few_shot_prompt = FewShotChatMessagePromptTemplate(example_prompt=example_prompt,examples=examples,
)

系统消息为模型设置了上下文和任务。少样本示例(包含在 few_shot_prompt 中)用于为模型提供额外的上下文和如何执行任务的示例。用户消息是用户可以输入他们具体问题的地方,然后模型将尝试将其重新表述为一个更通用的后退问题。

prompt = ChatPromptTemplate.from_messages([("system","""You are an expert at world knowledge. Your task is to step back and paraphrase a question to a more generic step-back question, which is much easier to answer. Here are few examples:""",),# Few shot examplesfew_shot_prompt,# New question("user", "{question}"),]
)

现在,我们将为从LLM生成问题定义一个管道。它从预定义的对话模板(提示)开始,将对话传递给OpenAI GPT模型以生成具有固定随机性水平(温度=0)的响应,然后使用StrOutputParser处理并提取模型响应中的文本内容。最终结果应该是根据提示中的用户输入生成的问题。

question_gen = prompt | ChatOpenAI(temperature=0) | StrOutputParser()

 Ask the question.

question = "What is the name of the rover that NASA landed on Mars in 2021?"

现在,我们将调用 `question_gen` 函数,并将一个包含问题的字典作为输入传递给它。这个过程将使用这个问题作为输入,遵循定义的管道,并生成一个或多个问题作为结果。

question_gen.invoke({"question": question})

这将是调用函数后生成的答案。

what rovers has NASA sent to Mars?

我们将设置一个简单的界面来执行DuckDuckGo搜索,并指定最大结果数量,这将允许我们通过使用查询调用`retriever`函数来轻松检索搜索结果。

search = DuckDuckGoSearchAPIWrapper(max_results=4)def retriever(query):return search.run(query)

我们将使用 `question` 调用 `retriever` 函数。

retriever(question)

在检索问题之后,我们将得到以下答案。

Perseverance landed on Mars in February 2021. 

As of early February of this year, the rover had gathered 18 samples — and deposited half for a future potential return to Earth.

JPL-CALTECH/NASA The Curiosity rover team has been preparing for the start of the Solar Conjunction in November, when contact with all Mars spacecraft will be impossible for three weeks since Mars will be behind the Sun as seen from Earth.

NASA's Perseverance rover is busy just exploring Mars, looking for signs of ancient life.

Perseverance, nicknamed "Percy", the centerpiece of NASA's $2.7 billion Mars 2020 mission, touched down ... Since arriving at Jezero Crater in 2021, the six wheeled, nuclear-powered rover has been examining geologic features and collecting samples of the Red Planet that are central to the first step of the NASA-ESA (European Space Agency) Mars Sample Return campaign.

现在,我们将基于变量 `question` 生成问题,然后使用这些生成的问题作为搜索查询,通过 `retriever` 函数从DuckDuckGo检索搜索结果。

retriever(question_gen.invoke({"question": question}))

下面将是生成的答案。

From Wikipedia, the free encyclopedia This is a list of the 50 spacecraft missions (including unsuccessful ones) relating to the planet , such as orbiters and rovers.

Mission to Mars Gravity assist, destination elsewhere [1] [2] [3] ’1M No.1‘,’1M No.2‘,’1M No.2‘,’OKB-1 2MV-4 No.1‘,’2MV-4 No.1‘ Booster stage ("Block L") disintegrated in (2MV-4 No.2) NASA has sent a host of remotely-operated landers, orbiters and rovers to study Mars and bring back geologic samples.

While no humans have set foot on the planet, that could change.

NASA has ... The Subsurface Water Ice Mapping (SWIM), a NASA-funded project, has released its fourth and the more recent map of the prospective locations of subsurface water ice on Mars.

This, as per NASA officials, will help mission planners decide where exactly to send the first humans to Mars. 

The blue areas on this map of Mars are regions where NASA ... Thanks in part to NASA's Curiosity Mars rover, geologists have discovered evidence for ancient rivers on the Red Planet, suggesting the right conditions for life.

从LangChain中心,我们将拉取一个“langchain-ai/stepback-answer”模型。

response_prompt = hub.pull("langchain-ai/stepback-answer")

`chain` 是一系列数据转换和操作,从基于用户的问题或后退问题的上下文提取开始,接着使用预定义的对话上下文与LLM进行交互,最后将模型的响应解析成可读的格式。

chain = ({# Retrieve context using the normal question"normal_context": RunnableLambda(lambda x: x["question"]) | retriever,# Retrieve context using the step-back question"step_back_context": question_gen | retriever,# Pass on the question"question": lambda x: x["question"],}| response_prompt| ChatOpenAI(temperature=0)| StrOutputParser()
)

现在,我们将使用 `question` 调用 `chain` 函数。

chain.invoke({"question": question})

下面将是最合适的生成的答案。

The name of the rover that NASA landed on Mars in 2021 is Perseverance.

然后,我们将给模型一个响应模板。

response_prompt_template = """You are an expert of world knowledge. I am going to ask you a question. Your response should be comprehensive and not contradicted with the following context if they are relevant. Otherwise, ignore them if they are not relevant.{normal_context}Original Question: {question}
Answer:"""
response_prompt = ChatPromptTemplate.from_template(response_prompt_template)

再次,我们将使用 `chain` 函数通过正常的问题检索上下文,然后使用预定义的响应模板传递问题。

chain = ({# Retrieve context using the normal question (only the first 3 results)"normal_context": RunnableLambda(lambda x: x["question"]) | retriever,# Pass on the question"question": lambda x: x["question"],}| response_prompt| ChatOpenAI(temperature=0)| StrOutputParser()
)

使用 `question` 调用 `chain` 过程。

chain.invoke({"question": question})

下面将是生成的答案。

The name of the rover that NASA landed on Mars in 2021 is Perseverance, also known as "Percy".

It is the centerpiece of NASA's Mars 2020 mission, which successfully touched down on February 18, 2021, in Jezero Crater.

Perseverance is a six-wheeled, nuclear-powered rover that is currently exploring Mars and searching for signs of ancient life.

It is part of the NASA-ESA Mars Sample Return campaign and is tasked with collecting samples of the Red Planet for future analysis and potential return to Earth.

Conclusion

通过使用后退提示,大型语言模型可以减少在推理过程中犯错的概率,更有效地处理复杂任务,并提供对复杂问题更准确、更细致的回答。

这种技术有望提高语言模型在各种需要深入理解和复杂推理的领域和应用程序中的实际效用。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/525533.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数字化转型导师坚鹏:科技金融政策、案例及发展研究

科技金融政策、案例及发展研究 课程背景: 很多银行存在以下问题: 不清楚科技金融有哪些利好政策? 不知道科技金融有哪些成功案例? 不知道科技金融未来发展方向? 课程特色: 以案例的方式解读原创方…

多模态响应与功能集成!华中科技大学微型磁控胶囊机器人登上《Nature Communications》!

胶囊机器人可以通过口服方式抵达胃肠道病灶区域实施医疗功能,为实现胃肠道疾病无痛无创诊疗和提高患者依从性提供了重要途径。其中,磁控胶囊机器人技术因其驱动方式具有非接触、穿透性能好和控制模式丰富等优势而被认为是最理想的胃肠道疾病诊疗手段之一…

ai学习前瞻-python环境搭建

python环境搭建 Python环境搭建1. python的安装环境2. MiniConda安装3. pycharm安装4. Jupyter 工具安装5. conda搭建虚拟环境6. 安装python模块pip安装conda安装 7. 关联虚拟环境运行项目 Python环境搭建 1. python的安装环境 ​ python环境安装有4中方式。 从上图可以了解…

汽车级瞬态抑制TVS二极管优势特性及型号大全

汽车级瞬态抑制TVS二极管是一种高性能的防浪涌过电压电路保护元器件,能够在瞬态电压过高的情况下提供可靠的保护。它能够迅速响应并吸收过电压,将其导向地线,从而保护车辆的电子设备免受损坏。东沃汽车级TVS二极管具有以下几个关键优势&#…

118.龙芯2k1000-pmon(17)-制作ramdisk

目前手上这个设备装系统不容易,总是需要借助虚拟机才能实现。 对生产就不太那么友好,能否不用虚拟机就能装Linux系统呢? 主要是文件系统的问题需要解决,平时我们一般是用nfs挂载后,然后对硬盘格式化,之后…

c++版本回文质数 Prime Palindromes 题解(洛谷)

c语言中的小小白-CSDN博客c语言中的小小白关注算法,c,c语言,贪心算法,链表,mysql,动态规划,后端,线性回归,数据结构,排序算法领域.https://blog.csdn.net/bhbcdxb123?spm1001.2014.3001.5343 给大家分享一句我很喜欢我话: 知不足而奋进,望远山而前行&am…

人才推荐 | 毕业于美国凯斯西储大学的博士,专业知识广泛

编辑 / 木子 审核 / 朝阳 伟骅英才 伟骅英才致力于以大数据、区块链、AI人工智能等前沿技术打造开放的人力资本生态,用科技解决职业领域问题,提升行业数字化服务水平,提供创新型的产业与人才一体化服务的人力资源解决方案和示范平台&#x…

目标检测——监控下打架检测数据集

一、简述 首先,监控下打架检测是维护公共安全的重要手段。在公共场所、学校、监狱等地方,打架事件往往难以避免。通过安装打架检测监控系统,可以实时监控并准确识别打架事件,及时采取必要的应对措施,有效地减少打架事…

[数据集][目标检测]变电站缺陷检测数据集VOC+YOLO格式8307张17类别

数据集格式:Pascal VOC格式YOLO格式(不包含分割路径的txt文件,仅仅包含jpg图片以及对应的VOC格式xml文件和yolo格式txt文件) 图片数量(jpg文件个数):8307 标注数量(xml文件个数):8307 标注数量(txt文件个数):8307 标注…

Kubernetes 安全秘籍:5 个你必须知道的知识点

Kubernetes 安全和身份验证是确保集群和应用安全的关键。今天将深入探讨 Service Account、身份验证和RBAC的关键概念和实践,帮助您构建安全可靠的应用。今天本文将着重于安全相关的内容,并提供更详细的示例和配置说明,帮助兄弟们更深入地理解…

java-初级项目实战-(swing篇)-羊了个羊(动态交互,)

判断是否压牌 蓝框为b方格的宽度和高度的2倍, 判断t方格是否压住b方格,就要保证t方格的左上的黑点始终在蓝框内 即t的 x坐标(x1,x2)之间,y(y1,y2)之间 t方格的黑点设置为(x,y) x1的x即为b的左上点x-其宽度 x2的x即为b左上点x其宽度 y1的y即为b的左上点y-其高度 y2的y即为b的左上…

JVM-1

目录 1.基础知识 1.栈 2.本地方法栈 3.程序计数器 4.堆 5.方法区 6.JVM内存可见性 2.虚拟机类加载机制 1.加载 2.验证 3.准备 4.解析 5.初始化 6.使用 7.卸载 1.基础知识 JVM内存模型(5种):栈,本地方法栈&#xff…