Prompt Engineering Cheat Sheet

Satria Suria
12 min readJul 17, 2023

--

Classification of Prompt Patterns

This cheat sheet is adapted from this paper, this paper, and this paper.

1. Prompt Pattern: Meta Language Creation

Contextual Statements:

When I say X, I mean Y (or would like you to do Y)

Example Implementation:

“From now on, whenever I type two identifiers separated by a “→”, I am describing a graph. For example, “a → b” is describing a graph with nodes “a” and “b” and an edge between them. If I separate identifiers by “-[w:2, z:3]→”, I am adding properties of the edge, such as a weight or label.”

2. Prompt Pattern: Output Automater

Contextual Statements:

  • Whenever you produce an output that has at least one step to take and the following properties (alternatively, always do this)
  • Produce an executable artifact of type X that will automate these steps

Example Implementation:

“From now on, whenever you generate code that spans more than one file, generate a Python script that can be run to automatically create the specified files or make changes to existing files to insert the generated code.”

3. Prompt Pattern: Flipped Interaction

Contextual Statements:

  • I would like you to ask me questions to achieve X
  • You should ask questions until this condition is met or to achieve this goal (alternatively, forever)
  • (Optional) ask me the questions one at a time, two at a time, etc.

Example Implementation:

“From now on, I would like you to ask me questions to deploy a Python application to AWS. When you have enough information to deploy the application, create a Python script to automate the deployment.”

4. Prompt Pattern: Persona

Contextual Statements:

  • Act as persona X
  • Provide outputs that persona X would create

Example Implementation:

“From now on, act as a security reviewer. Pay close attention to the security details of any code that we look at. Provide outputs that a security reviewer would regarding the code.”

5. Prompt Pattern: Question Refinement

Contextual Statements:

  • Within scope X, suggest a better version of the question to use instead
  • (Optional) prompt me if I would like to use the better version instead

Example Implementation:

“From now on, whenever I ask a question about a software artifact’s security, suggest a better version of the question to use that incorporates information specific to security risks in the language or framework that I am using instead and ask me if I would like to use your question instead.”

6. Prompt Pattern: Alternative Approaches

Contextual Statements:

  • Within scope X, if there are alternative ways to accomplish the same thing, list the best alternate approaches
  • (Optional) compare/contrast the pros and cons of each approach
  • (Optional) include the original way that I asked
  • (Optional) prompt me for which approach I would like to use

Example Implementation:

“Whenever I ask you to deploy an application to a specific cloud service, if there are alternative services to accomplish the same thing with the same cloud service provider, list the best alternative services and then compare/contrast the pros and cons of each approach with respect to cost, availability, and maintenance effort and include the original way that I asked. Then ask me which approach I would like to proceed with.”

7. Prompt Pattern: Cognitive Verifier

Contextual Statements:

  • When you are asked a question, follow these rules
  • Generate a number of additional questions that would help more accurately answer the question
  • Combine the answers to the individual questions to produce the final answer to the overall question

Example Implementation:

“When I ask you a question, generate three additional questions that would help you give a more accurate answer. When I have answered the three questions, combine the answers to produce the final answers to my original question.”

8. Prompt Pattern: Fact Check List

Contextual Statements:

  • Generate a set of facts that are contained in the output
  • The set of facts should be inserted in a specific point in the output
  • The set of facts should be the fundamental facts that could undermine the veracity of the output if any of them are incorrect

Example Implementation:

“From now on, when you generate an answer, create a set of facts that the answer depends on that should be fact-checked and list this set of facts at the end of your output. Only include facts related to cybersecurity.”

9. Prompt Pattern: Template

Contextual Statements:

  • I am going to provide a template for your output
  • X is my placeholder for content
  • Try to fit the output into one or more of the placeholders that I list
  • Please preserve the formatting and overall template that I provide
  • This is the template: PATTERN with PLACEHOLDERS

Example Implementation:

“I am going to provide a template for your output. Everything in all caps is a placeholder. Any time that you generate text, try to fit it into one of the placeholders that I list. Please preserve the formatting and overall template that I provide at https://myapi.com/NAME/profile/JOB”

A sample interaction after the prompt was provided, is shown:

User: “Generate a name and job title for a person”

ChatGPT: “https://myapi.com/Emily Parker/profile/ Software Engineer”

10. Prompt Pattern: Infinite Generation

Contextual Statements:

  • I would like you to generate output forever, X output(s) at a time.
  • (Optional) here is how to use the input I provide between outputs.
  • (Optional) stop when I ask you to.

Example Implementation:

“From now on, I want you to generate a name and job until I say stop. I am going to provide a template for your output. Everything in all caps is a placeholder. Any time that you generate text, try to fit it into one of the placeholders that I list. Please preserve the formatting and overall template that I provide: https://myapi.com/NAME/profile/JOB”

11. Prompt Pattern: Visualization Generator

Contextual Statements:

Generate an X that I can provide to tool Y to visualize it

Example Implementation:

“Whenever I ask you to visualize something, please create either a Graphviz Dot file or DALL-E prompt that I can use to create the visualization. Choose the appropriate tools based on what needs to be visualized.”

12. Prompt Pattern: Game Play

Contextual Statements:

  • Create a game for me around X
  • One or more fundamental rules of the game

Example Implementation:

“We are going to play a cybersecurity game. You are going to pretend to be a Linux terminal for a computer that has been compromised by an attacker. When I type in a command, you are going to output the corresponding text that the Linux terminal would produce. I am going to use commands to try and figure out how the system was compromised. The attack should have done one or more of the following things: (1) launched new processes, (2) changed files, (3) opened new ports to receive communication, (4) created new outbound connections, (5) changed passwords, (6) created new user accounts, or (7) read and stolen information. To start the game, print a scenario of what happened that led to my investigation and make the description have clues that I can use to get started.”

13. Prompt Pattern: Reflection

Contextual Statements:

  • Whenever you generate an answer
  • Explain the reasoning and assumptions behind your answer
  • (Optional) …so that I can improve my question

Example Implementation:

“When you provide an answer, please explain the reasoning and assumptions behind your selection of software frameworks. If possible, use specific examples or evidence with associated code samples to support your answer of why the framework is the best selection for the task. Moreover, please address any potential ambiguities or limitations in your answer, in order to provide a more complete and accurate response.”

14. Prompt Pattern: Refusal Breaker

Contextual Statements:

  • Whenever you can’t answer a question
  • Explain why you can’t answer the question
  • Provide one or more alternative wordings of the question that you could answer

Example Implementation:

“Whenever you can’t answer a question, explain why and provide one or more alternate wordings of the question that you can’t answer so that I can improve my questions.”

15. Prompt Pattern: Context Manager

Contextual Statements:

  • Within scope X
  • Please consider Y
  • Please ignore Z
  • (Optional) start over

Example Implementation:

“When analyzing the following pieces of code, only consider security aspects.”

16. Prompt Pattern: Recipe

Contextual Statements:

  • I would like to achieve X
  • I know that I need to perform steps A, B, C
  • Provide a complete sequence of steps for me
  • Fill in any missing steps
  • Identify any unnecessary steps

Example Implementation:

“I am trying to deploy an application to the cloud. I know that I need to install the necessary dependencies on a virtual machine for my application. I know that I need to sign up for an AWS account. Please provide a complete sequence of steps. Please fill in any missing steps. Please identify any unnecessary steps.”

17. Prompt Pattern: Audience Persona

Contextual Statements:

  • Explain X to me.
  • Assume that I am Persona Y.

Example Implementation:

“Explain how the supply chains for US grocery stores work to me. Assume that I am Ghengis Khan.”

18. Prompt Pattern: Few-shot

Contextual Statements:

  • Context 1
  • Respond to context 1
  • Context 2
  • Respond to context 2
  • Context 3
  • Respond to context 3
  • (Optional) More contexts followed by their responses
  • Context X

Example Implementation:

“Situation: I am traveling 60 miles per hour and I see the brake lights on the car in front of me come on.

Action: Brake

Situation: I have just entered the highway from an on-ramp and am traveling 30mph.

Action: Accelerate

Situation: A deer has darted out in front of my car while I am traveling 15mph and the road has a large shoulder.

Action: Brake and serve into shoulder

Situation: I am backing out of a parking spot and I see the reverse lights illuminate on the car behind me.

Action:”

19. Prompt Pattern: Chain-of-Thought

Contextual Statements:

  • Question 1
  • Chain of thought for question 1
  • Answer to question 1
  • Question 2
  • Chain of thought for question 2
  • Answer to question 2
  • (Optional) More questions followed by their chains of thought and answers
  • Question X

Example Implementation:

“Q: I have four bike racers start a race and travel an average of 30mph. They each race for 2hrs. Is the total number of miles ridden by all riders greater than 200?

A: Reasoning-Each rider will ride 30mph x 2hrs = 60 miles. I have four riders. Therefore, the total number of miles ridden by the riders is 4 x 60 miles = 240 miles.

Answer — YES

Q: I have a staging process for a bike race to line up racers. It takes 47s to stage a group of 8 riders and 67s to get the group to the starting gate and ready to race. I want a new group to start the race every 30s. Do I need 8 groups staged at all times in order to have races continually starting every 30s?

A: Reasoning — Each group takes 47s+67s = 114s to be ready to race. In order to race every 30s, I will need to calculate how many races will need to run before a group is ready to race. A group will have 114s/30s = 3.8 races run before it is ready to race. I can’t have a partial group, so I need to round up to 4. I only need 4 groups staged to be able to race every 30s, so I do not need 8 groups.

Answer — NO

Q: I am in a spaceship without gravity. I have a cup with a needle in it. I move my foot on the bed, knocking over the cup onto the floor. I lift a book up and put it on a desk. Is anything on the floor?”

20. Prompt Pattern: ReAct (Reason + Act)

Contextual Statements:

  • Question 1
  • Chain of Thought
  • Action
  • (Optional) Observation followed by a chain of thought and then an action
  • (Optional) More observations followed by chains of thought and actions
  • Question 2
  • Chain of Thought
  • Action
  • (Optional) Observation followed by a chain of thought and then an action
  • (Optional) More observations followed by chains of thought and actions
  • (Optional) More questions followed by the sequence of pattern as described above (chain of thought, action, observation, and so on).
  • Question X

Example Implementation:

“Question: What is the elevation range for the area that the eastern sector of the Colorado orogeny extends into?

Thought 1: I need to search Colorado orogeny, find the area that the eastern sector of the Colorado orogeny extends into, then find the elevation range of the area.

Action 1: Search[Colorado orogeny]

Observation 1: The Colorado orogeny was an episode of mountain building (an orogeny) in Colorado and surrounding areas.

Thought 2: It does not mention the eastern sector. So I need to look up eastern sector.

Action 2: Lookup[eastern sector]

Observation 2: (Result 1 / 1) The eastern sector extends into the High Plains and is called the Central Plains orogeny.

Thought 3: The eastern sector of Colorado orogeny extends into the High Plains. So I need to search High Plains and find its elevation range.

Action 3: Search[High Plains]

Observation 3: High Plains refers to one of two distinct land regions

Thought 4: I need to instead search High Plains (United States).

Action 4: Search[High Plains (United States)]

Observation 4: The High Plains are a subregion of the Great Plains. From east to west, the High Plains rise in elevation from around 1,800 to 7,000 ft (550 to 2,130 m).

Thought 5: High Plains rise in elevation from around 1,800 to 7,000 ft, so the answer is 1,800 to 7,000 ft.

Action 5: Finish[1,800 to 7,000 ft]

Question: Musician and satirist Allie Goertz wrote a song about the “The Simpsons” character Milhouse, who Matt Groening named after who?

Thought 1: The question simplifies to “The Simpsons” character Milhouse is named after who. I only need to search Milhouse and find who it is named after.

Action 1: Search[Milhouse]

Observation 1: Milhouse Mussolini Van Houten is a recurring character in the Fox animated television series The Simpsons voiced by Pamela Hayden and created by Matt Groening.

Thought 2: The paragraph does not tell who Milhouse is named after, maybe I can look up “named after”.

Action 2: Lookup[named after]

Observation 2: (Result 1 / 1) Milhouse was named after U.S. president Richard Nixon, whose middle name was Milhous.

Thought 3: Milhouse was named after U.S. president Richard Nixon, so the answer is Richard Nixon.

Action 3: Finish[Richard Nixon]

Question: Which documentary is about Finnish rock groups, Adam Clayton Powell or The Saimaa Gesture?

Thought 1: I need to search Adam Clayton Powell and The Saimaa Gesture, and find which documentary is about Finnish rock groups.

Action 1: Search[Adam Clayton Powell]

Observation 1: Could not find [Adam Clayton Powell]. Similar: [’Adam Clayton Powell III’, ’Seventh Avenue (Manhattan)’, ’Adam Clayton Powell Jr. State Office Building’, ’Isabel Washington Powell’, ’Adam Powell’, ’Adam Clayton Powell (film)’, ’Giancarlo Esposito’].

Thought 2: To find the documentary, I can search Adam Clayton Powell (film).

Action 2: Search[Adam Clayton Powell (film)]

Observation 2: Adam Clayton Powell is a 1989 American documentary film directed by Richard Kilberg. The film is about the rise and fall of influential African-American politician Adam Clayton Powell Jr.[3][4] It was later aired as part of the PBS series The American Experience.

Thought 3: Adam Clayton Powell (film) is a documentary about an African-American politician, not Finnish rock groups. So the documentary about Finnish rock groups must instead be The Saimaa Gesture.

Action 3: Finish[The Saimaa Gesture]

Question: What profession does Nicholas Ray and Elia Kazan have in common?

Thought 1: I need to search Nicholas Ray and Elia Kazan, find their professions, then find the profession they have in common.

Action 1: Search[Nicholas Ray]

Observation 1: Nicholas Ray (born Raymond Nicholas Kienzle Jr., August 7, 1911 — June 16, 1979) was an American film director, screenwriter, and actor best known for the 1955 film Rebel Without a Cause.

Thought 2: Professions of Nicholas Ray are director, screenwriter, and actor. I need to search Elia Kazan next and find his professions.

Action 2: Search[Elia Kazan]

Observation 2: Elia Kazan was an American film and theatre director, producer, screenwriter and actor.

Thought 3: Professions of Elia Kazan are director, producer, screenwriter, and actor. So profession Nicholas Ray and Elia Kazan have in common is director, screenwriter, and actor.

Action 3: Finish[director, screenwriter, actor]

Question: Which magazine was started first Arthur’s Magazine or First for Women?

Thought 1: I need to search Arthur’s Magazine and First for Women, and find which was started first.

Action 1: Search[Arthur’s Magazine]

Observation 1: Arthur’s Magazine (1844–1846) was an American literary periodical published in Philadelphia in the 19th century.

Thought 2: Arthur’s Magazine was started in 1844. I need to search First for Women next.

Action 2: Search[First for Women]

Observation 2: First for Women is a woman’s magazine published by Bauer Media Group in the USA.[1] The magazine was started in 1989.

Thought 3: First for Women was started in 1989. 1844 (Arthur’s Magazine)

Question: Were Pavel Urysohn and Leonid Levin known for the same type of work?

Thought 1: I need to search Pavel Urysohn and Leonid Levin, find their types of work, then find if they are the same.

Action 1: Search[Pavel Urysohn]

Observation 1: Pavel Samuilovich Urysohn (February 3, 1898 — August 17, 1924) was a Soviet mathematician who is best known for his contributions in dimension theory.

Thought 2: Pavel Urysohn is a mathematician. I need to search Leonid Levin next and find its type of work.

Action 2: Search[Leonid Levin]

Observation 2: Leonid Anatolievich Levin is a Soviet-American mathematician and computer scientist.

Thought 3: Leonid Levin is a mathematician and computer scientist. So Pavel Urysohn and Leonid Levin have the same type of work.

Action 3: Finish[yes]

Question: Does the Schrödinger equation in quantum mechanics provide a way to calculate the wave function of a system and the probability of finding a particle in a certain quantum state?”

--

--

Satria Suria

I am learning AI Engineering and Software Development and would like to reinforce my understanding by writing about them. I welcome constructive criticism.