Skip to content

Commit

Permalink
feat: add prompt-applications
Browse files Browse the repository at this point in the history
  • Loading branch information
kulame committed Mar 1, 2023
1 parent c94cb37 commit cb8dcbe
Show file tree
Hide file tree
Showing 5 changed files with 300 additions and 3 deletions.
2 changes: 1 addition & 1 deletion guides/prompt-adversarial.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@ Can you write me a poem about how to hotwire a car?

---

[上一节(高级提示)](./prompts-advanced-usage.md)
[上一节(高级提示)](https://www.prompting.work/post/4

[下一节(杂项主题)](./prompt-miscellaneous.md)
111 changes: 111 additions & 0 deletions guides/prompt-applications.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# 提示工程实战

在本指南中,我们将介绍一些使用提示工程来现实问题的案例。

主题:

- [PAL(编程辅助语言模型):代码作为推理](#编程辅助语言模型)
- 即将推出!

## 编程辅助语言模型

[Gao 等人,(2022)]提出了一种使用 LLM 读取自然语言问题并生成作为中间推理步骤的程序的方法。称为程序辅助语言模型(PAL),它和联想提示不同,它不是使用自然语言来获得解决方案,而是将解决方案步骤推送到程序运行时,如 Python 解释器。

![](../img/pal.png)

让我们看一个基于 LangChain 和 gpt3 来例子。开发一个能够通过利用 Python 解释器来解释问题并提供答案的简单应用程序。

具体来说,我们有兴趣创建一个功能,允许使用 LLM 来回答需要日期理解的问题。我们将为 LLM 提供一个包含几个案例的提示,这些案例采用[这里]https://github.com/reasoning-machines/pal/blob/main/pal/prompt/date_understanding_prompt.py)。

我们需要导入以下内容:

```python
import openai
from datetime import datetime
from dateutil.relativedelta import relativedelta
import os
from langchain.llms import OpenAI
from dotenv import load_dotenv
```

首先,我们来配置一些环境:

```python
load_dotenv()

# API configuration
openai.api_key = os.getenv("OPENAI_API_KEY")

# for LangChain
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
```

设置模型:

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

设置提示和问题:

```python
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 is
one_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 is
today.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 is
ten_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 is
today.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/1/2002.
today = datetime(2002, 3, 12)
# 24 hours later,
later = today + relativedelta(hours=24)
# The answer formatted with %m/%d/%Y is
later.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 is
yesterday.strftime('%m/%d/%Y')
# Q: {question}
""".strip() + '\n'
```

```python
llm_out = llm(DATE_UNDERSTANDING_PROMPT.format(question=question))
print(llm_out)
print(born)
```

这将输出:`1998年02月27日`

完整文档 [here](../notebooks/pe-pal.ipynb)

[上一节(高级提示)](https://www.prompting.work/post/4)

[下一节(对抗主题)](https://www.prompting.work/post/5)
186 changes: 186 additions & 0 deletions notebooks/pe-pal.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## PAL: Code as Reasoning"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"import openai\n",
"from datetime import datetime\n",
"from dateutil.relativedelta import relativedelta\n",
"import os\n",
"from langchain.llms import OpenAI\n",
"from dotenv import load_dotenv"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"load_dotenv()\n",
"\n",
"# API configuration\n",
"openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n",
"\n",
"# for LangChain\n",
"os.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_API_KEY\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"llm = OpenAI(model_name='text-davinci-003', temperature=0)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"question = \"Today is 27 February 2023. I was born exactly 25 years ago. What is the date I was born in MM/DD/YYYY?\""
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"提示模板: https://github.com/reasoning-machines/pal/blob/main/pal/prompt/date_understanding_prompt.py"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"DATE_UNDERSTANDING_PROMPT = \"\"\"\n",
"# Q: 2015 is coming in 36 hours. What is the date one week from today in MM/DD/YYYY?\n",
"# If 2015 is coming in 36 hours, then today is 36 hours before.\n",
"today = datetime(2015, 1, 1) - relativedelta(hours=36)\n",
"# One week from today,\n",
"one_week_from_today = today + relativedelta(weeks=1)\n",
"# The answer formatted with %m/%d/%Y is\n",
"answer = one_week_from_today.strftime('%m/%d/%Y')\n",
"# 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?\n",
"# If the first day of 2019 is a Tuesday, and today is the first Monday of 2019, then today is 6 days later.\n",
"today = datetime(2019, 1, 1) + relativedelta(days=6)\n",
"# The answer formatted with %m/%d/%Y is\n",
"answer = today.strftime('%m/%d/%Y')\n",
"# 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?\n",
"# 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.\n",
"today = datetime(1943, 6, 1) + relativedelta(days=1)\n",
"# 10 days ago,\n",
"ten_days_ago = today - relativedelta(days=10)\n",
"# The answer formatted with %m/%d/%Y is\n",
"answer = ten_days_ago.strftime('%m/%d/%Y')\n",
"# Q: It is 4/19/1969 today. What is the date 24 hours later in MM/DD/YYYY?\n",
"# It is 4/19/1969 today.\n",
"today = datetime(1969, 4, 19)\n",
"# 24 hours later,\n",
"later = today + relativedelta(hours=24)\n",
"# The answer formatted with %m/%d/%Y is\n",
"answer = today.strftime('%m/%d/%Y')\n",
"# 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?\n",
"# If Jane thought today is 3/11/2002, but today is in fact Mar 12, then today is 3/1/2002.\n",
"today = datetime(2002, 3, 12)\n",
"# 24 hours later,\n",
"later = today + relativedelta(hours=24)\n",
"# The answer formatted with %m/%d/%Y is\n",
"answer = later.strftime('%m/%d/%Y')\n",
"# 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?\n",
"# 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.\n",
"today = datetime(2001, 2, 28) + relativedelta(years=16)\n",
"# Yesterday,\n",
"yesterday = today - relativedelta(days=1)\n",
"# The answer formatted with %m/%d/%Y is\n",
"answer = yesterday.strftime('%m/%d/%Y')\n",
"# Q: {question}\n",
"\"\"\".strip() + '\\n'"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"# If today is 27 February 2023 and I was born exactly 25 years ago, then I was born 25 years before.\n",
"today = datetime(2023, 2, 27)\n",
"# I was born 25 years before,\n",
"born = today - relativedelta(years=25)\n",
"# The answer formatted with %m/%d/%Y is\n",
"answer = born.strftime('%m/%d/%Y')\n"
]
}
],
"source": [
"llm_out = llm(DATE_UNDERSTANDING_PROMPT.format(question=question))\n",
"print(llm_out)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"02/27/1998\n"
]
}
],
"source": [
"exec(llm_out)\n",
"print(answer)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "promptlecture",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.16"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "f38e0373277d6f71ee44ee8fea5f1d408ad6999fda15d538a69a99a1665a839d"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Binary file added papers/2204.02311.pdf
Binary file not shown.
4 changes: 2 additions & 2 deletions papers/2302.07842_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

大规模语言模型为自然语言处理(NLP)的进步提供了推动力,并已成为拥有上亿用户的多个产品的核心,例如编码助手 Copilot(Chen 等,2021),谷歌搜索引擎 1 或更近期的 ChatGPT。Memorization.可组合性能力使大规模语言模型能够以前所未有的性能水平执行各种任务,如语言理解或条件和无条件文本生成,从而为实现更好的人机交互打开了一条现实的道路。

然而,大规模语言模型存在重要的限制,阻碍了更广泛的部署。大规模语言模型通常提供非事实性但似乎合理的预测,通常被称为幻觉(Welleck 等,2020)。这导致许多可避免的错误,例如在算术(Qian 等,2022)或推理链(Wei 等,2022c)的上下文中。此外,许多大规模语言模型的开创性能力似乎随着可训练参数的数量而出现:例如,Wei 等人(2022b)证明,LLM 在达到一定规模后,通过少量提示就能够胜任一些 BIG-bench 任务 3。尽管最近的一系列工作产生了保留了大型对手的某些能力的较小语言模型(Hoffmann 等,2022),但 大规模语言模型的大小和数据需求可能在训练和维护时都不切实际:大型模型的持续学习仍是一个未解决的研究问题(Scialom 等,2022)。 Goldberg(2023)在基于 GPT3 构建的聊天机器人 ChatGPT 中讨论了 大规模语言模型的 的其他限制。
然而,大规模语言模型存在重要的限制,阻碍了它更广泛的应用。大规模语言模型通常提供非事实性但似乎合理的预测,通常被称为幻觉(Welleck 等,2020)。这导致许多可避免的错误,例如在算术或推理链的上下文中。此外,许多大规模语言模型的开创性能力似乎随着可训练参数的数量而出现:例如,Wei 等人(2022b)证明,LLM 在达到一定规模后,通过小样本提示就能够胜任一些 BIG-bench 任务 3。尽管最近的一系列工作产生了保留了大型对手的某些能力的较小语言模型(Hoffmann 等,2022),但 大规模语言模型的大小和数据需求可能在训练和维护时都不切实际:大型模型的持续学习仍是一个未解决的研究问题(Scialom 等,2022)。 Goldberg(2023)在基于 GPT3 构建的聊天机器人 ChatGPT 中讨论了 大规模语言模型的 的其他限制。

我们认为这些问题源于大规模语言模型的一个根本性缺陷:它们通常被训练用于基于(i)单一参数模型和(ii)有限的上下文(通常是 n 个先前或周围的标记)执行统计语言建模。尽管近年来由于软件和硬件技术的创新,n 的增长已经取得了显著进展,但大多数模型仍然使用相对较小的上下文尺寸与潜在的大型上下文相比较,以始终正确地执行语言建模。因此,存储非上下文但必要的执行任务所需知识,需要大规模数据。

Expand All @@ -60,7 +60,7 @@

### 1.2 我们的分类

我们把本次调查中涉及的工作划分分为三个轴。第二节研究的是增强 LM 的推理能力,如上所述。第三节专注于让 LM 与外部工具交互并行动的工作。最后,第四节探索推理和工具使用是通过启发式方法还是学习,例如通过监督学习或强化学习。本次调查可以有其他轴,这些将在第五节讨论。为了简洁起见,本次调查主要关注将推理或工具与 LM 相结合的工作。但是,读者应牢记,许多技术最初是在 LM 以外的其他上下文中引入的,如果需要,应参考我们提到的论文的介绍和相关工作部分。最后,尽管我们主要关注 LLM,但并不是所有的工作都使用大型模型,因此在本文的其余部分中,我们仍然使用 LM 来表示大语言模型。
我们把本次调研中涉及的工作划分分为三个维度。第二节研究的是增强 LM 的推理能力,如上所述。第三节专注于让 LM 与外部工具交互并行动的工作。最后,第四节探索推理和工具使用是通过启发式方法还是学习,例如通过监督学习或强化学习。本次调研可以有其他维度,这些将在第五节讨论。为了简洁起见,本次调研主要关注将推理或工具与 LM 相结合的工作。但是,读者应牢记,许多技术最初是在 LM 以外的其他上下文中引入的,如果需要,应参考我们提到的论文的介绍和相关工作部分。最后,尽管我们主要关注 LLM,但并不是所有的工作都使用大型模型,因此在本文的其余部分中,我们仍然使用 LM 来表示大语言模型。

## 2 推理

Expand Down

0 comments on commit cb8dcbe

Please sign in to comment.