forked from prompting-work/Prompt-Engineering-Guide-Cn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
300 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# 提示工程实战 | ||
|
||
在本指南中,我们将介绍一些使用提示工程来现实问题的案例。 | ||
|
||
主题: | ||
|
||
- [PAL(编程辅助语言模型):代码作为推理](#编程辅助语言模型) | ||
- 即将推出! | ||
|
||
## 编程辅助语言模型 | ||
|
||
[Gao 等人,(2022)]提出了一种使用 LLM 读取自然语言问题并生成作为中间推理步骤的程序的方法。称为程序辅助语言模型(PAL),它和联想提示不同,它不是使用自然语言来获得解决方案,而是将解决方案步骤推送到程序运行时,如 Python 解释器。 | ||
|
||
 | ||
|
||
让我们看一个基于 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters