Notes on Andrew Ng’s Prompt Course

1. Course Introduction

Two Types of Large Models

  • Base LLMs: Predict the next word based on text training data.

  • Instruction Tuned LLMs: Refined through RLHF on the base, forming useful, honest, and harmless AI.

2. Prompt Principles

  • Principle 1: Write Clear and Specific Instructions clear != short

  • Principle 2: Give the Model Time to “Think”

Principle 1: Write Clear and Specific Instructions

Strategy 1: Use Delimiters to Clearly Indicate Distinct Parts of the Input
  • Delimiters can be anything like: ““, “”, < >, <tag> </tag>, :

Benefits: Avoid prompt injection.

Example: Summarize the following paragraph.

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

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)

Strategy 2: Ask for a Structured Output

JSON, HTML

Example: Generate three fictional book titles, along with their authors and genres, using the following key names in JSON format: book_id, title, author, and genre.

1
2
3
4
5
6
7
8

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)

Strategy 3: Ask the Model to Check Whether Conditions Are Satisfied
  • Example: Ask the model to check if conditions are met. Extract steps from the provided text if possible, and output the steps in the specified format; if not, output “No steps provided.”

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

Negative Sample:

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

Strategy 4: “Few-shot” Prompting
  • Provide a few examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14

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)

Principle 2: Give the Model Sufficient Thinking Time

Strategy 1: Specify the Steps Required to Complete a Task
  • For complex tasks, it is best to determine each step required and specify the expected output.

Negative Sample: The titles of the listed French names are also in French.

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)

Modified: Format Output

1
2
3
4
5
6
7
8
9
10
11
12
13

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)

Strategy 2: Instruct the Model to Work Out Its Own Solution Before Rushing to a Conclusion

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

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
“`
“””
response = get_completion(prompt)
print(response)

Model Limitations: Hallucinations

  • Boie is a real company, the product name is not real.

1
2
3
4
5

prompt = f”””
Tell me about AeroGlide UltraSlim Smart Toothbrush by Boie
“””
response = get_completion(prompt)
print(response)

  • Solution: Ask the model to first find any relevant citations from the text, then ask it to use those citations to answer the question.

3. Iterative Prompt Development

  • Continuously adjust prompts based on output results

  • Below is an instruction manual for a chair. Write a product description based on the manual.

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

prompt = f”””
Your task is to help a marketing team create a
description for a retail website of a product based
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)

Issue 1: The text is too long
  • Limit the number of words/sentences/characters.

1
2
3

Use at most 50 words.

Use at most 3 sentences.

Issue 2. Text focuses on the wrong details
  • Focus on the technical details of the chair and the materials, and include the Product ID at the end of the description.

1
2
3
4
5
6

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.

Issue 3. Description needs a table of dimensions
  • Add a table to describe the information, and output it in HTML format.

1
2
3
4
5
6
7
8
9

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.

4. Summary

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

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.
“””

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)

Summarize with a Focus on Shipping and Delivery

1

to give feedback to the Shipping department.

Summarize with a Focus on Price and Value

1

to give feedback to the pricing department, responsible for determining the price of the product.

5. Reasoning

  • Extract tags, extract names, understand the sentiment of the text

Determine if the Text is Positive or Negative

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

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!!
“””

prompt = f”””
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}”’
“””
response = get_completion(prompt)
print(response)

Identify Different Emotions

1
2
3
4
5
6
7
8
9
10

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)

Identify if There is Anger

1
2
3
4
5
6
7
8
9

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)

Identify Product and Company Names

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

prompt = f”””
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}”’
“””
response = get_completion(prompt)
print(response)

Multi-task Recognition and Format Output

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

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)

Extract Five Keywords from the Article

1
2
3
4
5
6
7
8
9
10
11
12

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.

Text sample: ”'{story}”’
“””
response = get_completion(prompt)
print(response)

Public Opinion Monitoring Based on Keywords

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

topic_list = [
“nasa”, “local government”, “engineering”,
“employee satisfaction”, “federal government”
]

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)

6. Transformation

Translation

Tone Conversion

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)

Format Conversion

1
2
3
4
5
6
7
8
9
10
11
12
13

data_json = { “restaurant employees” :[
{“name”:”Shyam”, “email”:”[email protected]”},
{“name”:”Bob”, “email”:”[email protected]”},
{“name”:”Jai”, “email”:”[email protected]”}
]}

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)

Spelling Check

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

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)

Grammar Check

1
2
3
4
5
6
7
8
9
10
11
12
13

text = f”””
Got this for my daughter for her birthday cuz she keeps taking \
it 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)

Comparison Output

1
2
3
4

from redlines import Redlines

diff = Redlines(text,response)
display(Markdown(diff.output_markdown))

APA style

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

7. Expansion

AI Customer Service, Automatically Reply to Customers Based on Reviews

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 down 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
you 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)

Set temperature=0.7

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

8. Chatbot Implementation Principles

The following text represents the patent claims, please process according to the following steps:

  1. Extract technical entities from the text, technical entities refer to entities related to technical descriptions, such as devices, apparatus, mechanical components.

  2. Based on the technical entities obtained in the first step, extract the relationships between them, represented in the form of triples (Eh, Re, Et), where Eh and Et must be technical entities obtained in the first step; if not, do not generate this triple.

Below is an example:

Text: <A robot characterized by including: a robot body; an image acquisition component, located on the robot body, the image acquisition component includes an image acquisition device; a sound component, located on the robot body, and surrounding the image acquisition device.>

Answer: Technical Entities: Robot, Robot Body, Image Acquisition Component, Image Acquisition Device, Sound Component
The relationships between these technical entities: (Robot, includes, Robot Body), (Robot, includes, Image Acquisition Component), (Image Acquisition Component, located on, Robot Body), (Image Acquisition Component, includes, Image Acquisition Device), (Robot, includes, Sound Component), (Sound Component, located on, Robot Body), (Sound Component, surrounding, Image Acquisition Device)

Text: <A drive assembly for a joint module characterized by including: a fixed shell, the fixed shell having opposite first and second ends; an input shaft, penetrating through the fixed shell in the direction from the first end to the second end, the input shaft having a first shoulder; a first bearing, sleeved on the input shaft, and connected between the first end and the input shaft, the inner ring of the first bearing abutting against the first shoulder; and a limiting sleeve, sleeved on the input shaft, and located on the side of the first bearing away from the first shoulder, the limiting sleeve abutting against the inner ring of the first bearing to jointly limit the axial displacement of the inner ring of the first bearing.

Leave a Comment