Instruction 01

ChatGPT Prompt Engineering

Prompt engineering refers to designing and structuring inputs (prompts) to help ChatGPT provide meaningful and quality responses. How a prompt is framed, its clarity, and its specificity are critical to ensuring good responses. To write a good prompt, you should:

  • Be specific: The more detailed and specific the prompt, the better ChatGPT can generate responses.

  • Provide context: Including relevant context helps ChatGPT understand the nuance of the request. You can provide context directly, such as how to act or instructions to follow, via messages with the system role. Alternatively, context may also come from messages from the user directly, having the user role, or even be generated by ChatGPT, indicated by the assistant role.

  • Use examples: Giving examples can help guide ChatGPT to generate a desired form for a response, especially when a task requires a certain format or structure.

  • Limit scope: Keeping the scope as small as possible helps ChatGPT generate answers for what you’re interested in.

  • Iterate and refine: Adjusting the wording of your prompt can help you to refine the answers ChatGPT gives, including improving accuracy or creativity.

  • Anticipate errors: Write prompts to reduce ambiguity or misunderstanding.

Example Prompt 1

Provided the above-mentioned advice on writing good prompts, consider a few bad prompt examples and look at better alternatives:

Bad prompt: “Tell me about history.”   Why it’s bad: This prompt isn’t specific. ChatGPT could answer questions about a wide variety of historical time periods or events, and this is unlikely to match what the user actually wants.

Good prompt: “Explain the political and economic causes of World War 2.”   Why it’s good: This prompt is specific, has a clear context of what you’re asking about, and has a limited scope.

Example Prompt 2

  Bad prompt: “What should I do?”

Why it’s bad: Without additional context, ChatGPT doesn’t know what you’re asking about. Is it a personal problem? Is this a technical issue? Something else? Without more details, it’s impossible to know.     Good prompt:  “I’m deciding between two job offers, one with a higher salary but longer hours, and one with a better work-life balance but lower pay. Can you help me weigh the pros and cons?”   Why it’s good: This prompt provides clear context, allowing ChatGPT to offer a tailored response based on the user’s specific situation.

Example Prompt 3

Bad prompt: “What can you tell me about technology and how it affects everyone, like businesses, people, and governments, and maybe in the future too?”

Why it’s bad: This prompt is too broad and covers multiple topics at once, making it hard for ChatGPT to generate a focused response.   Good prompt: “Can you explain the basics of machine learning and how it differs from traditional programming?”   Why it’s good: It focuses on a specific area within AI, machine learning, and asks for a comparison, which helps to limit the scope and provide more useful responses.

Example Prompt 4  

Bad Prompt: “Help me rewrite this code.”

  Why it’s bad: This prompt is too broad and doesn’t provide any examples to help ChatGPT determine what you’d like to get done or what form the response should take.     Good prompt: “Consider this code written in Swift:

func sumOfNumbers(_ numbers: [Int]) -> Int {
  var sum = 0
  for value in numbers {
    sum = sum + value
  }
  return sum
}

Rewrite this code to use modern Swift features. For example, make use of Swift’s map, reduce, and other higher-order functions. Use async/await syntax if needed instead of using closure syntax. Keep the code as simple and readable as possible.”

Why it’s good: This gives a clear context of the code to be considered, limits the scope of updating modern syntax, and provides examples of what syntax to consider when updating the code.

Notice that this example mentioned “use async/await syntax if needed.” ChatGPT should identify that the function is purely synchronous, meaning async/await syntax isn’t applicable.   If you wanted to update other Swift code to modern syntax, you could simply replace the block of code given to ChatGPT, and everything else would remain the same! Effectively, you’ve created a prompt template that you can reuse anytime you want to ask ChatGPT to help you modernize a block of code.

Iterating and Prompt Variations

The best way to create good prompts is to iterate until ChatGPT provides a response in the form you’d like.

For example, you might start with the prompt, “Help me improve this code,” which isn’t ideal because it’s unclear what you’re trying to improve. The request is also very broad, and you don’t provide any examples of what you’re aiming to achieve.

Depending on the simplicity of the code, ChatGPT might still be able to offer suggestions. For instance, a function like sumOfNumbers could clearly benefit from using reduce, and ChatGPT will likely suggest this change, even with such a basic prompt.

For anything more complex, however, ChatGPT wouldn’t respond well to such a vague request. As a first step in improving the prompt, you might specify the programming language: “Help me improve this Swift code.”

This small change narrows the scope from “all possible programming languages” to just Swift—a significant improvement! However, it’s still unclear what exactly you want to improve.

To further refine the results, you could specify the desired outcome, such as “use modern Swift syntax,” and provide examples like “use Swift’s map, reduce, and other higher-order functions.”

See forum comments
Download course materials from Github
Previous: Introduction Next: Instruction 02