ChatGPT Prompting for Software Engineers

Notes from a short free course on ChatGPT prompting for developers.

I think that the course actually is not for developers, it’s rather basic and introductory but it contains some good advice.

This guide seems to be more elaborate and technical.

The code

openai package can be installed with pip install openai.

A useful code template used throughout the lecture:

import openai
import os

# you need the variable defined in the environment
openai.api_key = os.getenv('OPENAI_API_KEY')

def get_completion(prompt):
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": prompt}],
        temperature=0
    )
    return response.choices[0].message["content"]

The temperature is telling the model how freely it can explore the possible answers. At 0 it means that it will stick to the most probable answers/continuation. The higher temperature (up to 1.0) the more weight is on the less probable answers and the results should differ more between retries.

The temperature parameter could be seen as a creativity parameter.

Two principles of prompting

1. Write clear and specific instructions

2. Give the model time to think

Model limitations

Iterative prompt development

You can probably ignore articles like “10 perfect prompts…”. Create your own prompts suited for your needs.

Good general prompt beginning:
Your task is to...

Limit length with
Use at most X characters/words/sentences.

Refine prompts with a batch of examples.

Clarify instructions step by step.

Examples of tasks for ChatGPT

Chatbot

The internals of the ChatGPT API are suitable for chatbot creation. The model is instructed via messages.

[
  {
    "role": "system",
    "content": "You are an assistant..."
  },
  {
    "role": "user",
    "content": "Describe a ..."
  },
  {
    "role": "asistant",
    "content": "..."
  }
]

The system role sets behaviour / persona of assistant. High level structure, guiding of the assistant.

An example of prompt to create a chatbot for pizza orders:

{
  '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"""
}

(The example is taken directly from the course code.)

first published: 2023-05-06
last modified: 2023-05-23