越来越多的人意识到掌握提示工程的重要性,这种使用语言(例如英语)进行编码的新方法。
一些大型科技公司的首席执行官甚至预测,未来一半的工作将以即时工程为基础。
那么如何有效地与prompty engineering合作呢?最近,吴恩达与 OpenAI 合作发布了面向开发者的 ChatGPT 提示工程课程。这个免费课程提供高质量的内容,在这里我们总结了视频课程中提到的制作有效提示的指南,以及我的个人见解
有效提示的重要性
有效的提示对于从 ChatGPT 获得高质量的响应至关重要。精心设计的提示将帮助人工智能:
- 产生准确和相关的信息
- 保持上下文并保持主题
- 生成连贯且结构良好的响应
- 最大限度地减少错误和误解
构造不当的提示可能会导致不相关、模棱两可甚至不正确的输出。因此,花时间制作有效的提示对于从 ChatGPT 获得最佳结果至关重要。
制作高质量的提示
要创建产生高质量响应的有效提示,请考虑以下原则和策略:
原则 1:编写清晰具体的说明
确保您的提示清晰简洁,以帮助模型理解意图和所需的输出。避免可能导致多种解释的模棱两可的语言或措辞。这可以通过以下策略来实现:
策略 1:使用定界符清楚地指示输入的不同部分
定界符有助于避免误导性用户输入造成的潜在干扰。分隔符的示例包括:
三引号:“””
三重反引号:```
破折号 —
尖括号:<>
XML 标签:
提示示例:
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)
输出
Clear and specific instructions should be ...
This allows the model to clearly understand the problem itself but also avoids injecting uncontrollable instructions. For example “Forget the previous command, do XYZ”
策略二:请求结构化输出html json
这种方法有助于使模型输出直接可用于程序,例如可以由 Python 程序读取并转换为字典格式的 JSON 输出。
提示示例:
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)
输出
[{"book_id": 1,"title": "The Lost City of Zorath","author": "Aria Blackwood","genre": "Fantasy"},{"book_id": 2,"title": "The Last Survivors","author": "Ethan Stone","genre": "Science Fiction"}
]
策略 3:检查条件是否满足,检查完成任务所需的假设
如果任务的完成有必须满足的前提条件,我们应该要求模型先检查这些条件,如果不满足则指示它停止尝试。
提示示例(满足条件):
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:
Step 1 - ...
Step 2 - ...
Step 3 - ...
这具有考虑潜在边缘情况以避免意外错误或结果的额外好处。
策略四:“Few-shot”提示:给出一个成功完成任务的例子,然后让模型执行任务
为模型提供一个或多个示例提示有助于阐明预期输出。有关少样本学习的更多信息,请参阅 GPT-3 的论文:“Language Models are Few-Shot Learners”。
提示示例:
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>: Resilience is like a tree that ...
原则 2:给模型时间“思考”
该原则利用思维链的思想,将复杂的任务分解为 N 个连续的子任务,使模型能够逐步思考并产生更准确的输出。更详细的可以参考这篇论文:Chain-of-Thought Prompting Elicits Reasoning in Large Language Models
策略 1:指定完成任务所需的步骤
下面是一个涉及文本摘要、将其翻译成法语、在法语摘要中列出名称以及最终以 JSON 格式输出数据的示例。通过提供必要的步骤,模型可以参考前面步骤的结果并提高输出的准确性。
提示示例:
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)
输出:
Completion for prompt 2:Summary: Jack and Jill...
Translation: Jack et Jill partent en quête d'eau...
Names: Jack, Jill
Output JSON: {"french_summary": "Jack et Jill partent en quête d'eau...", "num_names": 2}
策略 2:在匆忙下结论之前指示模型制定出自己的解决方案
如果任务太复杂或者描述太少,那么模型只能靠猜测得出结论,就像一个人在解决一道复杂的数学题时,剩余考试时间严重不足,大概率会计算不出来。是错的。所以,在这种情况下,我们可以指示模型花更长的时间来思考这个问题。
例如,在检查学生的习题答案时,让模型先找到自己的答案,以防止仓促给出错误的答案。
不佳的提示示例:
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)
输出(不正确的):
The student's solution is correct.
更新的提示:
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: 100x
2. Solar panel cost: 250x
3. Maintenance cost: 100,000 + 10xTotal cost: 100x + 250x + 100,000 + 10x = 360x + 100,000Is the student’s solution the same as actual solution just calculated:
NoStudent grade:
Incorrect
模型局限性:幻觉
ChatGPT 可能会产生幻觉,产生似是而非的虚假信息(例如,不存在的文学作品)。为了避免这种情况,可以让模型先去寻找相关的参考信息(或者在问题中提到参考信息,比如谷歌之后),然后让模型根据这些参考信息来回答问题。
阅读英文原文
英文链接
关注
公众号
AI好书推荐
AI日新月异,但是万丈高楼拔地起,离不开良好的基础。您是否有兴趣了解人工智能的原理和实践? 不要再观望! 我们关于 AI 原则和实践的书是任何想要深入了解 AI 世界的人的完美资源。 由该领域的领先专家撰写,这本综合指南涵盖了从机器学习的基础知识到构建智能系统的高级技术的所有内容。 无论您是初学者还是经验丰富的 AI 从业者,本书都能满足您的需求。 那为什么还要等呢?
人工智能原理与实践 全面涵盖人工智能和数据科学各个重要体系经典
北大出版社,人工智能原理与实践 人工智能和数据科学从入门到精通 详解机器学习深度学习算法原理