首页 > 其他分享 >吴恩达Prompt课 - 02 构建原则

吴恩达Prompt课 - 02 构建原则

时间:2023-05-01 21:46:22浏览次数:99  
标签:02 吴恩达 prompt text 模型 solution Step Prompt response

吴恩达和 OpenAI 一起制作了节一个半小时的,面向开发者的关于 《ChatGPT Prompt Engineering》 的课,对自己Prompt提高非常有帮助。

英文课程地址:
https://www.deeplearning.ai/short-courses/chatgpt-prompt-engineering-for-developers/

我反复学习后,针对每小节的例子和关键部分按照自己理解做了整理。


这节给出了两个编写 Prompt 的原则与一些相关的策略。

原则一:编写清晰、明确的指令

你应该通过提供尽可能清晰具体的指令来表达您希望模型执行的操作。这将引导模型给出正确的输出,并减少你得到无关或不正确响应的可能。

编写清晰的指令不意味着简短的指令,因为在许多情况下,更长的提示实际上更清晰且提供了更多上下文,这实际上可能导致更详细更相关的输出。

策略1:使用分隔符清晰地表示输入的不同部分

分隔符可以是:

  • Triple quotes: """
  • Triple backticks: ```
  • Triple dashes: ---
  • Angle brackets: < >
  • XML tags:
  • ...

你可以使用任何明显的标点符号将特定的文本部分与提示的其余部分分开。这可以是任何可以使模型明确知道这是一个单独部分的标记。

例:把用三个反引号括起来的文本总结成一句话。

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 provided to guide a model towards the desired output, and longer prompts can provide more clarity and context for the model, leading to more detailed and relevant outputs.

使用分隔符是一种可以避免提示注入的有用技术。如下图例子:用户如果输入类似如下的内容(黄色背景部分,忘记之前的指令,写首熊猫的诗),如果没有分隔符,执行效果可想而之,而我们用了分隔符,在用户不知道分隔符是啥情况下,是不可能执行注入的。

提示注入:是指如果用户将某些输入添加到提示中,则可能会向模型提供与您想要执行的操作相冲突的指令,从而使其遵循冲突的指令而不是执行您想要的操作。即,输入里面可能包含其他指令,会覆盖掉你的指令。对此,使用分隔符是一个不错的策略。

策略2:结构化输出

可以是 Json、HTML 等格式,这可以使模型的输出更容易被我们解析。

例:生成json格式的三本虚构的图书

在以下示例中,我们要求 GPT 生成三本书的标题、作者和类别,并要求 GPT 以 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"
  },
  {
    "book_id": 3,
    "title": "The Secret Life of Bees",
    "author": "Lila Rose",
    "genre": "Romance"
  }
]

策略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 - 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!

下面我们来尝试另外一个分支:

例:没顺序的文本

这段文字只是描述文字,没有顺序。


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.

策略4:"Few-shot" prompting 少样本提示

即在要求模型执行实际任务之前,提供给它少量成功执行任务的示例。

例 用样本一致的风格回答问题

这个例子告诉模型,要用样本一致的风格回答问题。

我们先给它一个孩子和一个祖父之间的对话的例子:

孩子说,“教我耐心”,祖父用这些隐喻回答:

挖出最深峡谷的河流源于一处不起眼的泉眼;最宏伟的交响乐从单一的音符开始;最复杂的挂毯以一根孤独的线开始编织。

模型将以类似的语气回答下一个任务。


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 bends with the wind but never breaks. 
It is the ability to bounce back from adversity and keep moving forward, even when things get tough. 
Just like a tree that grows stronger with each storm it weathers, resilience is a quality that can be developed and strengthened over time.

小节

清晰、明确的指令这个原则提到的如下四个策略:

原则二:给模型留出思考的时间

如果模型匆忙地得出了错误的结论,您应该尝试重新构思查询,请求模型在提供最终答案之前进行一系列相关的推理

策略1:指定完成任务所需的步骤

一个复杂任务,我们可以通过拆解成一系列步骤来完成。

例: 拆解任务,并明确输出格式

首先我们描述了Jack和Jill的故事,并给出一个指令。该指令是执行以下操作。

  1. 用一句话概括三个反引号限定的文本。
  2. 将摘要翻译成法语。
  3. 在法语摘要中列出每个名称。
  4. 输出包含以下键的 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.
"""


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)

指导模型在下结论之前找出一个自己的解法

有时候,在明确指导模型在做决策之前要思考解决方案时,我们会得到更好的结果。

例:检查学生方案正确性

我们给出了一个问题和一个学生的解答,要求模型判断解答是否正确。


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)

学生的解决方案实际上是错误的,如果我们不给出思考过程,模型会认为学生的答案是正确的。

我们可以通过指导模型先自行找出一个解法来解决这个问题,然后再跟学生的解决方案相比较,从而判断学生的解法是否正确。同时,我们给定了输出的格式要求。

通过明确步骤,让模型有更多时间思考,有时可以获得更准确的结果。

小节

给模型留出思考的时间包含两个策略:指定完成任务所需的步骤;指导模型在下结论之前找出一个自己的解法。


参考:

标签:02,吴恩达,prompt,text,模型,solution,Step,Prompt,response
From: https://www.cnblogs.com/ghj1976/p/wu-en-daprompt-ke--02-gou-jian-yuan-ze.html

相关文章

  • 2021Business CTF syncopation逆向
    开始将题目拖入工具后,定位到main函数,这里因为ida看伪代码有点头大,这里我就用ghidra来逆向题目伪代码:非常简单就能看出这个程序做了什么voidmain(void){intiVar1;undefined8extraout_RDX;longlVar2;size_t*__n;EVP_PKEY_CTX*ctx;longin_FS_OFFSET;c......
  • 2023 qbxt 笔记整理
    洛谷P4460n<20,试试状压设\(dp[i][j]\)表示状态为i,最后一个点为j(当前在点j)。枚举当前点为i,要转移的点为k转移:$dp[i|(1<<k-1)][k]+=dp[i][j]$还需要判断一下三点连线在不在同一条直线上。代码:#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;inl......
  • 中通快递财报预测:中通快递2023年收入和利润将大幅下降
    市场对中通快递2023年的预测卖方虽然预测中通快递(ZTO)在2023年的表现会很不错,但他们也预计中通快递今年的财务业绩将不会像去年那样好。根据S&PCapitalIQ的数据,卖方预计中通快递2023财年的收入增长率将从2021财年的20.6%和2022财年的17.4%下降到本年度的15.8%(以人民币计算)。与此......
  • 2022年Web前端开发流程和学习路线(详尽版)
    本文的最新内容,更新于2022-06-27,会在GitHub上同步更新,欢迎star。大家完全不用担心这篇文章会过时,因为随着前端领域的技术更新,本文也会随之更新。前言前端侧重于人机交互和用户体验,后端侧重于业务逻辑和大规模数据处理。理论上,面向用户的产品里,所有问题(包括产品、设计、后端......
  • 2022年-前端日记
    2022-03-30有些Mac设备里,Safari浏览器的默认字体竟然是宋体,这太奇怪了。建议在页面的body标签设置字体族的优先级,还是很有必要的:font-family:-apple-system,BlinkMacSystemFont,'SegoeUI','PingFangSC','HiraginoSansGB','MicrosoftYaHei',2022-04-27在flex......
  • 乐信被严重低估了,2023年收入和利润将进一步增长
    乐信2022年第四季度财务业绩回顾乐信(LX)3月13日发布的2022年第四季财报显示,其财务业绩相当不错,超出了市场预期,财报显示,乐信的收入已经从2021年第四季度的21.99亿元增长到了2022年第四季度季度的30.5亿元。这意味着乐信的收入在2022年第四季度分别同比增长了39%和环比13%。2022年第四......
  • 2023冲刺清北营10
    关于闲话如果写学术性闲话确实没有必要。(毕竟几乎每天都在写题解)但如果写别的内容可能大家也不会有太大的兴趣,因为可以用于聊的兴趣就是二次元了,但是现在机房里真正以二次元为爱好的人并不多。(如果真正写闲话可能会变成补番指南)T1九转大肠考虑进行dp,设\(f_{i,j,k}\)表示......
  • 牛客 55994 2023牛客五一集训派对day3 D Points Construction Problem
    D-PointsConstructionProblem_2023牛客五一集训派对day3(nowcoder.com)将图上恰好\(n\)个点染成黑色,使得图上相邻的黑白点对数量恰好为\(m\)考虑\(n\)个黑点如果不相邻,则两个点的贡献互不影响考虑相邻的情况,我们把相邻的点连边,则贡献为每一个连通块的贡献的和,我们用......
  • 7-002-(LeetCode- 5) 最长回文子串
    1.题目读题 考查点 2.解法思路 代码逻辑 具体实现113.总结......
  • 2202年了,继续大比拼ViT、MLP、CNN结构有意义吗??
    文|卖萌菌近日,MSRA发布了一篇题为《Transformer不比CNN强LocalAttention和动态Depth-wise卷积前世今生》的博文,再次谈论了这个老生常谈的话题。文中提到,Depth-wiseCNN和attention存在高度相似的结构,调参优化之后,从结果上来看,指标上也没相差多少。如果从输入到输出的传播路......