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

news2025/6/17 2:13:12

原文地址: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 examples
        few_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.coloradmin.cn/o/1503862.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

ES入门七:索引管理Api的使用

索引设置 在传家索引的时候,可以在“settings”字段中指定索引的位置。numbers_of_shards和nubmer_of_replicas 是索引非常重要的两个配置,设置它们值的示例如下: 我们还可以动态修改索引的位置,其示例如下:如上示例…

接收端编程、UDP编程练习、wireshrak抓包工具、UDP包头

我要成为嵌入式高手之3月6日Linux高编第十六天&#xff01;&#xff01; ———————————————————————————— 学习笔记 接收端 recvfrom #include <sys/types.h> #include <sys/socket.h> ssize_t recvfrom(int sockfd, void *buf, si…

AI绘画提示词案例(宠物

目录 1. 雪地猫猫&#xff1a;1.1 提示词&#xff1a;1.2 效果&#xff1a; 2. 趴地猫猫&#xff1a;2.1 提示词&#xff1a;2.2 效果&#xff1a; 3. 长城萨摩耶&#xff1a;3.1 提示词&#xff1a;3.2 效果&#xff1a; 4. 沙发猫猫&#xff1a;4.1 提示词&#xff1a;4.2 效…

2024年新手视频剪辑软件推荐-6款视频剪辑软件测评

视频剪辑软件推荐 premiere premiere 直达地址:各大软件网站 说到底,还是得专业的来,虽然很多人觉得他是收费的,但是你懂的,想要免费总是会有办法的.别的不说,剪辑这块,我还是很认可这个软件,虽然我现在还是刚入门. 剪映 剪映 抖音官方推出的一款手机视频编辑剪辑应用,提供切割…

opencv编程

opencv编程 引言&#xff1a; ​ 本实验旨在介绍使用OpenCV进行图像处理和视频处理的基本操作。OpenCV&#xff08;Open Source Computer Vision Library&#xff09;是一个开源计算机视觉库&#xff0c;提供了丰富的图像和视频处理功能&#xff0c;既可以进行图像的读取、显示…

猫头虎分享已解决Bug || 批处理错误:BatchJobFailure, ProcessingDelay

博主猫头虎的技术世界 &#x1f31f; 欢迎来到猫头虎的博客 — 探索技术的无限可能&#xff01; 专栏链接&#xff1a; &#x1f517; 精选专栏&#xff1a; 《面试题大全》 — 面试准备的宝典&#xff01;《IDEA开发秘籍》 — 提升你的IDEA技能&#xff01;《100天精通鸿蒙》 …

手机号验证码重新发送

前文叙述 很久以前做的一个 demo &#xff0c;纯 HTML 、CSS、js 制作&#xff0c;一定时间段之后才可以重新发送验证码&#xff0c;如 60s 后再次发送验证码&#xff0c;在该时间段内发送验证码按钮为禁用状态&#xff0c;实战开发过程也亦是同理&#xff0c;因此记录一手。 一…

【IEEE列表会议】IEEE第三届信息与通信工程国际会议国际会议(JCICE 2024)

会议简介 Brief Introduction 2024年第三届信息与通信工程国际会议国际会议 (JCICE 2024) 会议时间&#xff1a;2024年5月10日-12日 召开地点&#xff1a;中国福州 大会官网&#xff1a;JCICE 2024-2024 International Joint Conference on Information and Communication Engi…

王道机试C++第 4 章 字符串:字符串内容续写几个小程序 Day30

统计字符 习题描述 统计一个给定字符串中指定的字符出现的次数。 输入描述&#xff1a; 测试输入包含若干测试用例&#xff0c;每个测试用例包含2行&#xff0c;第1行为一个长度不超过5的字符串&#xff0c;第2行为一个长度不超过80的字符串。注意这里的字符串包含空格&…

Unity性能优化篇(十) 模型优化之网格合并 Easy Mesh Combine Tool插件使用以及代码实现网格合并

把多个模型的网格合并为一个网格。可以使用自己写代码&#xff0c;使用Unity自带的CombineMeshes方法&#xff0c;也可以使用资源商店的插件&#xff0c;在资源商店搜Mesh Combine可以搜索到相关的插件&#xff0c;例如Easy Mesh Combine Tool等插件。 可大幅度减少Batches数量…

零、自然语言处理开篇

目录 0、NLP任务的基础——符号向量化 0.0 词袋模型 0.1 查表/One-hot编码 0.2 词嵌入模型/预训练模型 0.2.0 Word2Vec &#xff08;0&#xff09;CBOW &#xff08;1&#xff09;Skip-gram 0.2.1 GloVe 0.2.2 WordPiece 0.2.3 BERT 0.2.4 ERNIE NLP自然语言处理&am…

微信小程序-入门

1.下载和安装Npm&#xff1a;Npm https://docs.npmjs.com/downloading-and-installing-node-js-and-npm 或者 https://nodejs.org/en/download/ 未安装npm 提示 以下以安装node安装包为例 按任意键继续 安装完成后 2. 下载和安装小程序开发工具 &#xff1a;https:/…

@EnableWebMvc介绍和使用详细demo

EnableWebMvc是什么 EnableWebMvc 是 Spring MVC 中的一个注解&#xff0c;它用于启用 Spring MVC 框架的基本功能&#xff0c;以便你可以使用 Spring MVC 提供的特性来处理 Web 请求。 通常情况下&#xff0c;在基于 Spring Boot 的应用中&#xff0c;并不需要显式地使用 Ena…

音视频开发_音频基础知识

如何采集声音——模数转换原理 声音模数转换是将声音信号从模拟形式转换为数字形式的过程。它是数字声音处理的基础&#xff0c;常用于语音识别、音频编码等应用中。 音视频通信流程 音视频采集&#xff1a;首先是从麦克风、摄像头等设备中采集音频和视频数据&#xff0c;将现…

免费下载Corel Video Studio 2024-轻松创建令人惊叹的视频!

免费下载Corel Video Studio 2024-轻松创建令人惊叹的视频&#xff01; Corel Video Studio 2024免费下载Keygen 你厌倦了在视频编辑软件上花大钱吗&#xff1f;别再看了&#xff01;我们为您提供了完美的解决方案——Corel Video Studio 2024。最棒的部分是什么&#xff1f;…

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

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

猫头虎分享已解决Bug || 数据中心断电:PowerLoss, DataCenterBlackout

博主猫头虎的技术世界 &#x1f31f; 欢迎来到猫头虎的博客 — 探索技术的无限可能&#xff01; 专栏链接&#xff1a; &#x1f517; 精选专栏&#xff1a; 《面试题大全》 — 面试准备的宝典&#xff01;《IDEA开发秘籍》 — 提升你的IDEA技能&#xff01;《100天精通鸿蒙》 …

存算一体成为突破算力瓶颈的关键技术?

大模型的训练和推理需要高性能的算力支持。以ChatGPT为例&#xff0c;据估算&#xff0c;在训练方面&#xff0c;1746亿参数的GPT-3模型大约需要375-625台8卡DGX A100服务器训练10天左右&#xff0c;对应A100 GPU数量约3000-5000张。 在推理方面&#xff0c;如果以A100 GPU单卡…

qsort函数的用法及参数的讲解

第一种用法展示&#xff1a;&#xff08;整形数组的qsort&#xff09; 一&#xff0c;qsort函数的定义&#xff1a; qsort 函数的定义&#xff1a;void qsort (void* base, size_t num, size_t size, int (*compar)(const void*,const void*)); 使用其需要包含头文件&#x…

浅述字典攻击

一、前言 字典攻击是一种常见的密码破解方法&#xff0c;它使用预先编制的字典文件作为攻击字典&#xff0c;通过尝试猜测密码的方式来破解密码。下面是一个关于字典攻击的博客&#xff0c;希望能够为您了解字典攻击提供帮助。 二、字典攻击概述 字典攻击是一种密码破解方法&…