今天我学习了DeepLearning.AI的 Prompt Engineering 的在线课程,我想和大家一起分享一下该门课程的一些主要内容。
下面是我们访问大型语言模(LLM)的主要代码:
import openai
openai.api_key ='XXXXXXXXX'
def get_completion(prompt, model="gpt-3.5-turbo"):
messages = [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0,
)
return response.choices[0].message["content"]
原则1:写出清晰和具体的说明(Write clear and specific instructions)
策略 1:使用分隔符清楚地指示输入的不同部分
分隔符可以是:```, """, < >, <tag> </tag>, :
对策略1的说明:
策略1的意思是:如果我们需要chatgpt完成某个特定任务,那么我们要在prompt里面明确的告诉ChatGPT需要做的事情是什么以及所需要的素材的位置。比如我们要chatGPT对一段文本内容做一个总结或摘要,那么我们就需要在prompt中指明:1.需要gpt完成的任务是什么, 2. 任务所需的素材在哪里。
下面的代码演示的是让gpt对一段特定文本内容做摘要, 在定义prompt时我们需要用特定的分隔符来分隔需要做摘要的文本内容,这样做的目的是让gpt知道文本内容所在位置。
text = f"""
You should express what you want a model to do by \
providing instructions that are as clear and \
specific as you can possibly make them. \
This will guide the model towards the desired output, \
and reduce the chances of receiving irrelevant \
or incorrect responses. Don't confuse writing a \
clear prompt with writing a short prompt. \
In many cases, longer prompts provide more clarity \
and context for the model, which can lead to \
more detailed and relevant outputs.
"""
prompt = f"""
Summarize the text delimited by triple backticks \
into a single sentence.
```{text}```
"""
response = get_completion(prompt)
print(response)
?策略 2:可以要求GPT给出一个结果化的结果
prompt = f"""
Generate a list of three made-up book titles along \
with their authors and genres.
Provide them in JSON format with the following keys:
book_id, title, author, genre.
"""
response = get_completion(prompt)
print(response)
?策略3:要求模型检查条件是否满足
这里我们要求gpt去检查一段文本的内容是否满足特定的条件(步骤),如果满足则按条件输出符合条件的内容,如果不满足条件则输出:不符合条件。
text_1 = f"""
Making a cup of tea is easy! First, you need to get some \
water boiling. While that's happening, \
grab a cup and put a tea bag in it. Once the water is \
hot enough, just pour it over the tea bag. \
Let it sit for a bit so the tea can steep. After a \
few minutes, take out the tea bag. If you \
like, you can add some sugar or milk to taste. \
And that's it! You've got yourself a delicious \
cup of tea to enjoy.
"""
prompt = f"""
You will be provided with text delimited by triple quotes.
If it contains a sequence of instructions, \
re-write those instructions in the following format:
Step 1 - ...
Step 2 - …
…
Step N - …
If the text does not contain a sequence of instructions, \
then simply write \"No steps provided.\"
\"\"\"{text_1}\"\"\"
"""
response = get_completion(prompt)
print("Completion for Text 1:")
print(response)
text_2 = f"""
The sun is shining brightly today, and the birds are \
singing. It's a beautiful day to go for a \
walk in the park. The flowers are blooming, and the \
trees are swaying gently in the breeze. People \
are out and about, enjoying the lovely weather. \
Some are having picnics, while others are playing \
games or simply relaxing on the grass. It's a \
perfect day to spend time outdoors and appreciate the \
beauty of nature.
"""
prompt = f"""
You will be provided with text delimited by triple quotes.
If it contains a sequence of instructions, \
re-write those instructions in the following format:
Step 1 - ...
Step 2 - …
…
Step N - …
If the text does not contain a sequence of instructions, \
then simply write \"No steps provided.\"
\"\"\"{text_2}\"\"\"
"""
response = get_completion(prompt)
print("Completion for Text 2:")
print(response)
?上述两个prompt中第一个prompt的内容符合特定的条件,所以结果按条件输出结果,第二个prompt不符合条件,所以输出:?No steps provided.
策略 4:“Few-shot”提示词
prompt = f"""
Your task is to answer in a consistent style.
<child>: Teach me about patience.
<grandparent>: The river that carves the deepest \
valley flows from a modest spring; the \
grandest symphony originates from a single note; \
the most intricate tapestry begins with a solitary thread.
<child>: Teach me about resilience.
"""
response = get_completion(prompt)
print(response)
上述的prompt要求ChatGPT以一致的风格来回答问题,在prompt中给出来child和grandparent的一轮对话内容作为一个例子,这样做的目的是让ChatGPT明白所谓的“consistent style”是怎么一回事,prompt的最后是child的一个问题,但是没有grandparent的回答。这是让ChatGPT明白要做的事情是:充当grandparent角色来回答child的最后一个问题,并且风格要和前面的grandparent的风格保存一致。
下面是ChatGPT返回的结果:
策略 1:指定完成任务所需的步骤
这里我们会给模型一段文本,然后要求模型按指定的步骤来完成一个任务:
执行以下操作:
1 - 用 1 个句子总结以下用三重反引号分隔的文本。
2 - 将摘要翻译成法语。
3 - 在法语摘要中列出每个名字。
4 - 输出包含以下键的 json 对象:french_summary、num_names。
用换行符分隔你的答案。
ext = f"""
In a charming village, siblings Jack and Jill set out on \
a quest to fetch water from a hilltop \
well. As they climbed, singing joyfully, misfortune \
struck—Jack tripped on a stone and tumbled \
down the hill, with Jill following suit. \
Though slightly battered, the pair returned home to \
comforting embraces. Despite the mishap, \
their adventurous spirits remained undimmed, and they \
continued exploring with delight.
"""
# example 1
prompt_1 = f"""
Perform the following actions:
1 - Summarize the following text delimited by triple \
backticks with 1 sentence.
2 - Translate the summary into French.
3 - List each name in the French summary.
4 - Output a json object that contains the following \
keys: french_summary, num_names.
Separate your answers with line breaks.
Text:
```{text}```
"""
response = get_completion(prompt_1)
print("Completion for prompt 1:")
print(response)
?要求指定格式的输出
这里我们要求ChatGPT按指定的格式来输出内容,在prompt中的内容分成了2部分,上半部分告诉ChatGPT需要做哪几件事情,下半部分输出的格式:
prompt_2 = f"""
Your task is to perform the following actions:
1 - Summarize the following text delimited by
<> with 1 sentence.
2 - Translate the summary into French.
3 - List each name in the French summary.
4 - Output a json object that contains the
following keys: french_summary, num_names.
Use the following format:
Text: <text to summarize>
Summary: <summary>
Translation: <summary translation>
Names: <list of names in Italian summary>
Output JSON: <json with summary and num_names>
Text: <{text}>
"""
response = get_completion(prompt_2)
print("\nCompletion for prompt 2:")
print(response)
?策略 2:在匆忙下结论之前指示模型自己制定解决方案
这里在prompt中给ChatGPT出了一道学生做的简单的数学应用题,希望ChatGPT来判断学生的答案是否正确。对于ChatGPT给出的答案我们发现,它并没有仔细的去亲自去做这道题目,结果给出了一个错误的答案。从中我们发现,当prompt中的问题的逻辑比较复杂的时候,我们直接了当的要求ChatGPT给出一个答案时候,ChatGPT也会像人类一样经常犯错误。下面我们先将prompt翻译一下:
prompt = f"""
Determine if the student's solution is correct or not.
Question:
I'm building a solar power installation and I need \
help working out the financials.
- Land costs $100 / square foot
- I can buy solar panels for $250 / square foot
- I negotiated a contract for maintenance that will cost \
me a flat $100k per year, and an additional $10 / square \
foot
What is the total cost for the first year of operations
as a function of the number of square feet.
Student's Solution:
Let x be the size of the installation in square feet.
Costs:
1. Land cost: 100x
2. Solar panel cost: 250x
3. Maintenance cost: 100,000 + 100x
Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000
"""
response = get_completion(prompt)
print(response)
?
请注意,该学生的答案实际上是不正确的。
我们可以通过指示ChatGPT首先计算出自己的答案再来判断学生的答案是否正确。因此需要改写一下prompt,我们需要把求解这道应用题的详细步骤告诉ChatGPT:
接下来给出实现这些步骤的具体格式:
prompt = f"""
Your task is to determine if the student's solution \
is correct or not.
To solve the problem do the following:
- First, work out your own solution to the problem.
- Then compare your solution to the student's solution \
and evaluate if the student's solution is correct or not.
Don't decide if the student's solution is correct until
you have done the problem yourself.
Use the following format:
Question:
```
question here
```
Student's solution:
```
student's solution here
```
Actual solution:
```
steps to work out the solution and your solution here
```
Is the student's solution the same as actual solution \
just calculated:
```
yes or no
```
Student grade:
```
correct or incorrect
```
Question:
```
I'm building a solar power installation and I need help \
working out the financials.
- Land costs $100 / square foot
- I can buy solar panels for $250 / square foot
- I negotiated a contract for maintenance that will cost \
me a flat $100k per year, and an additional $10 / square \
foot
What is the total cost for the first year of operations \
as a function of the number of square feet.
```
Student's solution:
```
Let x be the size of the installation in square feet.
Costs:
1. Land cost: 100x
2. Solar panel cost: 250x
3. Maintenance cost: 100,000 + 100x
Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000
```
Actual solution:
"""
response = get_completion(prompt)
print(response)
?这里我们可以发现ChatGPT的实际输出是从“Use the following format:” 的“Actual solution:”开始的,并没有将“Question:”和“Student's solution:”这两部分内容输出。这表明ChatGPT能够理解人类的意图,因为“Question:”和“Student's solution:”部分在题目中已经出现,无需在重复输出,只需输出“Actual solution:”部分以后的内容即可。最后ChatGPT通过比对自己的答案和学生的答案后给出了正确的答案。
当ChatGPT不知道问题答案时候,ChatGPT往往不会承认自己不知道,因此GPT往往会在不知道正确答案的时候会给出一个貌似正确的错误答案。
下面我们在prompt中要求ChatGPT给出Boie这家公司的电动牙刷的相关信息,这里的 Boie 是一家真实的公司,产品名称不是真实的。
prompt = f"""
Tell me about AeroGlide UltraSlim Smart Toothbrush by Boie
"""
response = get_completion(prompt)
print(response)
?这里我们发现ChatGPT对一个Boie公司不存在的产品说了一大堆的内容,好像这个产品是真实存在似的。