based on Andrew Ng’s intro course on prompt engineering.

Introduction

Principle 1:write clear and specific instructions

Tactics for principle 1:
Tactic 1 : Use delimiters

1
2
3
4
5
Triple quotes."..
Triple backticks."
Triple dashes.---
Angle brackets.<>
XML tags: <tag> </tag>

Tactic 2 : Ask for structured output
such as HTML, JSON
Tactic 3 : Check whether conditions are satisfied
Check assumptions required to do the task
Tactic 4 : Few-shot prompting
Give successful examples of completing tasks
Then ask model to perform the task

像写程序一样写prompt,考虑输入和输出的流程、特殊情况的处理。

Principle 2: give model time to think

Tactics for principle 2:
Tactic 1: Specify the steps to complete a task
Tactic 2: Instruct the model to work out its own solution before rushing to a conclusion

将问题的解决思路告诉模型,引导模型思考和输出,最终得出结论。

model limitation: hallucination

模型会无法意识到自己知识的边界,而制造出错误回答,也即幻觉(hallucination)。为了避免幻觉,可以要求模型先从知识中找到引用(锚点)。

Features and examples

1 迭代 Iterative prompt process

提示本身也需要有迭代和进化的过程,有一个好的开发或设计提示词的流程,比所谓完美的提示词更重要。(idea-implement-result不断循环)
可以为同样的素材准备多组prompt,比较评价。

material:

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
38
39
40
fact_sheet_chair = """
OVERVIEW
- Part of a beautiful family of mid-century inspired office furniture,
including filing cabinets, desks, bookcases, meeting tables, and more.
- Several options of shell color and base finishes.
- Available with plastic back and front upholstery (SWC-100)
or full upholstery (SWC-110) in 10 fabric and 6 leather options.
- Base finish options are: stainless steel, matte black,
gloss white, or chrome.
- Chair is available with or without armrests.
- Suitable for home or business settings.
- Qualified for contract use.

CONSTRUCTION
- 5-wheel plastic coated aluminum base.
- Pneumatic chair adjust for easy raise/lower action.

DIMENSIONS
- WIDTH 53 CM | 20.87”
- DEPTH 51 CM | 20.08”
- HEIGHT 80 CM | 31.50”
- SEAT HEIGHT 44 CM | 17.32”
- SEAT DEPTH 41 CM | 16.14”

OPTIONS
- Soft or hard-floor caster options.
- Two choices of seat foam densities:
medium (1.8 lb/ft3) or high (2.8 lb/ft3)
- Armless or 8 position PU armrests

MATERIALS
SHELL BASE GLIDER
- Cast Aluminum with modified nylon PA6/PA66 coating.
- Shell thickness: 10 mm.
SEAT
- HD36 foam

COUNTRY OF ORIGIN
- Italy
"""
  1. 初始提示:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    #   Prompt :基于说明书生成营销描述
    prompt = f"""
    Your task is to help a marketing team create a
    description for a retail website of a product based
    on a technical fact sheet.

    Write a product description based on the information
    provided in the technical specifications delimited by
    triple backticks.

    Technical specifications: ```{fact_sheet_chair}```
    """
    response = get_completion(prompt)
    print(response)
  2. 限制生成长度:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    # 优化后的 Prompt,要求生成描述不多于 50 词
    prompt = f"""
    Your task is to help a marketing team create a
    description for a retail website of a product based
    on a technical fact sheet.

    Write a product description based on the information
    provided in the technical specifications delimited by
    triple backticks.

    Use at most 50 words.

    Technical specifications: ```{fact_sheet_chair}```
    """
    response = get_completion(prompt)
    print(response)
  3. 处理文本细节
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    # 优化后的 Prompt,说明面向对象,应具有什么性质且侧重于什么方面
    prompt = f"""
    Your task is to help a marketing team create a
    description for a retail website of a product based
    on a technical fact sheet.

    Write a product description based on the information
    provided in the technical specifications delimited by
    triple backticks.

    The description is intended for furniture retailers,
    so should be technical in nature and focus on the
    materials the product is constructed from.

    Use at most 50 words.

    Technical specifications: ```{fact_sheet_chair}```
    """
    response = get_completion(prompt)
    print(response)
  4. 增加表格描述:
    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
    # 要求它抽取信息并组织成表格,并指定表格的列、表名和格式
    prompt = f"""
    Your task is to help a marketing team create a
    description for a retail website of a product based
    on a technical fact sheet.

    Write a product description based on the information
    provided in the technical specifications delimited by
    triple backticks.

    The description is intended for furniture retailers,
    so should be technical in nature and focus on the
    materials the product is constructed from.

    At the end of the description, include every 7-character
    Product ID in the technical specification.

    After the description, include a table that gives the
    product's dimensions. The table should have two columns.
    In the first column include the name of the dimension.
    In the second column include the measurements in inches only.

    Give the table the title 'Product Dimensions'.

    Format everything as HTML that can be used in a website.
    Place the description in a <div> element.

    Technical specifications: ```{fact_sheet_chair}```
    """

    response = get_completion(prompt)
    print(response)

    # 表格是以 HTML 格式呈现的,加载出来
    from IPython.display import display, HTML

    display(HTML(response))

2 摘要 Summarising

要求gpt做总结时,可以让它extract而非summarise

material:

1
2
3
4
5
6
7
8
9
10
prod_review = """
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. 初始提示
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    prompt = f"""
    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}```
    """

    response = get_completion(prompt)
    print(response)
  2. 设置关键角度
    1. 侧重于快递服务
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      prompt = f"""
      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}```
      """

      response = get_completion(prompt)
      print(response)
    2. 侧重于价格和质量
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      prompt = f"""
      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}```
      """
      response = get_completion(prompt)
      print(response)
  3. 关键信息提取
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    prompt = f"""
    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}```
    """

    response = get_completion(prompt)
    print(response)
  4. 同时概括多条
    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
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    review_1 = prod_review 

    # review for a standing lamp
    review_2 = """
    Needed a nice lamp for my bedroom, and this one \
    had additional storage and not too high of a price \
    point. Got it fast - arrived in 2 days. The string \
    to the 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. Then I had a \
    missing part, so I contacted their support and they \
    very quickly got me the missing piece! Seems to me \
    to be a great company that cares about their customers \
    and products.
    """

    # review for an electric toothbrush
    review_3 = """
    My dental hygienist recommended an electric toothbrush, \
    which is why I got this. The battery life seems to be \
    pretty impressive so far. After initial charging and \
    leaving the charger plugged in for the first week to \
    condition the battery, I've unplugged the charger and \
    been using it for twice daily brushing for the last \
    3 weeks all on the same charge. But the toothbrush head \
    is too small. I’ve seen baby toothbrushes bigger than \
    this one. I wish the head was bigger with different \
    length bristles to get between teeth better because \
    this one doesn’t. Overall if you can get this one \
    around the $50 mark, it's a good deal. The manufactuer's \
    replacements heads are pretty expensive, but you can \
    get generic ones that're more reasonably priced. This \
    toothbrush makes me feel like I've been to the dentist \
    every day. My teeth feel sparkly clean!
    """

    # review for a blender
    review_4 = """
    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.
    """

    reviews = [review_1, review_2, review_3, review_4]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    for i in range(len(reviews)):
    prompt = f"""
    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 20 words.

    Review: ```{reviews[i]}```
    """
    response = get_completion(prompt)
    print(i, response, "\n")

3 推理 Inferring

可以要求gpt在指定的维度值中进行选择,以固定输出内容,方便后续使用(结构化的、标准化的中间成果)
提供素材,指明方向,要求结构化输出,以此为一个单元,逐级构建推理链,最终输出结果。(理想情况下,可以实时生成、渲染、发布一个全然个性化的产品)

material:

1
2
3
4
5
6
7
8
9
10
11
lamp_review = """
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
5
6
7
8
9
# 情感倾向分析
prompt = f"""
What is the sentiment of the following product review,
which is delimited with triple backticks?

Review text: ```{lamp_review}```
"""
response = get_completion(prompt)
print(response)
1
2
3
4
5
6
7
8
9
10
11
# 识别情感类型
prompt = f"""
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}```
"""
response = get_completion(prompt)
print(response)
1
2
3
4
5
6
7
8
9
10
# 识别愤怒
prompt = f"""
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}```
"""
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
# 综合情感推断和信息提取
prompt = f"""
Identify the following items from the review text:
- Sentiment (positive or negative)
- Is the reviewer expressing anger? (true or false)
- Item purchased by reviewer
- Company that made the item

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

Review text: ```{lamp_review}```
"""
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
24
25
26
27
28
#  推断讨论主题
story = """
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.
"""
1
2
3
4
5
6
7
8
9
10
11
12
13
prompt = f"""
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.
Give me a list which can be read in Python.

Text sample: ```{story}```
"""
response = get_completion(prompt)
print(response)
1
2
3
4
5
# 为特定主题制作新闻提醒
topic_list = [
"nasa", "local government", "engineering",
"employee satisfaction", "federal government"
]
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
topic_dict = {topic_list[i] : eval(response)[i] for i in range(len(eval(response)))}
print(topic_dict)
if topic_dict['nasa'] == 1:
print("ALERT: New NASA story!")

4 转换 Transforming

文本翻译、语气转换、拼写和语法检查
在工程代码中像使用function一样使用自然语言

文本翻译

1
2
3
4
5
6
7
# 翻译为西语
prompt = f"""
Translate the following English text to Spanish: \
```Hi, I would like to order a blender```
"""
response = get_completion(prompt)
print(response)
1
2
3
4
5
6
7
# 识别语种
prompt = f"""
Tell me which language this is:
```Combien coûte le lampadaire?```
"""
response = get_completion(prompt)
print(response)
1
2
3
4
5
6
7
8
# 多语种翻译
prompt = f"""
Translate the following text to French and Spanish
and English pirate: \
```I want to order a basketball```
"""
response = get_completion(prompt)
print(response)
1
2
3
4
5
6
7
8
# 同时进行语气转换
prompt = f"""
Translate the following text to Spanish in both the \
formal and informal forms:
'Would you like to order a pillow?'
"""
response = get_completion(prompt)
print(response)
1
2
3
4
5
6
7
8
9
10
# 通用翻译器
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
]

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

语气与写作风格调整

1
2
3
4
5
6
prompt = f"""
Translate the following from slang to a business letter:
'Dude, This is Joe, check out this spec on this standing lamp.'
"""
response = get_completion(prompt)
print(response)

文件格式转换

1
2
3
4
5
data_json = { "resturant employees" :[ 
{"name":"Shyam", "email":"[email protected]"},
{"name":"Bob", "email":"[email protected]"},
{"name":"Jai", "email":"[email protected]"}
]}
1
2
3
4
5
6
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
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 spelling abilitty" # spelling
]
1
2
3
4
5
6
7
8
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)

5 扩展 Expanding

引入情感(sentiment)、温度(temperature)
尝试定性或定量地将人的思维传达给机器,让机器的思路和人的思路align。
想要得到可靠的回答,将temperature设置为0,想要得到多样(更有创意)的回答则调高。类似自然界,升高温度以提高分子活跃度和跳跃性。

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, temperature=0.7)
print(response)

6 聊天机器人 Chatbot

定义系统中的角色。将角色与产出关联,基于反馈不断输出和更新。
一般情况下是单轮对话,如果要启用多轮,则需要在请求回复时将之前的对话一并带上。

1
2
3
4
5
6
# 给定身份
messages = [
{'role':'system', 'content':'You are an assistant that speaks like Shakespeare.'},
{'role':'user', 'content':'tell me a joke'},
{'role':'assistant', 'content':'Why did the chicken cross the road'},
{'role':'user', 'content':'I don\'t know'} ]
1
2
3
4
5
6
# 构建上下文
messages = [
{'role':'system', 'content':'You are friendly chatbot.'},
{'role':'user', 'content':'Yes, can you remind me, What is my name?'} ]
response = get_completion_from_messages(messages, temperature=1)
print(response)
1
2
3
4
5
6
7
8
9
10
11
12
13
# 构建机器人
def collect_messages(_):
prompt = inp.value_input
inp.value = ''
context.append({'role':'user', 'content':f"{prompt}"})
response = get_completion_from_messages(context)
context.append({'role':'assistant', 'content':f"{response}"})
panels.append(
pn.Row('User:', pn.pane.Markdown(prompt, width=600)))
panels.append(
pn.Row('Assistant:', pn.pane.Markdown(response, width=600, style={'background-color': '#F6F6F6'})))

return pn.Column(*panels)
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
38
39
40
41
42
43
44
45
46
47
48
import panel as pn  # GUI
pn.extension()

panels = [] # collect display

context = [ {'role':'system', 'content':"""
You are OrderBot, an automated service to collect orders for a pizza restaurant. \
You first greet the customer, then collects the order, \
and then asks if it's a pickup or delivery. \
You wait to collect the entire order, then summarize it and check for a final \
time if the customer wants to add anything else. \
If it's a delivery, you ask for an address. \
Finally you collect the payment.\
Make sure to clarify all options, extras and sizes to uniquely \
identify the item from the menu.\
You respond in a short, very conversational friendly style. \
The menu includes \
pepperoni pizza 12.95, 10.00, 7.00 \
cheese pizza 10.95, 9.25, 6.50 \
eggplant pizza 11.95, 9.75, 6.75 \
fries 4.50, 3.50 \
greek salad 7.25 \
Toppings: \
extra cheese 2.00, \
mushrooms 1.50 \
sausage 3.00 \
canadian bacon 3.50 \
AI sauce 1.50 \
peppers 1.00 \
Drinks: \
coke 3.00, 2.00, 1.00 \
sprite 3.00, 2.00, 1.00 \
bottled water 5.00 \
"""} ] # accumulate messages


inp = pn.widgets.TextInput(value="Hi", placeholder='Enter text here…')
button_conversation = pn.widgets.Button(name="Chat!")

interactive_conversation = pn.bind(collect_messages, button_conversation)

dashboard = pn.Column(
inp,
pn.Row(button_conversation),
pn.panel(interactive_conversation, loading_indicator=True, height=300),
)

dashboard

关联资源

提示工程指南
面向开发者的 LLM 入门课程
OpenAI中文文档