Prompt Engineering
记录好用的Prompt和怎么写Prompt
关于反斜杠的说明
在本课程中,我们使用反斜杠使文本与屏幕适配,而不插入换行符 “\n”。
无论是否插入换行符,GPT-3 都不会受到影响。但是,在通常使用 LLM 时,你可能要考虑 Prompt
中的换行符是否会影响模型的性能。
Prompt Engineering基本原则
指导原则一:编写清晰、具体的指令(clear, specific)
策略一:使用分隔符清晰地表示输入的不同部分
分隔符可以是:```,"",<>,,<\tag>等
triple backticks -> ```
XXX delimited by triple backticks
Given the customer email delimited by ```
prompt = f"""
Summarize the text delimited by triple backticks \
into a single sentence.
```{text}```
"""
XXX delimited by <>
XXX delimited by """
prompt = f"""
Summarize the text delimited by triple backticks \
into a single sentence.
\"\"\"{text}\"\"\"
"""
策略二:要求一个结构化的输出
可以是 Markdown、Json、HTML、XML 等格式
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}\"\"\"
"""
限制输出的样式
- Give your answer as a single word, either "positive" or "negative".
- Format the Anger value as a boolean.
可以限制输出文本的长度
- 3 sentences
- 50 words
- 280 characters
- Make your response as short as possible.
希望用换行符分割答案
"""
Separate your answers with line breaks.
"""
策略三:要求模型检查是否满足条件
如果任务的结果不一定满足假设条件,那么我们可以要求模型先检查这些假设条件,如果它们不满足,就指出这一点,并停止尝试完成完整的任务。
策略四:少样本提示(few-shot prompt)
format
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.
"""
指导原则二:给模型思考的时间
策略一:指定完成任务所需的步骤
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}>
"""
策略二:教导模型得出结论之前,先自己想办法解决问题
这个是视频给的例子,目的是判断学生给的答案对不对
他这个Prompt结构如下:
- Task: Your task is XXX
- Step: To solve the problem do the following:
- Format: Use the following format:
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:
"""
LLM 擅长的工作
-
Summary 总结
- Your task is to generate a short summary of a product review from an ecommerce site.
-
Extract relevant information 提取相关信息
- Your task is to extract relevant information from a product review from an ecommerce site to give feedback to the Shipping department.
-
Inference 推理(可以看做对文本进行分析,提取标签,提取名字,理解文本的情感,推断文本的主题)
- What is the sentiment of the following product review, which is delimited with triple backticks?
-
Transforming 转换任务
- 翻译
- Translate the following English text to Spanish:
- Tell me which language this is:
- Translate the following text to Spanish in both the
formal and informal forms:····
- Tone 语气和风格转换
- Translate the following from slang to a business letter:
- 文本格式转换
- Translate the following python dictionary from JSON to an HTML table with column headers and title:
- 转换代码语言
- 翻译
-
拼写检查/语法检查
- f"Proofread and correct:
{t}
" - Proofread and correct the following text
and rewrite the corrected version. If you don't find
and errors, just say "No errors found". Don't use
any punctuation around the text: - proofread and correct this review. Make it more compelling. 校对和更正text,并使其更有说服力。
- f"Proofread and correct:
-
Expanding 扩充任务
- 将一小段简短的文本,例如一组说明或主题列表,用大型语言模型生成一段更长的文本,例如关于某个主题的电子邮件或一篇文章。
- 让LLM帮你头脑风暴
-
chat
- 与语言模型的每次对话都是一次独立的交互,这意味着你必须提供所有相关的信息,以便模型在当前对话中使用。
-
集成多个任务(使用一个提示就能同时完成上面的任务)
-
如果需要的话,可以制定AI生成内容的签名
- Sign the email as
AI customer agent
.
- Sign the email as
Reference
- 【ChatGPT提示工程师&AI大神吴恩达教你写提示词|prompt engineering【完整中字九集全】】 https://www.bilibili.com/video/BV1Z14y1Z7LJ/?share_source=copy_web&vd_source=6821231fb1b89afa409f7e82e3feea72
- 【ChatGPT】吴恩达『提示工程』课程完全笔记 - youcans的文章 - 知乎
https://zhuanlan.zhihu.com/p/626966526 - ChatGPT|万字长文总结吴恩达prompt-engineering课 - 周末程序猿的文章 - 知乎
https://zhuanlan.zhihu.com/p/628394563