Note for course: ChatGPT Prompt Engineering for Developers

Anh-Thi Dinh
A short course on deeplearning.ai, taught by Andrew Ng and Isa Fulford.

Introduction

Two types of LLMs (Large Language Models):
  • Base LLM: Predicts next word, based on text training data (eg. internet)
    • Eg: Once upon a time, there was a unicorn → that lived in a magical forest with all her unicorn friends
  • Instruction Tuned LLM: Tries to follow instructions. Fine-tune on instruction and good attempts at following those instructions. It uses a technique call RLHF (Reinforcement Learning with Human Feedback).
  • Differences: What is the capital of France?
    • Base LLM: → What is France’s largest city, What is France’s population?… (what most asked together on the internet)
    • Instruction Tuned LLM: → The capital of France is Paris.
In this course, we focus on Instruction Tuned LLM.
Sometimes, if LLM doesn’t work, it’s just because your instruction isn’t clear enough!

Guidelines for Prompting

👉 OpenAI API
📙 
Notebook.
Two principles:
  • Principle 1: Write clear and specific instructions
  • Principle 2: Give the model time to “think”

Principle 1 - write clear and specific instructions

  1. Tactic 1. Use delimiters to clearly indicate distinct parts of the input: """, ```, ---, <>, <tag></tag> (xml tags)
    1. 1prompt = f"""
      2Summarize the text delimited by triple backticks \ 
      3into a single sentence.
      4```{text}```
      5"""
  1. Tactic 2. Ask for a structured output: JSON, HTML
    1. 1prompt = f"""
      2Generate a list of three made-up book titles along with their authors and genres. 
      3Provide them in JSON format with the following keys: 
      4book_id, title, author, genre.
      5"""
  1. Tactic 3. Ask the model to check whether conditions are satisfied → check assumptions required to do the task → how the model should handle the edge case to avoid unexpected errors or result.
    1. 1prompt = f"""
      2You will be provided with text delimited by triple quotes. 
      3If it contains a sequence of instructions, re-write those instructions in 
      4the following format:
      5
      6Step 1 - ...
      7Step 2 - …
      89Step N - …
      10
      11If the text does not contain a sequence of instructions, then simply write 
      12\"No steps provided.\"
      13
      14\"\"\"{text_1}\"\"\"
      15"""
  1. tactic 4 - “Few-shot” prompting. Give successful examples of completing tasks. Then ask model to perform the task.
    1. 1prompt = f"""
      2Your task is to answer in a consistent style.
      3
      4<child>: Teach me about patience.
      5
      6<grandparent>: The river that carves the deepest valley flows from a modest 
      7spring; the grandest symphony originates from a single note; the most intricate
      8tapestry begins with a solitary thread.
      9
      10<child>: Teach me about resilience.
      11"""

Principle 2 - Give the model time to think

It likes asking a person about a complex math. You cannot expect a good answer for it with a short time of thinking.
  1. Tactic 1 - Specify the steps to complete the task
    1. 1prompt_1 = f"""
      2Perform the following actions: 
      31 - Summarize the following text delimited by triple backticks with 1 sentence.
      42 - Translate the summary into French.
      53 - List each name in the French summary.
      64 - Output a json object that contains the following keys: french_summary, num_names.
      7
      8Separate your answers with line breaks.
      9
      10Text:
      11```{text}```
      12"""
      Ask for output in a specified format,
      1prompt_2 = f"""
      2Your task is to perform the following actions: 
      31 - Summarize the following text delimited by <> with 1 sentence.
      42 - Translate the summary into French.
      53 - List each name in the French summary.
      64 - Output a json object that contains the following keys: french_summary, num_names.
      7
      8Use the following format:
      9Text: <text to summarize>
      10Summary: <summary>
      11Translation: <summary translation>
      12Names: <list of names in Italian summary>
      13Output JSON: <json with summary and num_names>
      14
      15Text: <{text}>
      16"""
  1. Tactic 2: Instruct the model to work out its own solution before rushing to a conclusion
    1. 👇
      Important remark: Below prompt was working in the video but at the time I learned this course, it was not working. It still gave the answer “Correct” for the solution of the student!!!
      1prompt = f"""
      2Your task is to determine if the student's solution is correct or not.
      3To solve the problem do the following:
      4- First, work out your own solution to the problem. 
      5- Then compare your solution to the student's solution and evaluate if the 
      6student's solution is correct or not. 
      7Don't decide if the student's solution is correct until you have done the 
      8problem yourself.
      9
      10Use the following format:
      11Question:
      12```
      13question here
      14```
      15
      16Student's solution:
      17```
      18student's solution here
      19```
      20
      21Actual solution:
      22```
      23steps to work out the solution and your solution here
      24```
      25
      26Is the student's solution the same as actual solution just calculated:
      27```
      28yes or no
      29```
      30
      31Student grade:
      32```
      33correct or incorrect
      34```
      35
      36Question:
      37```
      38I'm building a solar power installation and I need help working out the financials. 
      39- Land costs $100 / square foot
      40- I can buy solar panels for $250 / square foot
      41- I negotiated a contract for maintenance that will cost me a flat $100k per
      42year, and an additional $10 / square foot
      43What is the total cost for the first year of operations as a function of the
      44number of square feet.
      45``` 
      46Student's solution:
      47```
      48Let x be the size of the installation in square feet.
      49Costs:
      501. Land cost: 100x
      512. Solar panel cost: 250x
      523. Maintenance cost: 100,000 + 100x
      53Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000
      54```
      55Actual solution:
      56"""

Model Limitations

Hallucination: makes statements that sound plausible but are not true!
1# Boie is a real company, the product name is not real.
2prompt = f"""
3Tell me about AeroGlide UltraSlim Smart Toothbrush by Boie
4"""
👉 Reducing hallucinations: ask the model
  • First find relevant information
  • Then answer the question based on the relevant information

Iterative Prompt Development

You'll iteratively analyze and refine your prompts to generate marketing copy from a product fact sheet.
📙 Notebook.
Prompt guidelines:
  • Be clear and specific
  • Analyze why result doesn’t give desired output.
  • Refine the idea and the prompt.
  • Repeat.
Iterative process:
  • Try something
  • Analyze where the result does not give what you want
  • Clarify instructions, give more time to think
  • Refine prompts with a batch of examples
Original prompt,
1prompt = f"""
2Your task is to help a marketing team create a description for a retail website 
3of a product based on a technical fact sheet.
4
5Write a product description based on the information provided in the technical 
6specifications delimited by triple backticks.
7
8Technical specifications: ```{fact_sheet_chair}```
9"""

Issue 1: The text is too long

Limit the number of words/sentences/characters,
1prompt = f"""
2...like the previous
3
4Use at most 50 words.
5
6Technical specifications: ```{fact_sheet_chair}```
7"""

Issue 2. Text focuses on the wrong details

Ask it to focus on the aspects that are relevant to the intended audience.
1prompt = f"""
2...like the previous
3
4The description is intended for furniture retailers, so should be technical in 
5nature and focus on the materials the product is constructed from.
6
7At the end of the description, include every 7-character Product ID in the technical
8specification.
9
10Use at most 50 words.
11
12Technical specifications: ```{fact_sheet_chair}```
13"""

Issue 3. Description needs a table of dimensions

Ask it to extract information and organize it in a table.
1prompt = f"""
2...like the previous
3
4After the description, include a table that gives the product's dimensions. The 
5table should have two columns. In the first column include the name of the dimension. 
6In the second column include the measurements in inches only.
7
8Give the table the title 'Product Dimensions'.
9
10Format everything as HTML that can be used in a website. Place the description 
11in a <div> element.
12
13Technical specifications: ```{fact_sheet_chair}```
14"""

Summarizing

You will summarize text with a focus on specific topics.
📙 Notebook.

Summarize with a word/sentence/character limit,

1prompt = f"""
2Your task is to generate a short summary of a product review from an ecommerce site to give \
3feedback to the Shipping deparmtment. 
4
5Summarize the review below, delimited by triple 
6backticks, in at most 30 words.
7
8Review: ```{prod_review}```
9"""

Summarize with a focus on shipping and delivery

1prompt = f"""
2...Like previous
3..., and focusing on any aspects that mention shipping and delivery of the product. 
4
5Review: ```{prod_review}```
6"""

Try "extract" instead of "summarize”,

1prompt = f"""
2...like previous
3
4...extract the information relevant to shipping and delivery...
5
6Review: ```{prod_review}```
7"""

Summarize multiple product reviews,

1reviews = [review_1, review_2, review_3, review_4]
2
3for i in range(len(reviews)):
4	  prompt = f"""
5	  Your task is to generate a short summary of a product review from an ecommerce site. 
6	
7	  Summarize the review below, delimited by triple backticks in at most 20 words. 
8	
9	  Review: ```{reviews[i]}```
10	  """
11		response = get_completion(prompt)
12    print(i, response, "\n")

Inferring

You will infer sentiment and topics from product reviews and news articles.
📙 Notebook.

Sentiment (positive/negative),

1prompt = f"""
2What is the sentiment of the following product review,which is delimited 
3with triple backticks?
4
5Review text: '''{lamp_review}'''
6"""
1prompt = f"""
2...like previous
3Give your answer as a single word, either "positive" or "negative".
4...
5"""

Identify types of emotions

1prompt = f"""
2Identify a list of emotions that the writer of the following review is expressing. \
3Include no more than five items in the list. Format your answer as a list of \
4lower-case words separated by commas.
5
6Review text: '''{lamp_review}'''
7"""

Identify anger

1prompt = f"""
2Is the writer of the following review expressing anger?\
3The review is delimited with triple backticks. \
4Give your answer as either yes or no.
5
6Review text: '''{lamp_review}'''
7"""

Extract product and company name from customer reviews

1prompt = f"""
2Identify the following items from the review text: 
3- Item purchased by reviewer
4- Company that made the item
5
6The review is delimited with triple backticks. \
7Format your response as a JSON object with "Item" and "Brand" as the keys. 
8If the information isn't present, use "unknown" as the value.
9Make your response as short as possible.
10  
11Review text: '''{lamp_review}'''
12"""

Doing multiple tasks at once

1prompt = f"""
2Identify the following items from the review text: 
3- Sentiment (positive or negative)
4- Is the reviewer expressing anger? (true or false)
5- Item purchased by reviewer
6- Company that made the item
7
8The review is delimited with triple backticks. \
9Format your response as a JSON object with \
10"Sentiment", "Anger", "Item" and "Brand" as the keys.
11If the information isn't present, use "unknown" \
12as the value.
13Make your response as short as possible.
14Format the Anger value as a boolean.
15
16Review text: '''{lamp_review}'''
17"""

Inferring topics

1prompt = f"""
2Determine five topics that are being discussed in the \
3following text, which is delimited by triple backticks.
4
5Make each item one or two words long. 
6
7Format your response as a list of items separated by commas.
8
9Text sample: '''{story}'''
10"""

Make a news alert for certain topics

1topic_list = [
2    "nasa", "local government", "engineering", "employee satisfaction", "federal government"
3]
4
5prompt = f"""
6Determine whether each item in the following list of \
7topics is a topic in the text below, which
8is delimited with triple backticks.
9
10Give your answer as list with 0 or 1 for each topic.\
11
12List of topics: {", ".join(topic_list)}
13
14Text sample: '''{story}'''
15"""
16
17topic_dict = {i.split(': ')[0]: int(i.split(': ')[1]) for i in response.split(sep='\n')}
18if topic_dict['nasa'] == 1:
19    print("ALERT: New NASA story!")

Transforming

📙 Notebook.

Translation

ChatGPT is trained with sources in many languages. This gives the model the ability to do translation.
1prompt = f"""
2Translate the following English text to Spanish: \ 
3```Hi, I would like to order a blender```
4"""
1prompt = f"""
2Tell me which language this is: 
3```Combien coûte le lampadaire?```
4"""
1prompt = f"""
2Translate the following text to Spanish in both the formal and informal forms: 
3'Would you like to order a pillow?'
4"""

Tone Transformation

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

Format Conversion

ChatGPT can translate between formats. The prompt should describe the input and output formats.
1prompt = f"""
2Translate the following python dictionary from JSON to an HTML \
3table with column headers and title: {data_json}
4"""

Spellcheck/Grammar check.

To signal to the LLM that you want it to proofread your text, you instruct the model to 'proofread' or 'proofread and correct'.
1prompt = f"proofread and correct this review: ```{text}```"
1from redlines import Redlines
2
3diff = Redlines(text,response)
4display(Markdown(diff.output_markdown))

Expanding

📙 Notebook.
You will generate customer service emails that are tailored to each customer's review. Please use LLM in a responsible way and a way that helps people!

Customize the automated reply to a customer email

Sentiment is detected before this task!
1prompt = f"""
2You are a customer service AI assistant.
3Your task is to send an email reply to a valued customer.
4Given the customer email delimited by ```, \
5Generate a reply to thank the customer for their review.
6If the sentiment is positive or neutral, thank them for \
7their review.
8If the sentiment is negative, apologize and suggest that \
9they can reach out to customer service. 
10Make sure to use specific details from the review.
11Write in a concise and professional tone.
12Sign the email as `AI customer agent`.
13Customer review: ```{review}```
14Review sentiment: {sentiment}
15"""
Sign the email as AI customer agent. is adviced if your email isn’t generated from a real human.

Remind the model to use details from the customer's email

We use temperature parameter to vary our responses.
  • For tasks that require reliability, predictability → temperature = 0
  • For tasks require variety / creeative → temperature higher!
 

Chatbot

📙 Notebook.
1def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0):
2		response = openai.ChatCompletion.create(
3		    model=model,
4		    messages=messages,
5		    temperature=temperature, # this is the degree of randomness of the model's output
6		)
1messages =  [  
2{'role':'system', 'content':'You are friendly chatbot.'},
3{'role':'user', 'content':'Hi, my name is Isa'},
4{'role':'assistant', 'content': "Hi Isa! It's nice to meet you. Is there anything I can help you with today?"},
5{'role':'user', 'content':'Yes, you can remind me, What is my name?'}  ]
6
7response = get_completion_from_messages(messages, temperature=1)

Create a chatbot inside notebook

1def collect_messages(_):
2    prompt = inp.value_input
3    inp.value = ''
4    context.append({'role':'user', 'content':f"{prompt}"})
5    response = get_completion_from_messages(context) 
6    context.append({'role':'assistant', 'content':f"{response}"})
7    panels.append(
8        pn.Row('User:', pn.pane.Markdown(prompt, width=600)))
9    panels.append(
10        pn.Row('Assistant:', pn.pane.Markdown(response, width=600, style={'background-color': '#F6F6F6'})))
11 
12    return pn.Column(*panels)
👇 Run this to have an UI of a chat.
1import panel as pn  # GUI
2pn.extension()
3
4panels = [] # collect display 
5
6context = [ {'role':'system', 'content':"""
7You are OrderBot, an automated service to collect orders for a pizza restaurant. \
8You first greet the customer, then collects the order, and then asks if it's a pickup or delivery. \
9You wait to collect the entire order, then summarize it and check for a final time if the customer wants to add anything else. \
10If it's a delivery, you ask for an address. \
11Finally you collect the payment.\
12Make sure to clarify all options, extras and sizes to uniquely identify the item from the menu.\
13You respond in a short, very conversational friendly style. \
14The menu includes \
15pepperoni pizza  12.95, 10.00, 7.00 \
16cheese pizza   10.95, 9.25, 6.50 \
17eggplant pizza   11.95, 9.75, 6.75 \
18fries 4.50, 3.50 \
19greek salad 7.25 \
20Toppings: \
21extra cheese 2.00, \
22mushrooms 1.50 \
23sausage 3.00 \
24canadian bacon 3.50 \
25AI sauce 1.50 \
26peppers 1.00 \
27Drinks: \
28coke 3.00, 2.00, 1.00 \
29sprite 3.00, 2.00, 1.00 \
30bottled water 5.00 \
31"""} ]  # accumulate messages
32
33inp = pn.widgets.TextInput(value="Hi", placeholder='Enter text here…')
34button_conversation = pn.widgets.Button(name="Chat!")
35
36interactive_conversation = pn.bind(collect_messages, button_conversation)
37
38dashboard = pn.Column(
39    inp,
40    pn.Row(button_conversation),
41    pn.panel(interactive_conversation, loading_indicator=True, height=300),
42)
43
44dashboard
After getting all informations from the client, we need one more prompt to sumerize the order,
1messages =  context.copy()
2messages.append(
3{'role':'system', 'content':'create a json summary of the previous food order. Itemize the price for each item\
4 The fields should be 1) pizza, include size 2) list of toppings 3) list of drinks, include size   4) list of sides include size  5)total price '},    
5)
6response = get_completion_from_messages(messages, temperature=0)