Avatar

Hej, I'm Julia.

ChatGPT Prompt Engineering for Developers - Course Notes

#ai #chatgpt

12 min read

I've been a little quiet here on my blog recently. The main reason for this is that I've been making a conscious effort to spend more time away from my computer during weekends. There's been a lot going on at work recently, and I've been spending too many hours in front of my screen. I've therefore been balancing things out over the weekend (and trying to avoid burnout) 😀.

But anyways... what's been up tech-wise? We've been embracing the recent advances in AI technology at our company recently. It really does feel like there's been a step shift in how we're going about building product, in addition to how we're making our working day more productive. Some examples:

  • We ran a company-wide, opt-in AI hackathon week, with the aim of getting AI-assisted features for the benefit of our users, into production, by the end of the week. It was a massive success!
  • As a software engineer, I'm strongly encouraged to use GitHub Copilot and ChatGPT as part of my daily workflow (these are costs that can be expensed).
    • Other AI tools we're trialling as a company include tools like OtterAI, ChatGPT's API and Langchain.
  • Setting up Communities of Practice around ChatGPT and practical uses of AI, with training sessions.

As one of the engineers building product, it's somewhat mandatory for us to keep up with the latest developments in consumer-facing AI. One of the main reasons for this is to allow us to better spot opportunities to inject AI (where appropriate) into what we're building. As an example, we had time set aside for us last week to go through the ChatGPT Prompt Engineering for Developers course (offered by DeepLearning.ai for free). It's a fairly short course, that takes about 2-3 hours, and one I'd highly recommend for anyone using ChatGPT.

Here are my notes (verbatim) from the course. It might not make a whole load of sense, so go check out the course for yourself! 😉

Update: Here are my notes from another course I took from DeepLearning called Building Systems With the ChatGPT API


Guidelines

Prompting principles

  1. Write clear and specific instructions
  2. Give the model time to "think"

Write clear and specific instructions

  1. Use delimiters to clearly indicate distinct parts of the input e.g.
prompt = f"""
Summarise the text delimited by triple backticks into a single message
```{text}```
"""
  • Examples of delimiters:
    • """
    • triple backticks
    • < >
    • <tag> </tag>
    • :
  • This also helps prevent prompt injection.
  1. Ask for a structured input e.g. JSON
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.
"""
  1. Ask the model to check whether conditions are satisfied. e.g.
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}\"\"\"
"""
  1. "Few shot" prompting
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.
"""

Give the model time to "think"

  • Instruct the model to spend longer / more computational power, thinking about the task.
  1. Specify the steps to complete a task
prompt = 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}>
"""
  1. Instruct the model to work out its own solution before rushing to a conclusion
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:
"""

Model Limitations

  • Hallucinations - making up answers that are not true but sound plausible.
  • To reduce hallucinations, ask the model to:
    • First, find relevant information
    • Then answer the question based on relevant information

Iterative Prompt Development

  • Expect to write an initial prompt, and iterate on that prompt, to get the desired result you want.
  • If you want to limit length, try saying "Use at most 250 characters" or "Use at most 3 sentences". LLMs are not great at counting.
  • Example:
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}```
"""

Summarising

  • Be clear about who / what the summary is for e.g.
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}```
"""
  • Try to extract instead of summarise:
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}```
"""
  • Ask the model to iterate through an array:
reviews = [review_1, review_2, review_3, review_4]

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

Inferring

  • Use GPT to infer sentiment and topics. Much faster than traditional data science models where you'd need a specific, labelled data set, and would have to train each model specifically e.g. for sentiment, or for topic.

  • To derive sentiment:

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}'''
"""
  • Identify types of emotions:
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}'''
"""
  • Identify anger:
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}'''
"""
  • Extract information:
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}'''
"""
  • Inferring topics:
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}'''
"""
  • Alert for specific topics:
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}'''
"""

Transforming

  • Can be used for text transformation like language translation, spelling, grammar checking, tone adjustment and format conversion.
  • Spanish translation:
prompt = f"""
Translate the following text to Spanish in both the \
formal and informal forms: 
'Would you like to order a pillow?'
"""
  • Multi-translator:
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")
  • Tone transformation:
prompt = f"""
Translate the following from slang to a business letter: 
'Dude, This is Joe, check out this spec on this standing lamp.'
"""
  • Format conversion
data_json = { "resturant 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}
"""
  • Spell/grammar checks
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)
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}```
"""

Expanding

  • Generate personalised text.
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}
"""
  • Play around with the model temperature to define how unpredictable / creative the model's answers are.

Chatbot

  • OpenAI's GPT model has a variety of message types, denoted by "role".
    • system: sets the behaviour of the assistant
    • user: you
    • assistant: the model
  • You need to provide the right context to the model i.e. sending the entire message history.

© 2016-2024 Julia Tan · Powered by Next JS.