【Prompt Engineering提示:Active-Prompt、方向性刺激提示、PAL(程序辅助语言模型)】

本文涉及的产品
NLP 自学习平台,3个模型定制额度 1个月
NLP自然语言处理_高级版,每接口累计50万次
视觉智能开放平台,图像资源包5000点
简介: Diao等人(2023)提出了一种名为Active-Prompt的新方法,通过自适应提示来优化大型语言模型(LLMs)在特定任务中的表现。此方法通过不确定性评估选择需标注的问题,利用少量人工标注的思维链(CoT)示例逐步优化模型,提高其解决问题的能力。相比固定范例,Active-Prompt能够更有效地针对不同任务调整提示,从而提升模型性能。

Active-Prompt

思维链(CoT)方法依赖于一组固定的人工注释范例。问题在于,这些范例可能不是不同任务的最有效示例。为了解决这个问题,Diao 等人(2023)(opens in a new tab)最近提出了一种新的提示方法,称为 Active-Prompt,以适应 LLMs 到不同的任务特定示例提示(用人类设计的 CoT 推理进行注释)。

下面是该方法的说明。第一步是使用或不使用少量 CoT 示例查询 LLM。对一组训练问题生成 k 个可能的答案。基于 k 个答案计算不确定度度量(使用不一致性)。选择最不确定的问题由人类进行注释。然后使用新的注释范例来推断每个问题。 image.gif 编辑

图片来源:Diao等人(2023)

方向性刺激提示

Li 等人,(2023)(opens in a new tab)提出了一种新的提示技术,以更好地指导 LLM 生成所需的摘要。

训练了一个可调节的策略 LM 来生成刺激/提示。越来越多地使用RL来优化 LLM。

下图显示了方向性刺激提示与标准提示的比较。策略 LM 可以很小,并且可以优化以生成指导黑盒冻结 LLM 的提示。 image.gif 编辑

图片来源:Li 等人,(2023)

PAL(程序辅助语言模型)

Gao 等人(2022)(opens in a new tab)提出了一种使用 LLMs 读取自然语言问题并生成程序作为中间推理步骤的方法。被称为程序辅助语言模型(PAL),它与思维链提示不同,因为它不是使用自由形式文本来获得解决方案,而是将解决步骤卸载到类似 Python 解释器的编程运行时中。 image.gif 编辑

图片来源:Gao 等人(2022)(opens in a new tab)

让我们以 LangChain 和 OpenAI GPT-3 为例。我们有兴趣开发一个简单的应用程序,它能够解释所提出的问题,并利用 Python 解释器提供答案。

具体来说,我们有兴趣创建一个功能,允许使用 LLM 回答需要日期理解的问题。我们将为 LLM 提供一个提示,其中包括一些示例,这些示例是从这里(opens in a new tab)采用的。

这是我们需要导入的包:

import openaifrom datetime import datetimefrom dateutil.relativedelta import relativedeltaimport osfrom langchain.llms import OpenAIfrom dotenv import load_dotenv

image.gif

让我们先配置一些环境:

load_dotenv() # API configurationopenai.api_key = os.getenv("OPENAI_API_KEY") # for LangChainos.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")

image.gif

设置模型实例:

llm = OpenAI(model_name='text-davinci-003', temperature=0)

image.gif

设置提示+问题:

question = "Today is 27 February 2023. I was born exactly 25 years ago. What is the date I was born in MM/DD/YYYY?" DATE_UNDERSTANDING_PROMPT = """# Q: 2015 is coming in 36 hours. What is the date one week from today in MM/DD/YYYY?# If 2015 is coming in 36 hours, then today is 36 hours before.today = datetime(2015, 1, 1) - relativedelta(hours=36)# One week from today,one_week_from_today = today + relativedelta(weeks=1)# The answer formatted with %m/%d/%Y isone_week_from_today.strftime('%m/%d/%Y')# Q: The first day of 2019 is a Tuesday, and today is the first Monday of 2019. What is the date today in MM/DD/YYYY?# If the first day of 2019 is a Tuesday, and today is the first Monday of 2019, then today is 6 days later.today = datetime(2019, 1, 1) + relativedelta(days=6)# The answer formatted with %m/%d/%Y istoday.strftime('%m/%d/%Y')# Q: The concert was scheduled to be on 06/01/1943, but was delayed by one day to today. What is the date 10 days ago in MM/DD/YYYY?# If the concert was scheduled to be on 06/01/1943, but was delayed by one day to today, then today is one day later.today = datetime(1943, 6, 1) + relativedelta(days=1)# 10 days ago,ten_days_ago = today - relativedelta(days=10)# The answer formatted with %m/%d/%Y isten_days_ago.strftime('%m/%d/%Y')# Q: It is 4/19/1969 today. What is the date 24 hours later in MM/DD/YYYY?# It is 4/19/1969 today.today = datetime(1969, 4, 19)# 24 hours later,later = today + relativedelta(hours=24)# The answer formatted with %m/%d/%Y istoday.strftime('%m/%d/%Y')# Q: Jane thought today is 3/11/2002, but today is in fact Mar 12, which is 1 day later. What is the date 24 hours later in MM/DD/YYYY?# If Jane thought today is 3/11/2002, but today is in fact Mar 12, then today is 3/12/2002.today = datetime(2002, 3, 12)# 24 hours later,later = today + relativedelta(hours=24)# The answer formatted with %m/%d/%Y islater.strftime('%m/%d/%Y')# Q: Jane was born on the last day of Feburary in 2001. Today is her 16-year-old birthday. What is the date yesterday in MM/DD/YYYY?# If Jane was born on the last day of Feburary in 2001 and today is her 16-year-old birthday, then today is 16 years later.today = datetime(2001, 2, 28) + relativedelta(years=16)# Yesterday,yesterday = today - relativedelta(days=1)# The answer formatted with %m/%d/%Y isyesterday.strftime('%m/%d/%Y')# Q: {question}""".strip() + '\n'

image.gif

llm_out = llm(DATE_UNDERSTANDING_PROMPT.format(question=question))print(llm_out)

image.gif

这将输出以下内容:

# If today is 27 February 2023 and I was born exactly 25 years ago, then I was born 25 years before.today = datetime(2023, 2, 27)# I was born 25 years before,born = today - relativedelta(years=25)# The answer formatted with %m/%d/%Y isborn.strftime('%m/%d/%Y')

image.gif

llm_out 是一段 python 代码,我们可以使用 exec 执行它:

exec(llm_out)print(born)

image.gif

这将输出以下内容:02/27/1998

相关文章
|
5月前
|
人工智能 自然语言处理 搜索推荐
揭秘ChatGPT的Prompt方法:原理与应用总结
揭秘ChatGPT的Prompt方法:原理与应用总结
108 0
|
5月前
|
自然语言处理
ERNIE-Bot 4.0提示词原则与提示词格式
ERNIE-Bot 4.0提示词原则与提示词格式
56 0
|
5月前
|
机器学习/深度学习 人工智能 JSON
LLM 大模型学习必知必会系列(二):提示词工程-Prompt Engineering 以及实战闯关
LLM 大模型学习必知必会系列(二):提示词工程-Prompt Engineering 以及实战闯关
LLM 大模型学习必知必会系列(二):提示词工程-Prompt Engineering 以及实战闯关
|
23天前
|
搜索推荐 UED
【Prompt Engineering:自我一致性、生成知识提示、链式提示】
自我一致性是提示工程技术之一,旨在改进链式思维提示中的解码方法。通过少样本CoT采样多个推理路径并选择最一致的答案,有助于提升涉及算术和常识推理任务的性能。例如,在解决年龄相关问题时,通过多次采样并挑选多数答案来提高准确性。此外,生成知识提示技术可预先生成相关信息辅助模型做出更准确预测,进一步优化模型表现。链式提示则通过将复杂任务分解为多个子任务来逐步处理,从而提高模型的透明度和可靠性,便于定位和改进问题。
【Prompt Engineering:自我一致性、生成知识提示、链式提示】
|
3月前
|
人工智能 搜索推荐
Prompt工程问题之prompt中要求详细的输出内容如何解决
Prompt工程问题之prompt中要求详细的输出内容如何解决
36 4
|
3月前
|
人工智能
Prompt工程问题之通过prompt使AI输出的语言风格多变如何解决
Prompt工程问题之通过prompt使AI输出的语言风格多变如何解决
42 4
|
3月前
|
人工智能 自然语言处理
Prompt工程问题之AI Prompt对prompt的帮助优化如何解决
Prompt工程问题之AI Prompt对prompt的帮助优化如何解决
32 0
|
3月前
|
弹性计算 运维 自然语言处理
Prompt工程问题之prompt工程的语言选定如何解决
Prompt工程问题之prompt工程的语言选定如何解决
28 0
|
5月前
|
人工智能
玩弄GPTs:人人都会的Prompt模板
玩弄GPTs:人人都会的Prompt模板
76 2
|
5月前
|
人工智能 安全 机器人
Prompt工程全攻略:15+Prompt框架一网打尽(BROKE、COAST、LangGPT)、学会提示词让大模型更高效
Prompt工程全攻略:15+Prompt框架一网打尽(BROKE、COAST、LangGPT)、学会提示词让大模型更高效
Prompt工程全攻略:15+Prompt框架一网打尽(BROKE、COAST、LangGPT)、学会提示词让大模型更高效