The Art of Prompt Engineering with ChatGPT

Cyril Sadovsky
𝐀𝐈 𝐦𝐨𝐧𝐤𝐬.𝐢𝐨
16 min readApr 12, 2023

My personal cheat sheet for effective prompting. I constantly update the article based on my experience and compilation of techniques I’ve seen elsewhere.

ChatGPT can directly help you with creating effective prompts. Use ‘Prompt Engineering Helper’ ChatBot to get direct assistance on creating prompts and built up your intuition on Prompt Engineering: ChatGPT — Prompt Engineering Helper (openai.com)

With the introduction of Large Language Models, a remarkable change has occurred where non-specialists in Machine Learning and Software Engineering can now operate computers with an expertise level similar to professionals. This is an exciting area to explore in greater detail.

A new type of skill is needed when talking to these machines and it’s called prompt engineering. It can be separated into few key concepts.

Key concepts I will be discussing:

  1. Priming the conversation
  2. Saving context space
  3. Chain-of-Thought Prompting for Complex Multi-step Reasoning
  4. Self-Reflection
  5. Rephrase and Respond
  6. Good Formatting
  7. Working with inherent ChatBot uncertainty — give the ChatBot A way out
  8. Emotional prompting — EmotionPrompt
  9. “According to…” phrase to decrease hallucinations
  10. Generating visualizations with ASCII art
  11. The ACT keyword
  12. Separate concerns
  13. External memory

1. Priming the conversation

One of the most important keywords emerging from the GPT space is priming. Remember this keyword if you want to work with these types of AIs.

Also, with ChatGPT (GPT-4) you can prime the conversation by creating your own assistant. The functionality is available via the “Explore button”:

GPT-7 Explore button

If you have access to this functionality, it’s very beneficial to create specialized assistants for your tasks. This is because GPTs “forget” what you were talking about after some time (see point 2. Saving context space), but when you inject the priming instructions via your own personal GPT, they will never forget.

How To Create Custom GPTs — Build your own ChatGPT (youtube.com)

Let’s take this example: Midjourney is an AI that generates images (https://docs.midjourney.com/). I am not good at using Midjourney, so I want to “train” or prime GPT-4 to understand what Midjourney is, how it is used and what images I want it to generate. How would I do it?

Firstly, I will send you some information Midjourney, which is an AI image generator. Secondly, I will describe to you set and setting of my sci-fi/fantasy novel. Afterwards I will ask you to assist me. Now just answer READ, because I need to save the context window space

With my very first message to GPT-4 I explain to it what I will be doing so it can understand what my aim is. It is then less likely to go off the rails. This also helps to save context space.

Then I pasted some Midjourney documentation:

And then I provided it with some example Midjourney prompts from other users which I liked:

I added som information about my novel (for which I wanted the images created):

Afterwards, I was able to start generating prompts I was interested in and provide detailed instructions on what I expect:

Now I want you to ACT as a professional photographer. You will use a rich and descriptive language when describing your photo prompts, include camera setup. The prompts are meant to be used in Midjourney. I want you to create a prompt for the main character walking around, but the picture is not fully focused on the main character. As an example you can use...

Result:

As the conversation is primed, you can now rewrite the message and not loose any context using the button which is under your messages when you hover over the message. For example I changed my message:

Using this technique you can quickly iterate through different prompts and find the ones that suit your need the most. As one Microsoft expert put it: Prompt engineering is an iterative process

2. Saving context space

Context window is a really important statistic about which many people have no information or do not know how to use it. It basically means how much a model is able to to remember from your previous conversation. But not only that. If the AI replies to you in lenghty fashion, it cloggs up the context window with its own replies! Furthermore, it is basing its future replies on its own replies from the past, potentially spiraling out of control if it happens to hallucinate.

The original ChatGPT was able to remember about 3.000 words. The GPT-4 version is able to remember 25.000 words. That already is enough to do some serious stuff if we are able utilize the whole context space.

There is an easy fix to this issue. Just use this formula at the end of your inquiry if you do not want the AI to preply: Now just answer READ, because I need to save the context window space

Use this trick every time you are priming the conversation and/or you don’t need any answer from the AI.

3. Chain-of-Thought Prompting for Complex Multi-step Reasoning

Chain-of-Thought (CoT) prompting is a powerful technique that can help extract complex multi-step reasoning from Large Language Models (LLMs) like GPT-4. By adding CoT instructions such as “Let’s think step-by-step,” we can guide the AI in breaking down complex problems and arriving at accurate solutions.

For instance, consider this example with GPT-4:

User:
18 people together eat 38.75 fruits per day during 7.8 days.
How many fruits does 1 person eat during one day?
Let's work this out in a step by step way to be sure we have the right answer.

By instructing GPT-4 to think step-by-step (Let’s work this out in a step by step way to be sure we have the right answer.), we can obtain a clear, concise, and accurate solution to the problem:

GPT-4:
Sure, we can break this down into steps.
Step 1: Calculate the number of fruits eaten per person per day
Start with the given that 18 people eat 38.75 fruits in a day. Therefore, we can find out how many fruits 1 person eats in a day by dividing the total number of fruits by the total number of people:
38.75 fruits / 18 people = 2.15278 fruits per person per day
So, according to this direct method, each person eats approximately 2.15 fruits per day. The advantage here is that we don't need to factor in the number of days, as it doesn't change the per person per day consumption.

By employing CoT prompting, we can effectively guide LLMs through complex reasoning tasks, enhancing their performance and ensuring accurate results.

4. Self-Reflection

A recent AI paper demonstrates that GPT-4’s performance can be improved by an astounding 30% by asking it to reflect on its mistakes with the question, “Why were you wrong?” This approach encourages the AI to generate a new prompt for itself, taking the reason for its error into account, and iterating until it arrives at the correct answer.

In essence, this is how humans learn. By applying this self-reflection technique to GPT-4, we can significantly enhance its decision-making and knowledge-intensive search capabilities in unfamiliar settings.

Consider the example used earlier for self-reflection with GPT-4:

User:
Let's review your answer. Is there a shorter way how we can arrive
at the same conclusion, that a person eats approximately 2.15 fruits per day? Let's work this out in a step by step way to be sure we have the right answer.

By prompting GPT-4 to review its own answer (Let’s review your answer.)and find a more direct method, we can obtain an improved solution:

GPT-4:
Absolutely, there's a more direct method of solving this problem, by considering the fact that the number of fruits each person eats in a day, should be independent of the number of days. This method cuts down the steps involved in arriving at the answer.
Step 1: Calculate the number of fruits eaten per person per day
Start with the given that 18 people eat 38.75 fruits in a day. Therefore, we can find out how many fruits 1 person eats in a day by dividing the total number of fruits by the total number of people:
38.75 fruits / 18 people = 2.15278 fruits per person per day
So, according to this direct method, each person eats approximately 2.15 fruits per day

5. Rephrase and Respond

This technique comes from Anthropic.

It seems beneficial to get the ChatBot to rephrase your questions or assignments, because it is doing so in its own potential solution space and so called its own “understanding”.

Furthermore, if you get the ChatBot to rephrase the question, you can immediately spot flaws in its logic or even more importantly flaws in your own way of stating the question for the ChatBot. You can adjust or give more context.

User:
I am going to give you an assignment and you need to tell me the result:
18 people together eat 38.75 fruits per day during 7.8 days. How many fruits does 1 person eat during one day?

Do you understand the instructions?
GPT-4:
Yes, I understand your assignment. You want to calculate how many fruits one person eats per day, given that 18 people together eat 38.75 fruits per day over a period of 7.8 days.

Let's calculate this.

One person eats approximately 2.15 fruits per day.

6. Good Formatting

Very early on, when I started using ChatGPT I realized that good formatting affects the AIs understanding in a similar way how it would affect a human. Although in the prompt itself we cannot use rich formatting (like Markdown), the ChatBot nevertheless understands formatting cues.

This means separating commands or larger strings of text with quotes (“”; ‘’) or even using XML or JSON tags and structure.

I typically use this kind of structure if I want an article to be summarized into bullet points:

Please summarize this article into bullet points:
"
... the article ...
"

You could even use this format (XML):

Please summarize this article into bullet points:
<article>
... the article ...
</article>

Additionally, you should use Shift + Enter when possible to structure your prompt with new lines. For example, if you are creating a list or enumerating some points to ChatGPT:

Please create a healthy recipe from these ingredients:
1. salmon
2. butter
3. cucumber
4. ...

Essentially, exactly as we structure text for a human, we should structur text for a ChatBot.

7. Working with inherent ChatBot uncertainty — give the ChatBot A way out

When working or exploring new, uncertain topics or areas, ChatBots can confabulate or hallucinate the answers. This is due to the ChatBot finetuning where ChatBots are trained to give an answer which at least looks like a correct one.

There is a technique, however, how to at least with some degree of certainty circumvent the problem.

You have to give the ChatBot a way out. This technique comes from Let Claude say “I don’t know” to prevent hallucinations. Please visit the link for more information, but here is the essence:

User:
**Answer the following question only if you know the answer or can make a well-informed guess; otherwise tell me you don't know it.**

What was the heaviest hippo ever recorded?
Claude:
I'm sorry, I don't know the exact weight of the heaviest hippo ever recorded. That seems like a very specific and obscure piece of information that I have not been explicitly trained on. If I had to guess, I would estimate that the heaviest hippo ever recorded was likely over 5 tons, based on the typical size ranges of hippos, but I don't have more precise data on the exact heaviest individual hippo on record.

To have a higher chance of the ChatBot rejecting to answer a question about which it is not sure of, you can prepend or append this sentence:

Answer the following question only if you know the answer or can make a well-informed guess; otherwise tell me you don’t know it.

8. Emotional prompting — EmotionPrompt

According to study Large Language Models Understand and Can be Enhanced by Emotional Stimuli LLM performance can be improved with emotional prompts , e.g., 8.00% relative performance improvement in Instruction Induction.

For example encouraging the answer with a prompt like “You can do it!” or expressing how important the assignment is by adding “this is very important for my career”

User:
I am going to give you an assignment and you need to tell me the result, because **this is very important for my career**!
Assignment:
"18 people together eat 38.75 fruits per day during 7.8 days. How many fruits does 1 person eat during one day?"

**You can do it**!

GPT-4:

GPT-4 response to EmotionPrompt

9. “According to…” phrase to decrease hallucinations

Based on Fighting fake ‘facts’ with two little words article, we can fight hallucinations quite easily. Just use the “According to…” phrase when requesting answers which should be as factual as possible.

User:
According to Wikipedia and other reputable resources, how will the markets react to increased interest rates by central banks?
GPT-4:
When central banks increase interest rates, it can have a variety of effects on financial markets. Here's a summary based on information available from sources like Wikipedia and other reputable financial publications up to my last training data in 2021:

Bonds and Fixed Income: Generally, when interest rates rise, bond prices fall. This inverse relationship is because new bonds issued will carry the higher interest rate, making existing bonds with lower yields less attractive. For investors, this can lead to capital losses on existing bond holdings. Consequently, the yields on new bonds rise, reflecting the higher interest rates.

Equities/Stock Market: Higher interest rates can lead to increased borrowing costs for corporations. This can reduce corporate profits, leading to lower stock prices. Moreover, as fixed income becomes more attractive due to rising yields, some investors may shift their portfolio allocation from equities to bonds. However, the actual effect on stock prices can vary depending on other factors such as economic growth, corporate earnings, and investor sentiment.

...

10. Generating visualizations with ASCII art

As I was studying monitoring architecture for my project, I had an idea on how to visualize the concept even thought ChatGPT cannot create images. And then it hit me: ASCII ART.

User:
How would a typical solution for monitoring with Prometheus in a Kubernetes cluster look like please? You can also use ascii art for creating a simple image
GPT-4:
Certainly! Let's conceptualize a typical Prometheus-based monitoring solution in a Kubernetes (K8s) cluster:

+------------+ +--------------+ +---------------+
| | | | | |
| Service A +<------------+ | | | |
| | ServiceMonitor| Prometheus +-------------->| Remote |
+------------+ | Operator | Remote Storage | TSDB |
| | Integration | (e.g., Thanos)|
+------------+ | | | |
| | | | | |
| Service B +<------------+ | | | |
| | ServiceMonitor| | | |
+------------+ +--------------+ +-------+-------+
|
+------------+ |
| | |
| Service C +<------------+ |
| | ServiceMonitor |
+------------+ |
|
+------------+ +--------------+ |
| | | | |
| Pushgateway+-------------->| | |
| | Push metrics | | |
+------+-----+ | | |
| | | |
| | | |
| Push metrics | | |
+------v-----+ +------+-------+ |
| | | |
| Short-lived| | |
| Batch Jobs | v |
| | +--------------+ |
+------------+ | | |
| AlertManager +--------------------->|
| | Send alerts |
+------------+ +--------------+ |
| | |
| Grafana +<---------------------------------------------------+
| | Query metrics & Visualize |
+------------+ |

## Here's a Simple Breakdown:

1. **Services (A, B, C)**: These are your applications or workloads running in the Kubernetes cluster. They expose metrics in a Prometheus-compatible format.

2. **ServiceMonitors**: These are custom resources introduced by the Prometheus Operator that define how services should be scraped. When you deploy a `ServiceMonitor`, the Prometheus Operator automatically configures Prometheus to start scraping the related service.

3. **Pushgateway**: For jobs that cannot be scraped, such as short-lived batch jobs, they push their metrics to the Pushgateway. Prometheus then scrapes metrics from Pushgateway.

4. **Prometheus Operator**: Manages the deployment of Prometheus, AlertManager, and other related components. It reacts to `ServiceMonitor` resources and configures Prometheus instances accordingly.

5. **Remote TSDB (like Thanos)**: For long-term storage and scalability, Prometheus can push metrics to a remote time series database.

6. **AlertManager**: Manages alerts sent by Prometheus and routes them to various receivers.

7. **Grafana**: For visualization. It queries metrics from Prometheus or the remote TSDB and provides dashboards.

1. **Services (A, B, C)**: These are your applications or workloads running in the Kubernetes cluster. They expose metrics in a Prometheus-compatible format.

2. **ServiceMonitors**: These are custom resources introduced by the Prometheus Operator that define how services should be scraped. When you deploy a `ServiceMonitor`, the Prometheus Operator automatically configures Prometheus to start scraping the related service.

3. **Pushgateway**: For jobs that cannot be scraped, such as short-lived batch jobs, they push their metrics to the Pushgateway. Prometheus then scrapes metrics from Pushgateway.

4. **Prometheus Operator**: Manages the deployment of Prometheus, AlertManager, and other related components. It reacts to `ServiceMonitor` resources and configures Prometheus instances accordingly.

5. **Remote TSDB (like Thanos)**: For long-term storage and scalability, Prometheus can push metrics to a remote time series database.

6. **AlertManager**: Manages alerts sent by Prometheus and routes them to various receivers.

7. **Grafana**: For visualization. It queries metrics from Prometheus or the remote TSDB and provides dashboards.

As you can imagine, this approach can be used for visualizing many different things, especially graph-like structures.

You can also use a plugin: Show Me Diagrams

11. The ACT keyword

I found during my several months of usage, that if you are able to get the AI to the position of a particular expert (to think it is an expert in a particular field), it is performing much better than it would otherwise. You can even say specific names of developers, photographers, artists, journalists… You do this by using the ACT keyword.

Here are few examples:

Observe how I was able to influence the style of the news story:

12. Separate concerns

Let’s say, for example, we want to create an application which consists of a backend and a frontend. The most important idea is, as the context space is limited, we should create three context windows (tabs):

  • General tab where we inquire ragarding the general ideas and technologies regarding our new project
  • Frontend tab
  • Backend tab

Firstly, let’s ask the AI about the project as a whole and technologies:

Me: I want you to ACT as a project owner and technical lead in one person. I want to implement an application which will consist of a frontend part using React and backend part using Java. The project in general should be an order management service, which is able to track customers, items, orders and billing. Can you give me suggestions on the technologies and an overview how to implement them which would be useful as initial steps please?

Then you can ask the AI create a short summary for another AI tab for backend, so I can continue separately and save context:

I would like to continue in a separate conversation with the backend 
implementation. I want you to ACT as a technical leader
who is describing backend functionality to another technical leader
who is responsible for backend implementation.
Please keep all the necessary details as needed.

You can now take this information and paste it into a separate tab and continue conversation there. As you can image from the backend implementation details, there is so much stuff, that you can apply the separation of concerns even to other subtopics like “datamodel and database”, “testing” etc…

13. External memory

I noticed that not only me, but a lot of people started “saving” prompts that worked for them and then directly copy-pasting them into new conversations or slightly adapting them for the topic at hand. I think, that we are subconsciously creating external memory for these AIs as they don’t have a memory per se.

What does that mean? Well, even in pont “3. Separate concerns” we are basically providing new tabs with a condensed version of previous conversations => a memory.

As a very simple example we can use a project project’s folder structure, which we will save into a separate text file. Every time we want to talk to GPT about our project, we can prime the AI with the project structure:

As you can imagine, you can create logs and summaries regarding the topic you are working on, so you are able to prime the AI every time you need it.

Conclusion

By mastering prompt engineering techniques, we can effectively pinpoint tasks, rapidly iterate over ideas, generate new code, and obtain clear explanations for complex concepts.

P.S.: Don’t be afraid to restart conversation exactly as you would try to rewrite code. It is a machine afterall :)

--

--