在现代人工智能开发中,编写Prompt(提示)是与AI模型互动的重要一环。但如何编写Prompt以确保模型能够准确理解你的意图并提供有用的回应,却是一门艺术。本文将分享两个关键原则,这些原则将帮助你更好地编写Prompt,让你的AI交互更加高效。
原则一:编写清晰、具体的说明
原则一:编写清晰、具体的说明,这是编写Prompt时不可或缺的原则之一。为什么这么重要呢?因为只有当你提供清晰、具体的指令时,AI才能够准确理解你的需求,从而提供有针对性的回应。
策略1:使用分隔符清晰地标识出输入的各个部分
为了让模型明确知道哪些部分是关键信息,你可以使用分隔符来标识输入的各个部分。这些分隔符可以是三个反引号、引号、XML标签等,只要能够让模型明确地知道这是一个独立的部分即可。以下是一个示例:
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.
这样的分隔符可以帮助模型更好地理解你的指令,从而提供更准确的回应。
### 策略2:提示模型以结构化格式输出结果
有时,你可能需要模型以特定的结构化格式输出结果,比如JSON、HTML等。在这种情况下,你可以明确要求模型按照特定格式提供回应。以下是一个示例:
```markdown
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.</code></pre>
<p>这样的指令可以帮助模型生成符合你要求的结构化数据,使得结果更易于处理和理解。</p>
<h3>策略3:让模型检查是否满足条件</h3>
<p>有时,你可能需要模型检查输入文本是否满足特定条件,并根据条件输出不同的回应。以下是一个示例:</p>
<pre><code class="language-markdown">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."</code></pre>
<p>这样的指令可以帮助模型进行条件判断,并根据情况提供不同的回应,增加了交互的灵活性。</p>
<h2>原则二:给予模型思考的时间</h2>
<p><strong>原则二</strong>:给予模型思考的时间,这是另一个关键原则。有时候,模型需要一些时间来处理复杂的任务或生成有深度的回应,因此,你需要明确要求模型不要匆忙得出结论,而是要找出自己的解决方案。</p>
<h3>策略1:明确说明完成任务所需的步骤</h3>
<p>为了确保模型不匆忙,你可以明确要求模型按照一系列步骤来完成任务。这样,模型将会仔细思考每一步,而不是仓促生成回应。以下是一个示例:</p>
<pre><code class="language-markdown">Your task is to 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:
``<code>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.</code>``</code></pre>
<p>这样的指令将确保模型不会仓促生成回应,而是按照步骤仔细思考任务。</p>
<h3>策略2</h3>
<p>:提示模型不要匆忙得出结论,而是要找出自己的解决方案</p>
<p>有时,你可能需要模型对某个问题进行分析,并给出自己的解决方案,而不是仅仅回答问题。以下是一个示例:</p>
<pre><code class="language-markdown">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:
``<code>question here</code>``
Student's solution:
``<code>student's solution here</code>``
Actual solution:
``<code>steps to work out the solution and your solution here</code>``
Is the student's solution the same as actual solution just calculated:
``<code>yes or no</code>``
Student grade:
``<code>correct or incorrect</code>``
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:
这样的指令要求模型首先思考问题,然后进行比较和评估,确保回应是经过深思熟虑的。
结论
编写Prompt时,清晰明了的指令和给予模型足够的思考时间是两个关键原则。只有当你的指令清晰明了,模型才能够准确理解你的需求;而给予模型足够的思考时间,可以确保回应是经过深思熟虑的。遵循这两个原则,你将能够更好地与AI模型互动,获得更有价值的结果。
参考
- Deep Learning AI - ChatGPT Prompt Engineering for Developers
- Deep Learning AI - ChatGPT Prompt Engineering Lesson