ChatGPT Prompt Engineering for Developers: A Summary of Course by DeepLearning.ai

Explore our summary of DeepLearning.AI's ChatGPT course. Learn to engineer effective prompts and use cases including summarizing, inferring, transforming, expanding, and chatbot creation.

ChatGPT Prompt Engineering for Developers: A Summary of Course by DeepLearning.ai
Photo by Matheus Bertelli: https://www.pexels.com/photo/light-art-space-dark-16027813/

We’ve all grown weary of the Twitter threads and LinkedIn posts that proclaim, ‘25 Amazing ways to use ChatGPT’ or ‘20 Prompts you should know.’ However, as a beginner in ML/AI, I pondered whether there was a way to understand the underlying model and create my own prompts. That’s when I stumbled upon this free course by DeepLearning.AI: ChatGPT Prompt Engineering for Developers.

The course lasts approximately one hour, and I’d like to save you some time by summarizing its key teachings. A heartfelt thanks to the DeepLearning.AI and OpenAI teams for providing this dedicated ‘How-To’ guide to enhance our prompting efficiency. In this article, I have used ChatGPT and LLMs interchangeably.

Andrew Ng starts by explaining two types of Large Language Models.

  1. Base LLM
  2. Instruction Tuned LLM

Base LLM: This type of LLM predicts the next word based on text training data. Basically, the objective of this LLM is to figure out what is the next most likely word to follow. This can work for generic text and can complete sentences. But when you’re expecting answers to questions like “What is the capital of France?”, the result could be more questions related to France instead of the answer “Paris”.

Instruction Tuned LLM: Instruction Tuned LLMs start off with Base LLMs that have been trained on a huge amount of text data and are further trained with Inputs and Outputs that are instructions. These are refined using Reinforcement Learning from Human Feedback (RLHF) to improve the system's ability to follow instructions.

Base LLMs are likelier to output toxic data, as they are simply trained on text. However, Instruction tuned LLMs are trained to follow instructions that make them Helpful, Honest and Harmless.

Since the ChatGPT is an Instruction tuned LLM, we are more likely to get helpful information when we provide clear instructions like tone of the response, what type of response you are interested in (“Scientific discoveries of Alan Turing” vs “Write something about Alan Turing”) etc.,


Guidelines for Prompting

The Guidelines provided are simple.

Write Clear and Specific Instructions

  • Clear Instruction doesn’t mean short instructions.
  • Use Delimiters: Make it clear to the model what section of the prompt you want it to process.
  • For example: Summarize the text ‘’’ Born into an upper-middle class family, Van Gogh drew as a child and was serious …. ‘’’. It can be any punctuation that makes it clear to the separation between prompt and text clear to the model.
  • Ask for a structured output: say HTML, JSON. This helps in using the output generated by GPT in Python or any other script.
Credits: DLAI — Learning Platform Prototype (deeplearning.ai)
  • Ask the model to check whether conditions are satisfied. For example, you provide an instruction and then ask the model whether the text in the prompt follows the provided instructions.
  • Few Shots Prompting: We provide the structure of the output we want as an example, and we ask the model to generate new text imitating the presented example in terms of style, tone of the text etc.,
Credits: DLAI — Learning Platform Prototype (deeplearning.ai)

Give the Model time to think.

When you ask the model to perform a complicated task in a constrained condition, say when you ask for a response without breaking a complicated prompt into multiple steps or when you ask the model to respond in a lesser number of words, there is a chance that the model can provide an incorrect response.

To avoid this, we have the following tactics.

  • Specify the steps to complete a task.
    1. Step 1: Write a short story with two characters.
    2. Step 2: Translate the story into French.
    3. Step 3: List the names of the characters. etc.,
  • Instruct the model to work out its own solution before rushing to a conclusion. Suppose you want to evaluate something; instead of asking the model to perform the evaluation directly, you can ask the model to generate its own solution and then generate the conclusion. This provides additional time for the model to provide a reasonable response.

Model Limitations

The model does not know the boundaries of the knowledge it is trained on. Hence it can generate fabricated responses that may not actually be true. This is called Hallucination.

To reduce the model hallucinations, ask for it to quote the source and then ask the model to provide a response based on the source material.

Iterative Prompt Development Process

To obtain the prompt that works well for the task you want to achieve, following a good iterative process to improve the prompt is good practice. Andrew states in the course that the iteratively developed prompts satisfy the necessity better. Hence there could be no such thing as the best prompt one should follow.

The important step here is to develop an iterative framework. The framework presented in the course is:

Credits: DLAI — Learning Platform Beta (deeplearning.ai)

The process of getting the task done using an LLM can be similar to the framework followed while writing the code. We have an idea; we implement the idea and check the result. We do the Error Analysis and modify the idea to generate the desired result. This iterative process can be applied to Prompt Engineering as well.

We have a task in mind; we provide an initial prompt to the LLM. Check the result, then iteratively modify the prompt to get the desired result. For example: “Generate an Essay on Best Presidents of the World” -> “Generate an Essay on Best Presidents of the World under 50 words” -> “Generate an Essay on Best Presidents of the World under 50 words. Restrict the candidates who were elected in Presidential System of Government”.


Summarizing

Summarizing is one of the most common uses of ChatGPT. However, the course provides another insight into what we emphasize when we generate a summary.

For example, there is a text related to a Product. We can generate multiple summaries based on what specifically we need to know. We can generate a summary to give feedback to the Shipping department, thus making the summary focus on shipping details. In another scenario, we can generate a summary highlighting the feedback on Design for the Design department.

One of the interesting use cases of Summarizing through ChatGPT Professor Andrew presented is, saving the list of reviews and iterating them through a for loop generating a summary for each review. This is an interesting use case when we only want short and concise information for varied data.

Another way to use this feature is to use it to “Extract” information that is necessary. This eliminates additional details focusing only on the necessary information. For example, “Extract the shipping feedback for the product”. The resulting text doesn’t contain the product information but only contains the shipping-related information.

Inferring

Text analysis, which requires multiple models and hours of work by skilled Machine Learning Engineer, can be done in minutes using the right prompts with an LLM. Professor Andrew presents an example of a review and extracts the nature of the Sentiment in the review (Positive or Negative).

Credits: DLAI — Learning Platform Beta (deeplearning.ai)

Further, we can use the LLM to infer the types of emotions of the review writer and it can directly give them to us for further processing.

Credits: DLAI — Learning Platform Beta (deeplearning.ai)

Further text analysis involves extracting richer information like the customers' names and topics discussed and generating alerts when something related to a specific topic comes up. All these text analyses need lots of expertise and time, but now we can use LLMs instead.

Transforming

One of the prominent uses of LLMs is to transform one type of text into another. This transformation can be language-to-language transformation, Tone transformation or Format transformation. LLMs can also be used to perform Spelling and Grammar checks or Proofread content.

This particular section in the course deals with using multiple ways of transformation using ChatGPT. Since the GPT is trained with hundreds of languages with varying levels of proficiency, we can simultaneously translate multiple sentences using FOR loop.

We can also use it to translate from one file format to another. For example, if we want to transform JSON inputs to HTML tables with headers, LLMs can do that.

Sending Data to ChatGPT API
Providing the Data in JSON format. Credits: DLAI — Learning Platform Beta (deeplearning.ai)
Example display of Transformation using ChatGPT
Visualizing the Output from ChatGPT in HTML format. Credits: DLAI — Learning Platform Beta (deeplearning.ai)

And the most favourite application I personally use and is advocated in the course is to check the Grammar, and flow of the sentence. It is also used to change the text's tone based on the Audience. For example, a piece of a sentence that a colleague says can be changed into a letter that could be written to a professor.

Expanding

Expanding is taking a shorter piece of text and generating a longer response. For example, writing a letter for a certain necessity or Providing information on a topic etc., We can use ChatGPT as a brainstorming partner and code interpreter. But we must keep in mind that this could also generate false information. So, Andrew, in his course, advocates responsible use of this feature.

Temperature is an important parameter that modifies exploration and variety of responses during expansion. The higher the temperature, the higher the likelihood of the model choosing responses with less likelihood. This variety helps in generating diverse responses with improved creativity.

When generating responses:
Lower Temperature -> For tasks that require Predictability & Reliability. Higher Temperature -> For tasks that require variety.

Chatbot

ChatGPT is trained to take a series of messages as input and return a model-generated response as output. The course teaches how to build a chatbot to explain how ChatGPT can be used to handle multi-turn conversations.

Code to access ChatGPT through OpenAI API
Credits: DLAI — Learning Platform Beta (deeplearning.ai)

The course presents using two helper functions in developing a Chatbot. The “messages” argument is a list of message dictionaries. Each dictionary specifies the role of the entity sending the message and the content present in the message.

There are three roles for the messages sent to LLM:

  1. System: The role that sets the context of the message sent. For example, “You are a mathematics helper bot. You explain the ways to solve mathematical equations.”
  2. Assistant: The assistant corresponds to the LLM that generates the response corresponding to the user prompt.
  3. User: The content or prompt sent to the LLM is sent under the "User" role.
Roles in sending messages to ChatGPT
Credits: DLAI — Learning Platform Beta (deeplearning.ai)

The function get_completion_from_messages(), takes a list of messages instead of a single prompt. This allows the chatbot to handle back and forth conversations.

##Credits : https://learn.deeplearning.ai/chatgpt-prompt-eng/lesson/8/chatbot

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)

“Collect messages” helper function collects prompts from user interface and append it to the list called “Context” based on the role of the message sending entity.

##Credits : https://learn.deeplearning.ai/chatgpt-prompt-eng/lesson/8/chatbot

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

Whenever the Button “Chat” is pressed, a new “Collect Messages” function is called, and the context is updated to provide the model response.

Chatbot building using ChatGPT
Credits: https://learn.deeplearning.ai/chatgpt-prompt-eng/lesson/8/chatbot

This is how the chatbot developed using ChatGPT would look like by using the above helper functions.

Summary

The course provides a comprehensive perspective on how developers can effectively use ChatGPT. It begins by discussing the principles and tactics for crafting effective prompts. Then, it delves into other key use cases, such as Summarizing, Inferring, Transforming, and Expanding. A practical use case demonstrating the development of a chatbot using ChatGPT is also presented. Undertaking this course has given me a well-rounded understanding of how to use ChatGPT to its potential. I hope you’ve found this article helpful. We’re interested in hearing what additional topics you’d like to see covered in this area, so feel free to let us know!