文章目录
  1. 1. 提示指南
    1. 1.1. 提示原则
      1. 1.1.1. 原则1: 给出清晰且具体的指令
        1. 1.1.1.1. 技巧1. 使用分隔符清晰分割出“输入”部分
      2. 1.1.2. 原则2: 给模型时间进行思考
        1. 1.1.2.1. 技巧1: 分解完成一个任务的多个步骤
        2. 1.1.2.2. 技巧2: 在得出结论之前要求模型给出自己的解
      3. 1.1.3. 模型限制:幻觉(Hallucinations)
    2. 1.2. 迭代式提示工程开发
      1. 1.2.1. ML的迭代周期
      2. 1.2.2. 提示工程周期指南
        1. 1.2.2.1. 例子
    3. 1.3. 总结及提取
      1. 1.3.1. 总结案例
        1. 1.3.1.1. 总结用户评论重点
    4. 1.4. 推断
      1. 1.4.1. 案例(推断产品评论是正向还是负向)
      2. 1.4.2. 案例 从一篇文章中推断(Inferring)主题
    5. 1.5. 转换(Transforming)
      1. 1.5.1. 翻译
        1. 1.5.1.1. 案例 不同语言的翻译
    6. 1.6. 扩展(Expanding)
      1. 1.6.1. 自动回复客户邮件
    7. 1.7. GPT 中的几个参数
      1. 1.7.1. temperature

提示指南

提示原则

原则1: 给出清晰且具体的指令

技巧1. 使用分隔符清晰分割出“输入”部分

分隔符可以为:```, “”””, < >,<tag> </tag>, :

例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
```

##### 技巧2: 要求按结构化形式(JSON,HTML)输出

##### 技巧3: 要求模型检查是否满足条件

##### 技巧4: “少样本提示” ("Few-shot" prompting)

Few-shot prompting是一种自然语言处理技术,用于解决对于仅有少量样本或无标注数据的情况下,如何构建高质量的语言模型的问题。在这种方法中,一个预训练的语言模型被设计用于从少量的示例中快速学习和推理新任务的解决方案,通常是利用模板式提示(prompting)来指导学习。

举个例子,一个少样本的文本分类任务可以通过给模型一些文本片段作为提示,告诉模型如何将文本分类为某个特定的类别。这些提示可以是简单的单词或短语,也可以是更复杂的结构化提示,比如“选择一个句子,其中描述了XX”,从而让模型更好地理解和执行任务。Few-shot prompting在各种自然语言处理任务中都具有广泛的应用,包括文本分类、命名实体识别、机器翻译等。

简而言之就是给模型举个例子,然后向模型提问类似问题。

例如:

```plainText

原则2: 给模型时间进行思考

技巧1: 分解完成一个任务的多个步骤
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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.
"""
# example 1
prompt_1 = f"""
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:
```{text}```
"""
response = get_completion(prompt_1)
print("Completion for prompt 1:")
print(response)
技巧2: 在得出结论之前要求模型给出自己的解

直接给出结果的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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)

要求模型给出自己的解,再得出结论:

1
2
3
4
5
6
7
8
9
10
11
12
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

1
Student's solution:

student’s solution here

1
Actual solution:

steps to work out the solution and your solution here

1
2
Is the student's solution the same as actual solution \
just calculated:

yes or no

1
Student grade:

correct or incorrect

1
2

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.
1
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
1
2
3
4
Actual solution:
"""
response = get_completion(prompt)
print(response)

模型限制:幻觉(Hallucinations)

就是常说的模型“一本正经胡说八道”,会对可能的结果(并不真实存在)做出描述。

1
Tell me about  AeroGlide UltraSlim Smart Toothbrush by boie
1
2
3
4
5
The AeroGlide UltraSlim Smart Toothbrush by Boie is a high-tech toothbrush that incorporates advanced features to provide an optimal teeth cleaning experience. It uses patented AeroGlide bristles to gently yet effectively clean teeth and gums, reducing the risk of gum disease and cavities. The bristles are made from an ultra-soft material that is gentle on tooth enamel and won't scratch or damage it.

The toothbrush also features a smart timer that ensures users brush their teeth for the recommended two minutes, and a rechargeable battery that provides up to two weeks of use on a single charge. Additionally, it comes with a sleek travel case that makes it easy to take the toothbrush with you on the go.

Overall, the AeroGlide UltraSlim Smart Toothbrush by Boie is a great choice for those who want a high-quality, effective toothbrush that incorporates advanced features to improve their oral health.

Boie 是真实存在的公司,但是“AeroGlide UltraSlim Smart Toothbrush” 产品不存在

迭代式提示工程开发

ML的迭代周期

想法(Idea)-> 实现/代码-数据 (Implementation(code/data) prompt) -> 实验结果(Experimental result)-> 错误分析(Error Analysis) -> 想法 …..

提示工程周期指南

  • 提示清晰具体
  • 分析输出为什么没有给出预期结果
  • 调优想法及提示
  • 重复
例子

总结及提取

总结案例

总结用户评论重点
1
2
3
4
5
6
7
8
Got this panda plush toy for my daughter's birthday, \
who loves it and takes it everywhere. It's soft and \
super cute, and its face has a friendly look. It's \
a bit small for what I paid though. I think there \
might be other options that are bigger for the \
same price. It arrived a day earlier than expected, \
so I got to play with it myself before I gave it \
to her.

提示指令:

  • 有字数限制的总结
1
2
3
4
5
6
7
Your task is to generate a short summary of a product \
review from an ecommerce site.

Summarize the review below, delimited by triple
backticks, in at most 30 words.

Review: ```{prod_review}```
  • 关注某个方面(示例中为:交付/快递)的总结
1
2
3
4
5
6
7
8
9
Your task is to generate a short summary of a product \
review from an ecommerce site to give feedback to the \
Shipping deparmtment.

Summarize the review below, delimited by triple
backticks, in at most 30 words, and focusing on any aspects \
that mention shipping and delivery of the product.

Review: ```{prod_review}```
  • 关注某几个方面(示例中为:价格及价值)的总结
1
2
3
4
5
6
7
8
9
10
Your task is to generate a short summary of a product \
review from an ecommerce site to give feedback to the \
pricing deparmtment, responsible for determining the \
price of the product.

Summarize the review below, delimited by triple
backticks, in at most 30 words, and focusing on any aspects \
that are relevant to the price and perceived value.

Review: ```{prod_review}```
  • 使用“抽取”(extract)代替“总结”(summarize)
1
2
3
4
5
6
7
8
9
Your task is to extract relevant information from \ 
a product review from an ecommerce site to give \
feedback to the Shipping department.

From the review below, delimited by triple quotes \
extract the information relevant to shipping and \
delivery. Limit to 30 words.

Review: ```{prod_review}```

推断

案例(推断产品评论是正向还是负向)

1
2
3
4
5
6
7
8
9
Needed a nice lamp for my bedroom, and this one had \
additional storage and not too high of a price point. \
Got it fast. The string to our lamp broke during the \
transit and the company happily sent over a new one. \
Came within a few days as well. It was easy to put \
together. I had a missing part, so I contacted their \
support and they very quickly got me the missing piece! \
Lumina seems to me to be a great company that cares \
about their customers and products!!

提示指令:

  • 产品评论情感是正向还是负向
1
2
3
4
What is the sentiment of the following product review, 
which is delimited with triple backticks?

Review text: '''{lamp_review}'''
  • 简化回答
1
2
3
4
5
6
7
What is the sentiment of the following product review, 
which is delimited with triple backticks?

Give your answer as a single word, either "positive" \
or "negative".

Review text: '''{lamp_review}'''
  • 识别情绪类型(多个,用“,” 连接)
1
2
3
4
5
6
Identify a list of emotions that the writer of the \
following review is expressing. Include no more than \
five items in the list. Format your answer as a list of \
lower-case words separated by commas.

Review text: '''{lamp_review}'''
  • 识别愤怒
1
2
3
4
5
Is the writer of the following review expressing anger?\
The review is delimited with triple backticks. \
Give your answer as either yes or no.

Review text: '''{lamp_review}'''
  • 从用户评论中提取产品及公司名
1
2
3
4
5
6
7
8
9
10
11
12
Identify the following items from the review text: 
- Item purchased by reviewer
- Company that made the item

The review is delimited with triple backticks. \
Format your response as a JSON object with \
"Item" and "Brand" as the keys.
If the information isn't present, use "unknown" \
as the value.
Make your response as short as possible.

Review text: '''{lamp_review}'''
1
2
3
4
{
"Item": "lamp",
"Brand": "Lumina"
}

案例 从一篇文章中推断(Inferring)主题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
In a recent survey conducted by the government, 
public sector employees were asked to rate their level
of satisfaction with the department they work at.
The results revealed that NASA was the most popular
department with a satisfaction rating of 95%.

One NASA employee, John Smith, commented on the findings,
stating, "I'm not surprised that NASA came out on top.
It's a great place to work with amazing people and
incredible opportunities. I'm proud to be a part of
such an innovative organization."

The results were also welcomed by NASA's management team,
with Director Tom Johnson stating, "We are thrilled to
hear that our employees are satisfied with their work at NASA.
We have a talented and dedicated team who work tirelessly
to achieve our goals, and it's fantastic to see that their
hard work is paying off."

The survey also revealed that the
Social Security Administration had the lowest satisfaction
rating, with only 45% of employees indicating they were
satisfied with their job. The government has pledged to
address the concerns raised by employees in the survey and
work towards improving job satisfaction across all departments.

提示指令

  • 推断5个主题
1
2
3
4
5
6
7
8
Determine five topics that are being discussed in the \
following text, which is delimited by triple backticks.

Make each item one or two words long.

Format your response as a list of items separated by commas.

Text sample: '''{story}'''
  • 对特定主题设置提醒,进行监控
1
2
3
4
5
6
7
8
9
10
11
12
13
prompt = f"""
Determine whether each item in the following list of \
topics is a topic in the text below, which
is delimited with triple backticks.

Give your answer as list with 0 or 1 for each topic.\

List of topics: {", ".join(topic_list)}

Text sample: '''{story}'''
"""
response = get_completion(prompt)
print(response)
1
2
3
4
5
nasa: 1
local government: 0
engineering: 0
employee satisfaction: 1
federal government: 1
1
2
3
topic_dict = {i.split(': ')[0]: int(i.split(': ')[1]) for i in response.split(sep='\n')}
if topic_dict['nasa'] == 1:
print("ALERT: New NASA story!")
1
ALERT: New NASA story!

转换(Transforming)

翻译

案例 不同语言的翻译

提示指令:

1
2
Translate the following English text to Spanish: \ 
```Hi, I would like to order a blender```
  • 检测是哪种语言?
1
2
Tell me which language this is: 
'''Combien coûte le lampadaire?'''
  • 翻译多个语言
1
2
3
Translate the following  text to French and Spanish
and English pirate: \
'''I want to order a basketball'''
  • 翻译为书面语及非书面语
1
2
3
Translate the following text to Spanish in both the \
formal and informal forms:
'Would you like to order a pillow?'
  • 多种语言翻译为统一语言
1
2
3
4
5
6
7
user_messages = [
"La performance du système est plus lente que d'habitude.", # System performance is slower than normal
"Mi monitor tiene píxeles que no se iluminan.", # My monitor has pixels that are not lighting
"Il mio mouse non funziona", # My mouse is not working
"Mój klawisz Ctrl jest zepsuty", # My keyboard has a broken control key
"我的屏幕在闪烁" # My screen is flashing
]
1
2
3
4
5
6
7
8
9
10
11
for issue in user_messages:
prompt = f"Tell me what language this is: ```{issue}```"
lang = get_completion(prompt)
print(f"Original message ({lang}): {issue}")

prompt = f"""
Translate the following text to English \
and Korean: ```{issue}```
"""
response = get_completion(prompt)
print(response, "\n")
  • 语气转换(Tone transformation)
1
2
Translate the following from slang to a business letter: 
'Dude, This is Joe, check out this spec on this standing lamp.
1
2
3
4
5
6
7
8
9
Dear Sir/Madam,

I am writing to bring to your attention a standing lamp that I believe may be of interest to you. Please find attached the specifications for your review.

Thank you for your time and consideration.

Sincerely,

Joe
  • 格式转换(JSON 转 HTML)
1
2
3
4
5
6
7
8
9
10
11
12
data_json = { "resturant employees" :[ 
{"name":"Shyam", "email":"shyamjaiswal@gmail.com"},
{"name":"Bob", "email":"bob32@gmail.com"},
{"name":"Jai", "email":"jai87@gmail.com"}
]}

prompt = f"""
Translate the following python dictionary from JSON to an HTML \
table with column headers and title: {data_json}
"""
response = get_completion(prompt)
print(response)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<table>
<caption>Restaurant Employees</caption>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>Shyam</td>
<td>shyamjaiswal@gmail.com</td>
</tr>
<tr>
<td>Bob</td>
<td>bob32@gmail.com</td>
</tr>
<tr>
<td>Jai</td>
<td>jai87@gmail.com</td>
</tr>
</tbody>
</table>
1
2
from IPython.display import display, Markdown, Latex, HTML, JSON
display(HTML(response))
  • 拼写/语法检查
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
text = [ 
"The girl with the black and white puppies have a ball.", # The girl has a ball.
"Yolanda has her notebook.", # ok
"Its going to be a long day. Does the car need it’s oil changed?", # Homonyms
"Their goes my freedom. There going to bring they’re suitcases.", # Homonyms
"Your going to need you’re notebook.", # Homonyms
"That medicine effects my ability to sleep. Have you heard of the butterfly affect?", # Homonyms
"This phrase is to cherck chatGPT for speling abilitty" # spelling
]
for t in text:
prompt = f"""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:
```{t}```"""
response = get_completion(prompt)
print(response)

提示指令关键词:

  • proofread (校对)
  • rewrite (重写)
  • correct (纠正)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
text = f"""
Got this for my daughter for her birthday cuz she keeps taking \
mine from my room. Yes, adults also like pandas too. She takes \
it everywhere with her, and it's super soft and cute. One of the \
ears is a bit lower than the other, and I don't think that was \
designed to be asymmetrical. It's a bit small for what I paid for it \
though. I think there might be other options that are bigger for \
the same price. It arrived a day earlier than expected, so I got \
to play with it myself before I gave it to my daughter.
"""
prompt = f"proofread and correct this review: ```{text}```"
response = get_completion(prompt)
print(response)

# show the diff
from redlines import Redlines

diff = Redlines(text,response)
display(Markdown(diff.output_markdown))
  • 以某种写作风格改写文字
1
2
3
4
5
6
7
8
prompt = f"""
proofread and correct this review. Make it more compelling.
Ensure it follows APA style guide and targets an advanced reader.
Output in markdown format.
Text: ```{text}```
"""
response = get_completion(prompt)
display(Markdown(response))

扩展(Expanding)

自动回复客户邮件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# given the sentiment from the lesson on "inferring",
# and the original customer message, customize the email
sentiment = "negative"

# review for a blender
review = f"""
So, they still had the 17 piece system on seasonal \
sale for around $49 in the month of November, about \
half off, but for some reason (call it price gouging) \
around the second week of December the prices all went \
up to about anywhere from between $70-$89 for the same \
system. And the 11 piece system went up around $10 or \
so in price also from the earlier sale price of $29. \
So it looks okay, but if you look at the base, the part \
where the blade locks into place doesn’t look as good \
as in previous editions from a few years ago, but I \
plan to be very gentle with it (example, I crush \
very hard items like beans, ice, rice, etc. in the \
blender first then pulverize them in the serving size \
I want in the blender then switch to the whipping \
blade for a finer flour, and use the cross cutting blade \
first when making smoothies, then use the flat blade \
if I need them finer/less pulpy). Special tip when making \
smoothies, finely cut and freeze the fruits and \
vegetables (if using spinach-lightly stew soften the \
spinach then freeze until ready for use-and if making \
sorbet, use a small to medium sized food processor) \
that you plan to use that way you can avoid adding so \
much ice if at all-when making your smoothie. \
After about a year, the motor was making a funny noise. \
I called customer service but the warranty expired \
already, so I had to buy another one. FYI: The overall \
quality has gone done in these types of products, so \
they are kind of counting on brand recognition and \
consumer loyalty to maintain sales. Got it in about \
two days.
"""
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
prompt = f"""
You are a customer service AI assistant.
Your task is to send an email reply to a valued customer.
Given the customer email delimited by ```, \
Generate a reply to thank the customer for their review.
If the sentiment is positive or neutral, thank them for \
their review.
If the sentiment is negative, apologize and suggest that \
they can reach out to customer service.
Make sure to use specific details from the review.
Write in a concise and professional tone.
Sign the email as `AI customer agent`.
Customer review: ```{review}```
Review sentiment: {sentiment}
"""
response = get_completion(prompt)
print(response)
1
2
3
4
5
6
7
8
9
10
11
Dear Valued Customer,

Thank you for taking the time to leave a review about our product. We are sorry to hear that you experienced an increase in price and that the quality of the product did not meet your expectations. We apologize for any inconvenience this may have caused you.

We would like to assure you that we take all feedback seriously and we will be sure to pass your comments along to our team. If you have any further concerns, please do not hesitate to reach out to our customer service team for assistance.

Thank you again for your review and for choosing our product. We hope to have the opportunity to serve you better in the future.

Best regards,

AI customer agent

GPT 中的几个参数

temperature

0-1 取值闭区间参数, 数据越小代表结果约确定/精确, 数值越大结果返回更多样。

文章目录
  1. 1. 提示指南
    1. 1.1. 提示原则
      1. 1.1.1. 原则1: 给出清晰且具体的指令
        1. 1.1.1.1. 技巧1. 使用分隔符清晰分割出“输入”部分
      2. 1.1.2. 原则2: 给模型时间进行思考
        1. 1.1.2.1. 技巧1: 分解完成一个任务的多个步骤
        2. 1.1.2.2. 技巧2: 在得出结论之前要求模型给出自己的解
      3. 1.1.3. 模型限制:幻觉(Hallucinations)
    2. 1.2. 迭代式提示工程开发
      1. 1.2.1. ML的迭代周期
      2. 1.2.2. 提示工程周期指南
        1. 1.2.2.1. 例子
    3. 1.3. 总结及提取
      1. 1.3.1. 总结案例
        1. 1.3.1.1. 总结用户评论重点
    4. 1.4. 推断
      1. 1.4.1. 案例(推断产品评论是正向还是负向)
      2. 1.4.2. 案例 从一篇文章中推断(Inferring)主题
    5. 1.5. 转换(Transforming)
      1. 1.5.1. 翻译
        1. 1.5.1.1. 案例 不同语言的翻译
    6. 1.6. 扩展(Expanding)
      1. 1.6.1. 自动回复客户邮件
    7. 1.7. GPT 中的几个参数
      1. 1.7.1. temperature