不久前,吴恩达和OpenAI合作开发了一套针对ChatGPT提示工程的最佳实践课程,课程名为《ChatGPT Prompt Engineering for Developers》。

这门课程时长为1个小时,内容简略易懂,还供给了实践的环境。要害是,课程免费,并且讲师是吴恩达(Andrew Ng,DeepLearning.AI创始人)和伊莎富尔福德(Isa Fulford,OpenAI的技术人员),含金量非常高。

ChatGPT提示工程Prompt两个关键原则 - 吴恩达和OpenAI合作

本文将要介绍的是编写Prompt的两个要害准则,非常非常重要,一定要把握。

毫不夸大的说,只需你把握并灵敏的去应用这两个准则,你就已经超过了大部分人编写Prompt的水平。

编写Prompt的两个要害准则

准则一:编写清晰、具体的阐明

准则二:给予模型考虑的时刻

(什么,太简略了?没关系,下面会一步步分解并举例阐明。示例内容有点长,主张打开电脑,耐性学习并花点时刻去实践,值得!)

准则一:编写清晰、具体的阐明

你应该经过供给尽可能清晰和具体的阐明(instructions)来表达您希望模型做什么。这将引导模型朝着你希望的方向输出内容,并下降收到无关或过错回复的可能性。 请不要将编写清晰的提示(prompt)与编写简略的提示(prompt)混淆,因为在许多情况下,更长的提示(prompt)实际上会为模型供给更多的清晰度和上下文,然后可能获得更具体和相关的输出。

战略1:运用分隔符清晰地标识出输入的各个部分

分隔符可以是任何清晰的标点符号,用来将特定文本片段与提示的其余部分分隔。这些标点符号可以是三个反引号、引号、XML标签、章节标题等,只需可以让模型清晰地知道这是一个独立的部分即可。

三个双引号:"""
三个反引号: ```
三个破折号: ---
尖括号: <>
XML标签: <tag></tag>

输入Prompt提示,让ChatGPT将用三个反引号分隔的文本总结成一个句子:

Summarize the text delimited by triple backticks into a single sentence.
```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.```

输出成果:

To guide a model towards the desired output and reduce irrelevant or incorrect responses, it is important to provide clear and specific instructions, which may require longer prompts for more clarity and context.

战略2:提示模型以结构化格局输出成果

经过清晰提示模型,以JSON、HTML等结构化格局输出成果。

输入Prompt提示,让ChatGPT生成一个由三个虚拟的书名及其作者和类型组成的列表,并以JSON格局和给定字段输出成果:

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.

输出成果:

[  {    "book_id": 1,    "title": "Whispers of the Forgotten Realm",    "author": "Aria Ravenswood",    "genre": "Fantasy"  },  {    "book_id": 2,    "title": "The Quantum Paradox",    "author": "Dr. Maxwell Sterling",    "genre": "Science Fiction"  },  {    "book_id": 3,    "title": "A Symphony of Shadows",    "author": "Lilith Blackwood",    "genre": "Gothic Romance"  }]

战略3:让模型检查是否满意条件

输入Prompt提示,让ChatGPT剖析文本内容,如果文本包含一个指令序列,则以指定格局重写这些指令。不然,输出“没有供给过程”。

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."
"""
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.
"""

输出成果

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 - Add some sugar or milk to taste.
Step 7 - Enjoy your delicious cup of tea.

输入Prompt提示,剖析另一段文本内容:

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."
"""
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.
"""

输出成果

No steps provided.

战略4:少样本提示

少样本提示(Few-shot prompting),是指供给1个或多个示例,让ChatGPT学习以到达我们希望的作用。

输入Prompt提示,让ChatGPT以与示例共同的风格来答复。示例内容是,孩子问什么是耐性,然后祖父母用比方的方法来答复:

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.

输出成果:

<grandparent>: Resilience is like a tree that bends with the wind but does not break. It is the ability to recover from difficult situations and keep moving forward. Just like a tree that endures storms and still stands tall, resilience comes from within and helps us face life's challenges with strength and perseverance.

答复作用非常好。

准则二:给予模型考虑的时刻

战略1:清晰阐明完成任务所需的过程

输入Prompt提示,让ChatGPT依照指定的过程来答复。先总结文本,然后翻译成法语,最终总结指定内容并以JSON格局输出:

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:
```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.```

输出成果:

Two siblings, Jack and Jill, go on an adventure to fetch water from a well on a hill, but misfortune strikes and they both tumble down the hill, causing them to return home slightly battered but with their adventurous spirits undimmed.
Deux frres et surs, Jack et Jill, vont chercher de l'eau d'un puits sur une colline, mais ils chutent tous les deux et retournent chez eux lgrement meurtris mais avec leurs esprits d'aventure intacts. 
Jack, Jill. 
{
  "french_summary": "Deux frres et surs, Jack et Jill, vont chercher de l'eau d'un puits sur une colline, mais ils chutent tous les deux et retournent chez eux lgrement meurtris mais avec leurs esprits d'aventure intacts.",
  "num_names": 2
}

要求以另一种指定的格局输出:

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: <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.>

输出成果:

Summary: Jack and Jill go on a quest to fetch water, but while climbing a hill, Jack falls and both siblings tumble down, returning home slightly injured but with undimmed adventurous spirits.
Translation: Jack et Jill partent chercher de l'eau, mais Jack tombe et ils descendent tous les deux la colline, rentrant chez eux lgrement blesss mais avec des esprits aventureux indomptables.
Names: Jack, Jill
Output JSON: {"french_summary": "Jack et Jill partent chercher de l'eau, mais Jack tombe et ils descendent tous les deux la colline, rentrant chez eux lgrement blesss mais avec des esprits aventureux indomptables.", "num_names": 2}

战略2:提示模型不要匆忙得出定论,而是要找出自己的处理方案

输入Prompt提示,让ChatGPT判断学生回答财政问题的答案是否正确:

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

输出成果:

The student's solution is correct.

实际上,以上答案是过错的。

我们可以经过提示模型首要找出自己的处理方案,然后将ChatGPT的处理方案与学生的处理方案进行比较,并评价学生的处理方案是否正确来处理这个问题:

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:

输出成果:

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 + 10x
Total cost: 100x + 250x + 100,000 + 10x = 360x + 100,000
Is the student's solution the same as actual solution just calculated:
No
Student grade:
Incorrect

这次得出了正确的定论,学生的答案是过错的。

最终

引用吴恩达的一句话:

生成式人工智能为人工智能工程师供给了许多时机,让他们可以在几分钟或几小时内构建强壮的应用程序,而以前需要几天或几周的时刻。我很高兴分享这些最佳实践,以使更多的人可以使用这些革命性的新功能。

参阅

www.deeplearning.ai/short-cours…

learn.deeplearning.ai/chatgpt-pro…

关于我

公众号:AI技术前沿

公众号同步发布:ChatGPT提示工程的两个要害准则 – 吴恩达和OpenAI合作(珍藏版)