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
- Clear ≠ short.
- Use delimiters
(triple quotes, backticks, dashes, XML tags, angle brackets).
- This helps avoiding prompt injections.
- Wrap the actual input (after instructions) into separators (<xxx>, ```xxx```).
- Ask for structured output (HTML, JSON; e.g. suggest keys to fill in).
- Check whether conditions are satisfied
(something like if or assert in programming languages).
If a condition ... is not met, output ...
. - Few-shot prompting.
- Provide examples of tasks / templates to follow and only then ask the model.
2. Give the model time to think
- Specify the steps to complete a task.
- You may give it a template to fill in the partial results.
- Instruct the model to work out its own solution before rushing to a conclusion.
Model limitations
- The model doesn’t know its own limitations.
- Hallucinations.
- To reduce them: first let the model find relevant information, then answer the question, based on the relevant info.
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 withUse at most X characters/words/sentences.
Refine prompts with a batch of examples.
Clarify instructions step by step.
Examples of tasks for ChatGPT
- Extracting a list of steps from instructions (a recipe).
- Analogy.
- Summarization.
Your task is to generate a short summary of ...
- Inferring and information extraction.
- Sentiment analysis:
What is the sentiment of the ...
Identify a list of emotions that the writer of the ...
- Information extraction:
Identify the following items from the text: ...
- Topic extraction:
Determine five topics that are being discussed...
- Zero-shot learning: give it a list of topics and ask which are used.
- Sentiment analysis:
- Transforming.
- Translation:
Translate the following English text to Chinese: <...>
- Language identification:
Tell me which language this is: <...>
- Change of register:
Translate the following text into the informal form: <...>
orTranslate the following from slang to a business letter: <...>
. - Formatting:
Translate the following Python dictionary / JSON to an HTML table with column headers and title: <...>
. - Spell/grammar check:
Proofread and correct the following text: <...>
. Make it more compelling. Ensure if follows APA style guide. Output in markdown format. Text: <...>
.
- Translation:
- Expanding
- Generate a response (email) from customer support/service to a review.
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:
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.)
last modified: 2023-05-29
https://vit.baisa.cz/notes/code/chatgpt-prompting/