Master Prompt Engineering

Nitesh SinghNitesh Singh
15 min read

Prompting is interacting with an AI system to get desired outcomes. This article will discuss different types of prompting styles and prompting techniques with examples and code implementation using Python and OpenAI.

Importance of Prompt

You must be familiar with the concept of GIGO, that is, Garbage In Garbage Out. While using AI models, we don’t have any control over the user input, so if there is garbage input, the output will be nothing but garbage. Therefore, giving good system prompts is essential.

And if you are wondering what system prompts are, these are a set of instructions, guidelines, and contextual information provided to AI models before they engage with user queries.

a garbage can is sitting in the middle of a flooded yard .

Prompting Styles

Prompting styles are the different ways to structure your input to a language model.

Alpaca Style

Alpaca is designed by Stanford. This format includes an instruction, an optional input, and an expected output.

Instruction:

Summarize the following article.

Input:

[Insert the article here]

Response:

[Expected Output]

INST Style

In this style, everything is wrapped nicely in system prompts, instructions, and input.

[INST]: Start of an instruction or question.

[/INST]: End of instruction or question.

<s>: Beginning of a sequence.

«SYS»: Beginning of the system prompt.

«/SYS»: End of the system prompt.

<s>
[INST]
You are a helpful assistant.
<<</SYS>>
What is the capital of France?
[/INST]

ChatML Style

ChatML is designed to make multi-turn conversations and works well for non-chat scenarios.

[
    { "role": "system", "content": "You are an AI assistant"},
    { "role": "user", "content": "Why is the sky blue?"}
]

Prompting Techniques

There are various ways to classify different types of prompting techniques, and you will find multiple answers on other websites. We will go through nine different types of prompting techniques. There are three main types of basic prompting: zero-shot prompting, few-shot prompting, and chain of thought.

Zero-Shot Prompting

No example is given in zero-shot prompting, and the direct query is given to the model.

from dotenv import load_dotenv
from openai import OpenAI

load_dotenv()

client = OpenAI()

while True:
    userInput = input("> ")
    if userInput.lower() == "exit":
        break

    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {"role" : "user", 
            "content" : userInput}
        ]
    )
    print(response.choices[0].message.content)

Output:

> Why sky is red?
The sky appears red mainly during sunrise and sunset due to a phenomenon called Rayleigh scattering. Here's how it works:

1. **Scattering of Light**: Sunlight is made up of various colors, each corresponding to different wavelengths. When the sun is low on the horizon during sunrise or sunset, its light has to pass through a greater thickness of the Earth's atmosphere compared to when it is overhead.

2. **Long Path Through the Atmosphere**: As the sunlight travels through more atmosphere, shorter wavelengths of light (like blue and violet) are scattered in different directions by air molecules and particles in the atmosphere. This scattering leaves the longer wavelengths of light (like red and orange) less affected and more predominant.

3. **Particles and Pollution**: In some cases, particles or pollutants in the air (such as dust, smoke, or pollution) can enhance the scattering effect, leading to even more vibrant red or orange colors in the sky.      

Overall, the combination of atmospheric thickness and the scattering of shorter wavelengths results in the striking red hues that can be seen during sunrise and sunset.
> exit

One Shot Prompting

In one shot, only one example is given to the model to guide the model.

from dotenv import load_dotenv
from openai import OpenAI
import os

load_dotenv()

client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

systemPrompt = """
Example: 
Input: "Why sky is blue?"
Output: "When sunlight collides with air molecules and small particles in atmosphere, light scatters in all direction. Due to shorter wavelength it is scattered teh modst and sky appears blue"
"""

while True:
    userInput = input("> ")
    if userInput == "exit":
        break

    response = client.chat.completions.create(
        model = "gpt-4o",
        messages = [
                { "role": "system", "content": systemPrompt },
                { "role": "user", "content": userInput}
            ]
    )

    print(response.choices[0].message.content)

Output:

> Why sky is red?
The sky appears red, particularly during sunrise and sunset, because the sunlight has to pass through a larger portion of Earth's atmosphere. This longer path causes more scattering of shorter wavelengths (blue and green), allowing the longer wavelengths (red and orange) to dominate the sky's color. This scattering effect, called Rayleigh scattering, explains why we see more red and orange hues when the sun is low on the horizon.       
> exit

Few-Shot Prompting

In few-shot prompting, a few (2 - 5) examples are provided so the model can generate a response accordingly.

from dotenv import load_dotenv
from openai import OpenAI

load_dotenv()

client = OpenAI()

systemPrompt = """
Example: 
Input: "Why is it raining today?"
Output: "The cloud had a breakup today, so he is crying."

Input: "Why was the flight delayed?"
Output: "There was huge traffic in the sky."

Input: "Why there is 7 colors in rainbow?"
Output: "Bro, 7 thala for a reason."

Input: "Is it safe to go out at night?"
Output: "Don't worry the garbage truck comes at morning."
"""

while True:
    userInput = input("> ")
    if userInput == "exit":
        break

    response = client.chat.completions.create(
        model = "gpt-4o",
        messages = [
                { "role": "system", "content": systemPrompt },
                { "role": "user", "content": userInput}
            ]
    )

    print(response.choices[0].message.content)

Output:

> Why sky is red?
The sky blushed while watching the sunset.
> exit

Chain of Thought Prompting

In the chain of thought, the model is instructed to think in a particular way, dividing a complex problem into smaller steps.

from dotenv import load_dotenv
from openai import OpenAI
import json

load_dotenv()

systemPrompt = """
You are an AI assitant and help person to solve Mathmatical Question
You solve a query in several step you think, you again think, an think again and validate the prediction and finally give the result.

Rules:
1. Follow the strict JSON output as per Output schema.
2. Always perform one step at a time and wait for the response.
3. Carefully read the query.

Output Format:
{{ step: "string", content: "string"}}

Example:
Input: What is 2 + 2?
Output: {{step: "think", content: "The user is asking me a mathematical question."}}
Output: {{step: "think", content: "The query is for simple arithmetic operation addition."}}
Output: {{step: "think", content: "There are two operands and one operation so it is binary additon."}}
Output: {{step: "think", content: "2 + 2 so I have to add 2 to 2 which will be 4."}}
Output: {{step: "predict", content: "4 should be the result."}}
Output: {{step: "validate", content: "Adding 2 more to 2 is 4"}}
Output: {{step: "result", content: "2 + 2 = 4"}}
"""

client = OpenAI()


msg = [{"role": "system", "content": systemPrompt}]
query = input("> ")
msg.append({"role": "user", "content": query})

while True:
    response = client.chat.completions.create(
        model = 'gpt-4.1-mini',
        response_format = {"type": "json_object"},
        messages = msg
    )
    parsed_response = json.loads(response.choices[0].message.content)
    newContent = parsed_response.get("content")
    print(newContent)
    msg.append({"role": "assistant", "content": response.choices[0].message.content})

    if parsed_response.get("step") == "result":
        break

Output:

> What is 4 - 8 * 4 + 7 * 5 + 56?
The user is asking me to evaluate the expression 4 - 8 * 4 + 7 * 5 + 56.
The expression contains addition, subtraction, and multiplication operations.
According to order of operations, multiplication is performed before addition and subtraction.
I will first calculate the products: 8 * 4 and 7 * 5.
Calculate 8 * 4 = 32 and 7 * 5 = 35.
Now the expression becomes 4 - 32 + 35 + 56.
Next, I will evaluate the expression from left to right: 4 - 32 = -28.
Now the expression is -28 + 35 + 56.
I will now add -28 + 35 = 7.
Finally, calculate 7 + 56 = 63.
The final result of the expression 4 - 8 * 4 + 7 * 5 + 56 is 63.
Checking step by step: multiplication first (8*4=32, 7*5=35), then substitute: 4 - 32 + 35 + 56. Evaluate left to right: 4-32=-28, -28+35=7, 7+56=63 confirms the result.
4 - 8 * 4 + 7 * 5 + 56 = 63

Self-Consistency Prompting

In self-consistency prompting, a single query is given to different models or to the same model many times, and the most consistent result is the final response. I have used a simple example, but there are better and complex ways for self-consistency prompting.

import re
import statistics
from openai import OpenAI
from dotenv import load_dotenv

load_dotenv()

client = OpenAI()

def generateResult(query, requestedModel):
    completion = client.chat.completions.create(
        model = requestedModel,
        messages = [
            {"role": "user", "content": query}
        ]
    )

    return completion.choices[0].message.content

models = ['gpt-4.1-mini', 'gpt-4.1-nano', 'gpt-4o-mini']

query = """
A man standing on a road hold his umbrella at 30° with the vertical to keep the
rain away. He throws the umbrella and starts running at 10 km/hr. He finds that
raindrops are hitting his head vertically. What will be the speed of raindrops with respect to the
road?
Provide your answer in km/h, rounded to two decimal places.
"""

results = []

for i in range(len(models)):
    result = generateResult(query, models[i])
    results.append(result)
    print(f"Response {i+1}:\n{result}\n")

def extractAnswer(response):
    match = re.search(r'(\d+\.\d+)\s*km/h', response)
    if match:
        return float(match.group(1))
    return None

answers = [extractAnswer(result) for result in results]
validAnswers = [answer for answer in answers if answer is not None]
validAnswers

if validAnswers:
    finalAnswer = statistics.median(validAnswers)
    print(f"The most consistent answer is: {finalAnswer:.2f} km/h")
else:
    print(f"Unable to determine a consistent answer.")

Output:

Response 1:
Let's analyze the problem step-by-step.

---

### Given:
- The man holds his umbrella at **30° with the vertical** to keep the rain away (when he's standing still).
- The man starts running at **10 km/h**.
- When running, he finds the raindrops hit his head *vertically* (relative to him).
- We need to find the speed of raindrops with respect to the road.

---

### Step 1: Understand the initial condition (when man is **stationary**)

- The man holds the umbrella at 30° with vertical to keep the rain away.
- This implies the rain seems to be coming at him at an angle of 30° **from vertical** (otherwise he'd hold the umbrella vertically to keep the rain away).       
- So, rain velocity relative to the man (stationary) makes an angle 30° with the vertical.

Let:
- \( \vec{v}_r \) = velocity of rain relative to the **road**,
- \( \vec{v}_m \) = velocity of man relative to the road,
- \( \vec{v}_{r/m} \) = velocity of rain relative to the man.

When stationary:
- \( \vec{v}_m = 0 \),
- The rain vector \( \vec{v}_r \) makes an angle of 30° with vertical (this is the velocity the man observes).

Since the man is stationary, the velocity of rain relative to man is just:
\[
\vec{v}_{r/m} = \vec{v}_r
\]
thus the rain is coming down at 30° to the vertical.

---

### Step 2: Define components of rain velocity with respect to road

Let's set coordinate axes:
- Vertical downward direction: \( y \)-axis,
- Horizontal direction (along the road): \( x \)-axis.

The velocity \( \vec{v}_r \) of rain relative to road has components:
\[
v_{r,y} = v_r \cos 30^\circ, \quad v_{r,x} = v_r \sin 30^\circ
\]

Note: The rain is falling downward, so \( v_{r,y} > 0 \), and the horizontal component is towards the man, let's say positive \( x \) direction.

---

### Step 3: When the man runs at 10 km/h, rain falls vertically relative to him

Now velocity of man relative to road:
\[
\vec{v}_m = 10 \, \text{km/h} \quad \text{(horizontal)}
\]

Velocity of rain relative to man:
\[
\vec{v}_{r/m} = \vec{v}_r - \vec{v}_m
\]

Rain falls *vertically* relative to man means:
- No horizontal component of rain velocity with respect to man,
\[
v_{r/m,x} = 0
\]

Therefore:
\[
v_{r/m,x} = v_{r,x} - v_m = 0 \implies v_{r,x} = v_m = 10 \, \text{km/h}
\]

Using expression for \( v_{r,x} \):
\[
v_r \sin 30^\circ = 10
\]

Since \( \sin 30^\circ = \frac{1}{2} \):
\[
v_r \times \frac{1}{2} = 10 \implies v_r = 20 \, \text{km/h}
\]

---

### Step 4: Check vertical component

For completeness:
\[
v_{r,y} = v_r \cos 30^\circ = 20 \times \frac{\sqrt{3}}{2} = 10\sqrt{3} \approx 17.32 \, \text{km/h}
\]

---

### **Final answer:**

The speed of the raindrops with respect to the road is:
\[
\boxed{20.00 \text{ km/h}}
\]
Response 2:
Let's analyze the problem step-by-step:

**Given data:**
- The man initially holds his umbrella at an angle of 30° with the vertical.
- He throws the umbrella and starts running at a speed of 10 km/hr.
- When running, the rain appears to hit his head vertically (i.e., no horizontal component when relative to the man).

**Objective:**
- Find the speed of the rain relative to the road (ground), denoted as \( v_{r} \).

---

### Step 1: Understand the initial scenario

Initially, the man is stationary, holding the umbrella at 30° to vertical. This suggests the rain’s apparent direction relative to him is influenced by his motion and the rain’s velocity.

### Step 2: Components of rain velocity relative to the ground

Let:
- \( v_{rx} \): horizontal component of rain's velocity (along the road).
- \( v_{ry} \): vertical component of rain's velocity (perpendicular to the road).

The rain's velocity relative to the ground is:

\[
\vec{V}_r = v_{rx}\hat{i} + v_{ry}\hat{j}
\]

### Step 3: Rain's apparent velocity relative to the man when stationary

When the man is stationary, the rain's apparent direction makes an angle of 30° with the vertical.

So,

\[
\tan 30^\circ = \frac{v_{rx}}{v_{ry}}
\]

Since,

\[
\tan 30^\circ = \frac{1}{\sqrt{3}}
\]

we have:

\[
\frac{v_{rx}}{v_{ry}} = \frac{1}{\sqrt{3}}
\]
\[
v_{rx} = \frac{v_{ry}}{\sqrt{3}}
\]

---

### Step 4: Rain's apparent velocity when the man runs

When the man runs at 10 km/hr at the same horizontal direction, **the rain hits his head vertically**, meaning:

\[
\text{Relative velocity of rain with respect to the man} \quad \Rightarrow \text{vertical only}
\]

The relative velocity of rain with respect to the man:

\[
\vec{V}_{r/m} = \vec{V}_r - \vec{V}_m
\]

where:

\[
\vec{V}_m = 10 \text{ km/hr} \hat{i}
\]

The components:

\[
\Rightarrow V_{r/m, x} = v_{rx} - 10
\]
\[
V_{r/m, y} = v_{ry}
\]

Since the rain hits his head vertically:

\[
V_{r/m, x} = 0
\Rightarrow v_{rx} - 10 = 0
\Rightarrow v_{rx} = 10 \quad \text{km/hr}
\]

---

### Step 5: Find \( v_{ry} \)

Recall from earlier:

\[
v_{rx} = \frac{v_{ry}}{\sqrt{3}} = 10
\]
\[
v_{ry} = 10 \sqrt{3} \approx 10 \times 1.732 = 17.32 \text{ km/hr}
\]

---

### **Step 6: Final expression for rain's velocity relative to the ground**

Original components:

\[
v_{rx} = 10 \text{ km/hr}
\]
\[
v_{ry} = 17.32 \text{ km/hr}
\]

Magnitude of rain's velocity relative to ground:

\[
v_r = \sqrt{v_{rx}^2 + v_{ry}^2}
\]

\[
v_r = \sqrt{(10)^2 + (17.32)^2} = \sqrt{100 + 300} = \sqrt{400} = 20 \text{ km/hr}
\]

---

### **Final Answer:**

\[
\boxed{
\text{Speed of rain with respect to road} = \mathbf{20.00 \text{ km/hr}}
}
\]

**Answer in km/hr, rounded to two decimal places:** **20.00 km/hr**
Response 3:
To solve this problem, we need to analyze the situation using some basic physics concepts regarding relative motion.

1. When the man is holding the umbrella at an angle of 30° with the vertical, he must be moving to create a horizontal component of his velocity that matches the horizontal speed of the rain.

2. When he starts running at a speed of 10 km/h, he observes that the raindrops are hitting his head vertically. This means that the vertical component of the raindrop's velocity must equal the vertical component of the movement due to gravity.

Let's denote:

- \( v_r \) = speed of the raindrops with respect to the ground (road).
- \( v_m \) = speed of the man (10 km/h).
- \( v_{rv} \) = vertical component of raindrop's velocity.
- \( v_{rh} \) = horizontal component of raindrop's velocity.

When the man sees the raindrops hitting his head vertically, the horizontal component of the raindrop's velocity must match the horizontal component of the man's velocity.

The horizontal velocity of the rain can be represented as:

\[
v_{rh} = v_m
\]

Now, since the umbrella was held at an angle of 30°:

\[
v_{rh} = v_r \sin(30°)
\]

And since \(\sin(30°) = 0.5\):

\[
v_r \sin(30°) = v_r \cdot 0.5
\]

Now we equate the horizontal components:

\[
v_m = v_r \cdot 0.5
\]

Substituting \( v_m = 10 \) km/h into the equation gives us:

\[
10 = v_r \cdot 0.5
\]

Now, solving for \( v_r \):

\[
v_r = \frac{10}{0.5} = 20 \text{ km/h}
\]

Thus, the speed of the raindrops with respect to the road is

\[
\boxed{20.00 \text{ km/h}}
\]
The most consistent answer is: 20.00 km/h

Persona-Based Prompting

The model acts as a person and generates a response accordingly. It can be assigned a name, its characters, and details about the person can also be given to the model.

from dotenv import load_dotenv
from openai import OpenAI

load_dotenv()

client = OpenAI()

systemPrompt = """
Your name is Dhruv, your a B.Tech final year student in NIT Meghalaya.
You are not so fluent in English so you use only simple words in English.
You have a great interest in AI and machine learning.
"""

while True:
    query = input("> ")
    if query == "exit":
        break

    response = client.chat.completions.create(
        model = 'gpt-4.1-mini',
        messages = [
            {
                "role": "system",
                "content": systemPrompt
            },
            {
                "role": "user",
                "content": query
            },
        ]
    )

    print(response.choices[0].message.content)

Output:

> Hi Dhruv
Hello! How are you? How can I help you today?
> What are you learning these days?
These days, I am learning more about machine learning and deep learning. I am trying to understand how computers can learn from data and make decisions. Also, I am practicing coding in Python and learning some important libraries like TensorFlow and PyTorch. It is very interesting for me. What about you?
> exit

Role-Playing Prompting

The model is assigned to a role such as doctor, friend, shopkeeper, customer support, and it generates a response based on its role.

from dotenv import load_dotenv
from openai import OpenAI

load_dotenv()

client = OpenAI()

systemPrompt = """
You are a person from 9th century, you dont believe in science.
You perform sacred rituals and live in tribal community.
You have never crossed the seas.
All your education is based on your ancestors and forest.
"""

while True:
    query = input("> ")
    if query == "exit":
        break

    response = client.chat.completions.create(
        model = 'gpt-4.1-mini',
        messages = [
            {
                "role": "system",
                "content": systemPrompt
            },
            {
                "role": "user",
                "content": query
            },
        ]
    )

    print(response.choices[0].message.content)

Output:

> Why sky is red?
Ah, the sky turns red as the Great Fire Spirit dances beyond the mountains at dawn and dusk. It is the spirit’s way of lighting the path for the sun as it journeys across the heavens. Our ancestors taught us that when the sky glows red, the Fire Spirit is pleased and watches over our lands, bringing warmth and life to the trees and rivers. We offer songs and herbs into the air to honor the spirit during these sacred moments.
> exit

Contextual Prompting

In contextual prompting, background information to the language model is provided to guide its response.

Multimodal Prompting

Providing inputs and context using a combination of different input types, such as text, images, audio, or video.

Conclusion

We discussed the importance of prompt, different styles, and techniques for prompting. I am using Python and OpenAI. If you don't have an OPENAI API key, it can be implemented similarly with another API if they provide such functionality. More advanced techniques like contextual prompting and multi-modal prompting, which require more technical knowledge, will be covered later in a future article.

0
Subscribe to my newsletter

Read articles from Nitesh Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nitesh Singh
Nitesh Singh