
https://learn.deeplearning.ai/courses/ai-prompting-for-everyone/information
Generative AI offers many opportunities for AI engineers to build, in minutes or hours, powerful applications that previously would have taken days or weeks. I’m excited about sharing these best practices to enable many more people to take advantage of these revolutionary new capabilities.(Innovate and create value in ways that were cost-prohibitive, highly technical, or simply impossible before now).
– Andrew Ng
Here is the text from the video transcript arranged into a clear, readable format.
The Two Key Principles
- Write clear and specific instructions.
- Give the model time to think.
Setup (Using the OpenAI API)
Before we get started, we need to do a little setup. Throughout the course, we’ll use the OpenAI Python library to access the OpenAI API.
- If you haven’t installed this Python library already, you can install it using pip:
pip install openai - Then, import OpenAI and set your API key (a secret key from the OpenAI website).
- You can also set this as an environment variable.
- For this course, you don’t need to do any of this, as the API key is already set in the environment.
We’ll use OpenAI’s ChatGPT model (GPT-3.5 Turbo) and the chat completions endpoint. We’ll define a helper function called getCompletion that takes in a prompt and returns the completion for that prompt.
Principle 1: Write Clear and Specific Instructions
Express what you want the model to do by providing instructions that are as clear and specific as possible. This guides the model towards the desired output and reduces irrelevant or incorrect responses.
Note: Don’t confuse a clear prompt with a short prompt. Longer prompts often provide more clarity and context, leading to more detailed and relevant outputs.
Tactics for Clear Instructions
Tactic 1: Use Delimiters to clearly indicate distinct parts of the input.
- Use triple backticks, quotes, XML tags, section titles, or any clear punctuation.
- This also helps avoid prompt injections (where a user adds conflicting instructions).
Example: « Summarize the text delimited by triple backticks into a single sentence. » ```text```
Tactic 2: Ask for a Structured Output (e.g., HTML or JSON).
- This makes parsing the model’s outputs easier.
Example: « 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. »
Tactic 3: Ask the model to check whether conditions are satisfied.
- If the task makes assumptions that aren’t necessarily satisfied, tell the model to check these assumptions first. If they’re not satisfied, indicate this and stop.
- Consider potential edge cases and how the model should handle them.
Example: « You’ll be provided with text delimited by triple quotes. If it contains a sequence of instructions, rewrite them in a given format. If the text does not contain a sequence of instructions, simply write ‘no steps provided’. »
Tactic 4: Few-shot prompting.
- Provide examples of successful executions of the task before asking the model to do the actual task.
Example: Provide a conversation between a child and grandparent about « patience » (with a metaphorical response), then ask « teach me about resilience. » The model will respond in a similar metaphorical tone.
Principle 2: Give the Model Time to Think
If a model is making reasoning errors by rushing to an incorrect conclusion, reframe the query to request a chain or series of relevant reasoning before the model provides its final answer.
If you give a model a task that is too complex for a short amount of time or a small number of words, it may make up an incorrect guess (just like a person would). Instruct the model to think longer about a problem, spending more computational effort.
Tactics for Giving the Model Time to Think
Tactic 1: Specify the steps required to complete a task.
- Lay out the exact sequence of actions for the model to follow.
Example prompt:
- Summarize the following text (delimited by triple backticks) in one sentence.
- Translate the summary into French.
- List each name in the French summary.
- Output a JSON object with keys:
french_summary,num_names.
- You can also specify the exact output format for more predictable and parsable results.
Tactic 2: Instruct the model to work out its own solution before rushing to a conclusion.
- This is especially useful for problems where a student’s solution might be incorrect. Asking the model to solve the problem first, then compare its solution to the student’s, yields more accurate results.
Example: Determine if a student’s math solution is correct.
- Bad approach: Model reads the student’s plausible-looking but incorrect solution and agrees.
- Good approach: Instruct the model: « First, work out your own solution to the problem. Then, compare your solution to the student’s solution. Don’t decide if the student’s solution is correct until you have done the problem yourself. »
Model Limitations: Hallucinations
Language models have not perfectly memorized all information they’ve seen and don’t know the boundary of their knowledge well. They might try to answer questions about obscure topics and make things up that sound plausible but are not true. These fabricated ideas are called hallucinations.
Example: Asking about a fictitious product (« AeroGlide Ultra Slim Smart Toothbrush by Boy ») will result in a realistic-sounding but completely made-up description.
Tactic to reduce hallucinations: When you want the model to generate answers based on a provided text, ask the model to first find any relevant quotes from the text and then use those quotes to answer questions. Tracing answers back to the source document helps reduce hallucinations.