Guidelines
原则一:编写清晰、具体的指令
Clear 不等于 short
策略一:使用分隔符清晰表示输入的不同部分
分隔符可以是:,"",<>,<tag>,<\tag> 等,也可以是别的自己看得懂的分隔符
可以使用分隔符将指令(Prompt)和具体内容分隔开,使用分隔符是避免提示注入[1]的一种有效方式
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)在这里将用户输入的内容使用 分隔开,最后以 {text} 加到 prompt 内
策略二:要求结构化的输出,比如 JSON 或者 HTML
第二个策略是要求生成一个结构化的输出,使得模型的输出更容易被我们所解析,在这里的提示词内,需要指明 LLM 需要生成的字段,需要指明,不要含糊
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)最后的输出结果有问题,它并没有按照要求,完全输出为 JSON 字段,而是生成了一些无关的数据
Here's a list of three made-up book titles in JSON format:
[
{
"book_id": 1,
"title": "The Whispering Shadows",
"author": "Eleanor Voss",
"genre": "Gothic Horror"
},
{
"book_id": 2,
"title": "Neon Mirage",
"author": "Darius Kane",
"genre": "Cyberpunk Thriller"
},
{
"book_id": 3,
"title": "The Last Alchemist of Prague",
"author": "Lina Kovac",
"genre": "Historical Fantasy"
}
]
Let me know if you'd like any modifications!一种比较好的解决方法是在 Prompt 里面强调不需要生成 JSON 字段以外的数据
prompt = f"""
Generate a list of three made-up book titles along \
with their authors and genres.
All you need to provide is json format and no other text.
Provide them in JSON format with the following keys:
book_id, title, author, genre.
"""
response = get_completion(prompt)
print(response)在这里只加上了 All you need to provide is json format and no other text.,效果很不错
[
{
"book_id": 1,
"title": "The Whispering Shadows",
"author": "Elena Voss",
"genre": "Mystery"
},
{
"book_id": 2,
"title": "Starlight Chronicles",
"author": "Marcus Ryle",
"genre": "Science Fiction"
},
{
"book_id": 3,
"title": "The Last Ember",
"author": "Clara Dunmore",
"genre": "Fantasy"
}
]策略三:要求模型检查是否满足条件
如果任务做出的假设不一定满足,我们可以告诉模型让他先检查,如果不满足,则不进行执行,满足后则执行,便于处理一些边缘条件,类似于编程中的条件判断
- 明确条件:在提示词中清晰定义需要检查的条件,说明哪些前提必须满足
- 分步指令:指导模型先验证条件,再根据结果决定是否执行任务
- 处理不满足情况:指定当条件不满足时,模型应如何响应(如返回提示信息或采取替代行动)
- 清晰输出:要求模型在输出中说明条件检查结果和后续操作
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)Completion for Text 1:
Here are the steps for making a cup of tea:
Step 1 - Get some water boiling.
Step 2 - Grab a cup and put a tea bag in it.
Step 3 - Once the water is hot enough, pour it over the tea bag.
Step 4 - Let it sit for a bit so the tea can steep.
Step 5 - After a few minutes, take out the tea bag.
Step 6 - If you like, add some sugar or milk to taste.
Step 7 - Enjoy your delicious cup of tea!下面是一种反例,在这个例子中,并没有任何分步的步骤,只是一段描述性的语句,由于我们在 Prompt 中让其进行检查,并且告知如果不满足的结果,因此 LLM 会对这种边缘条件进行处理
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)Completion for Text 2:
No steps provided.策略四:提供少量示例
在要求 LLM 实际执行前,给他少量成功执行任务的案例,类似于给一个模版
在下面的例子中,我们试图让 LLM 模拟语气,这种描述性东西是抽象的,比较好的解决方案是给一些实际的例子,让 LLM 用这个实际的例子来执行任务
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)<grandparent>: The bamboo bends low in the storm but never breaks, rising again when the wind has passed. The diamond is forged under great pressure, yet emerges brighter. Even the humblest weed finds its way through cracks in stone, teaching us that strength often wears the cloak of gentleness.原则二:给模型时间去思考
LLM 并不具备深入思考的能力,需要给它一些推理的指示,可以让 AI 在回答问题前,指示它们话更多的时间进行思考
策略一:指定完成任务所需的步骤
首先我们描述了杰克和吉尔的故事,并给出一个指令。该指令是执行以下操作。首先,用一句话概括三个反引号限定的文本。第二,将摘要翻译成法语。第三,在法语摘要中列出每个名称。第四,输出包含以下键的 JSON 对象:法语摘要和名称数。然后我们要用换行符分隔答案
text = 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)Completion for prompt 1:
1 - Siblings Jack and Jill embark on a quest to fetch water from a hilltop well, encounter a mishap but return home safely, their adventurous spirits intact.
2 - Les frères et sœurs Jack et Jill partent en quête d'eau d'un puits au sommet d'une colline, rencontrent un malheur mais rentrent chez eux sains et saufs, leur esprit d'aventure intact.
3 - Jack, Jill
4 -
{
"french_summary": "Les frères et sœurs Jack et Jill partent en quête d'eau d'un puits au sommet d'une colline, rencontrent un malheur mais rentrent chez eux sains et saufs, leur esprit d'aventure intact.",
"num_names": 2
}上文中的姓名有概率被替换成法语的,但是我们需要的其实是英文的,可以在 Prompt 中对其进行进一步的指示,这里很巧妙的使用了尖括号,在里面对要求 LLM 输出的数据进行了说明和限制
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 French summary>
Output JSON: <json with summary and num_names>
Text: <{text}>
"""
response = get_completion(prompt_2)
print("\nCompletion for prompt 2:")
print(response)策略二:指导模型在下结论之前找出自己的一个想法
在接下来的这个 Prompt 中,我们先要求 LLM 对问题进行思考,然后再将自己的思考结果和学生的解法进行对比,从而判断学生的解法是否正确
通过明确步骤,让 LLM 能有更多的思考时间,从而得到更正确的答案
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)Let x be the size of the installation in square feet.
Costs:
1. Land cost: $100 * x
2. Solar panel cost: $250 * x
3. Maintenance cost: $100,000 + $10 * x
Total cost: 100x + 250x + 100,000 + 10x = (100x + 250x + 10x) + 100,000 = 360x + 100,000
\```
Is the student's solution the same as actual solution just calculated:
\```
no
\```
Student grade:
\```
incorrect局限性
LLM 在训练的时候接收了大量的数据,但是它们并没有完全记住所见的信息,因此它并不清楚自己知识的边界,在回答一些问题时,它极有可能输出一些看似很合理,但其实是编造的信息,我们称之为幻觉[2]
例如在如下示例中,我们要求告诉我们 Boie 公司生产的 AeroGlide UltraSlim Smart Toothbrush 产品的信息,事实上,这个公司是真实存在的,但产品是编造的,模型则会一本正经地告诉我们编造的知识
prompt = f"""
Tell me about AeroGlide UltraSlim Smart Toothbrush by Boie
"""
response = get_completion(prompt)
print(response)
强制要求 LLM 先找到文本中的引用,要求它们按照引用来回答问题,这种回溯文档的方式对于减少幻觉非常有帮助