prompt-engineering
Prompt engineering basics
Prompt Engineering in a nutshell
A good prompt consists of these 4 ingredients:
- Initial context
- Telling chatgpt what role to play, and providing any initial context about the situation the model needs to know.
- Use the act as or imagine syntax for defining the gpt role
- Instructions
- Start by saying, your task is ____. The task is best paired with good context or a good role.
- Input data
- Make sure chatgpt can find your input data by clearly separating it from the rest of your prompt.
- Put your input data as the last thing in your prompt, to prevent confusion
- You could do something like, “here is the text below:”
- Constraints and format
- Explain to gpt what format you want the output to be in
- using fewer than 200 characters
- your response should be formatted as markdown, you should bold any key sentences or phrases
- Explain to gpt what format you want the output to be in
All together, you should get something like this:
Act as an article summarizing assistant. I will provide you with the text of a news article, and I’d like you to generate a summary. (step 1, initial context) The summary should include a 2 sentence overall summary and then also include 4-6 bullet points summarizing the key points of the article. (step 2, instructions) Your total output should not exceed 120 words. (step 4, constraints) Here is the text: (then you do step 3, the input)
This is formally known as the RGC prompt:
- RGC prompting: A type of prompting that can be universally used to generate detailed output. It has these following attributes:
- role: Tell chatgpt who to act as
- result: Tell chatgpt what kind of output you want back
- goal: What is the purpose that the output is supposed to serve? What are you trying to accomplish here?
- context: Provide what or who the output is for
- constraint: Guidelines for the response.
- You are an expert
[role]. Create[result]. The goal is[end goal]. The content is for[context]. Your guidelines for writing are[constraints].
What makes a good prompt
- Setting the stage: What is your role and what are your objectives? Is there context about your work that Claude should know about?
- Defining the task: What action do you want Claude to take? Do you want Claude to write, analyze, build, or something else?
- Specifying rules: What's the style or tone you want Claude to use? Are there examples that you can attach to show Claude what you're looking for?
Then follow the 6-part prompt framework
- Give context: Be specific about what you want, why you want it, and relevant background
- Show examples: Demonstrate the output style or format you're looking for
- Specify constraints: Clearly define format, length, and other output requirements
- Break complex tasks into steps: Guide the AI through multi-step reasoning
- Ask the AI to think first: Give space for the AI to work through its process
- Define the AI's role or tone: Specify how you want the AI to communicate
Basic techniques
Shot prompting
The concept of a shot is providing the model context that is an example of a response you want to get back or of what you want the model to do.
NOTE
The main use case of shot prompting is to inject a bit of determinism into the AI so we can get a predictable response back. This is useful if you need the response to be in a certain style, format, etc.
There are three types of "shot" prompting:
- zero-shot: you don't give the AI any examples of the response you want back.
- one-shot: you give the AI one example of the response you want back.
- few-shot: you give the AI many examples of the response you want back.
The structure of a shot prompting prompt should be as follows to ensure the AI follows it correctly:
- At the beginning, give general background context and instructions and say something like "below you have examples."
- paste the examples delimited by xml tags like
<example-1>to clearly delineate where an example starts and ends. - Put your main instructions at the end
Chain of thought
For complex mathematical calculations, you can ask the ai to walk through a solution step by step:
think step by step, outline your solution process (in detail), and derive the solution step by step.