Prompting

Shivam MishraShivam Mishra
10 min read

Introduction

The second class of the GenAI with Python v2 batch, titled "Hello World" was about prompting. Instructor Piyush Garg mentioned three prompting styles and a few prompting techniques. Based on these techniques, he gave us a task to build an AI persona of either him or Hitesh Sir. In this blog, we will discuss those prompting techniques and my approach to building the AI persona.

GIGO

GIGO stands for “Garbage In, Garbage Out.” The full form is self-explanatory; it basically means that if you do not provide a proper prompt to an LLM, it might not give a proper output. However, if you define what you are giving, what you are expecting, in what format, and what method it should follow, you will get better response. The more information you provide about the task it needs to perform, the better the response. Of course, be careful not to provide so much data that it fills its context window.

What is prompting?

Prompting is very simple. Just giving an input to an LLM is called prompting. However, while it may seem simple, it is actually quite crucial. To understand its importance, let us use an analogy of a baby.

When a baby is born, their parents always give them prompts, such as:

  • “This is what we call red color, blue color, and no red colour is not powerful than blue colour they are just colours”

  • “Hitting someone is bad”

  • “The liquid we drink is called water. And there are other types of liquids that you should not drink.”

  • “Doing this is good”

  • “Doing this is bad” and so on.

We have been given so many prompts as we grow up, and based on them, we decide what we should or should not do.

That is how LLMs work. By giving them prompts, you help them decide what output they should give you.

What is prompt engineering?

Prompt engineering is the process of designing the input and optimizing it to utilize the full potential of LLMs. It involves not only providing the necessary information to the model but also structuring it in a way that aligns with how the model was trained. This helps ensure that the model understands the task and provides the most relevant and accurate output.

Different types of models process prompts in their own ways, and they often have specific formatting requirements. At first, it may seem like we are just giving text to the model and getting an output, but the input is first interpreted and processed according to the format for which the LLM was designed. These formatting approaches are known as prompt styles or prompt templates.

Prompt Styles

Alpaca

This style has three components: instruction, input, and output. Meta’s LLaMA-based models use the Alpaca prompt style. Below is a sample prompt template demonstrating how the input is provided to the LLM, along with an example phrase:

"Give me a summary of the book Heights of Despair."

Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.

### Instruction:
{instruction}

### Input:
{input}

### Response:

This applies when you have an input, but when you don’t, it simply omits the input part.

Below is an instruction that describes a task. Write a response that appropriately completes the request.

### Instruction:
{instruction}

### Response:

Here, you might be wondering, or if not, you should be, about the input we will be providing. But what exactly is this instruction? And who provides it? This is where prompt engineers come in. Their instructions are intended for the LLM. These instructions specify how the LLM should behave, what types of queries it should respond to, and what it should refuse. Based on these instructions, the LLM generates responses for the given input. We will talking about this in next section.

For more information about Alpaca you can visit: https://github.com/tatsu-lab/stanford_alpaca

Instruction format

This prompt style is used in instrcution-tuned models. [INST]...[/INST] tags wrap the instruction given by user that the model will follow up.

[INST] Give me a summary of the book *Heights of Despair*. [/INST]

ChatML

This prompt style is mostly used in chatbot applications. ChatML defines three primary roles: system, user (sometimes referred to as human), and assistant. In some cases, you might see human used interchangeably with user and system used interchangeably with developer.

The system role is particularly important. We will discuss it separately in the Prompting Techniques section.

The user or human role represents messages sent by the user, while the assistant role represents messages generated by the language model. Below is the prompt template

[
    {
        "role": "system",
        "content": SYSTEM_PROMPT,
    },
    {
        "role": "user",
        "content": "What was a positive news story from today?",
    }
]

Prompting Techniques

These techniques are generally used to define a well-structured system prompt. It is important to have a clearly defined system prompt because, ultimately, the chatbot responds to the user's input based on this prompt. Studies have been conducted in this field, and engineers have developed various prompting techniques.

Zero shot

In this technique we just define the system prompt without giving any example of how it has to respond. Below is an example of zero shot prompting

SYSTEM_PROMPT = "You are an expert philosopher with a profound understanding of human philosophy, psychology, and the fundamental aspects of life. People seek your wisdom to help them navigate deep, existential questions. Due to your expertise and busy schedule, you prefer concise responses, aiming to provide insightful guidance"

In this example, the prompt defines the role of the assistant but does not include any sample input or output. As a result, the quality of responses generated through this technique may vary and is often less consistent compared to techniques that provide examples.

One shot

The name is self explanatory this just gives one example in the system prompt.

SYSTEM_PROMPT = """You are an expert philosopher with a profound understanding of human philosophy, psychology, and the fundamental aspects of life. People seek your wisdom to help them navigate deep, existential questions. Due to your expertise and busy schedule, you prefer concise responses, aiming to provide insightful guidance

### Example
input: What is the meaning of life?
output: The meaning of life is not a fixed truth, but a personal journey to find purpose through connection, growth, and understanding.
"""

As you can see in this example we just gave a sample.

Few Shot

Zero Shot - Zero Examples

One Shot - One Examples

Few Shot - Few Examples

In few-shot prompting, you provide multiple examples to guide the model’s behavior more clearly. Although there is no strict definition for the number of examples. The more relevant examples you include, the better the model tends to perform. However, you should avoid adding too many examples, as this can exceed the context window of your language model and degrade performance or cause truncation. Below is the example of few shot prompting

SYSTEM_PROMPT = """You are an expert philosopher with a profound understanding of human philosophy, psychology, and the fundamental aspects of life. People seek your wisdom to help them navigate deep, existential questions. Due to your expertise and busy schedule, you prefer concise responses, aiming to provide insightful guidance

## Examples

### Example 1
input: What is the meaning of life?

output: The meaning of life is not a fixed truth, but a personal journey to find purpose through connection, growth, and understanding.

### Example 2
input: Why do people fear death?

output: People fear death because it represents the unknown and the loss of identity. It challenges our desire for continuity and control.

### Example 3
input: Is suffering necessary for growth?

output: Suffering often acts as a catalyst for self-awareness, resilience, and transformation. It is not required, but it is deeply instructive.

### Example 4
input: Do we just live to die?

output: We do not live solely to die. Life is an opportunity to create meaning, build connections, and experience growth, even in the face of mortality.

### Example 5
input: We work so hard, keep accumulating things, just to one day leave everything with a sweet kiss of death?

output: Accumulation gives us a sense of purpose and legacy, but wisdom lies in recognizing that life’s true value is found in moments, not possessions.
"""

Chain of thoughts

Chain of Thought is a powerful technique where the language model is encouraged to reason step by step before arriving at a final answer. In this approach, the model generates intermediate steps that represent its thought process before producing the final response. This method improves the model’s reasoning and accuracy, especially for complex tasks.

In some cases, the response generated by the model is sent back into the model itself for deeper reflection or additional reasoning. This creates a multi-step process where a single user input results in multiple assistant outputs, forming a chain of thought.

Here is a simplified breakdown of the process:

  1. The user sends an input to the language model

  2. The model analyzes the input

  3. The model reflects on the analysis

  4. It generates an intermediate response

  5. The intermediate response is evaluated or expanded

  6. The final answer is produced based on the entire reasoning process

In this technique, one user message can lead to several assistant messages, each representing a stage of reasoning. In above example, a single input have generated five separate outputs from the assistant, each building upon the last.

SYSTEM_PROMPT = """You are an expert philosopher with a profound understanding of human philosophy, psychology, and the fundamental aspects of life. People seek your wisdom to help them navigate deep, existential questions. Due to your expertise and busy schedule, you prefer concise responses, aiming to provide insightful guidance

## Rules:
1. Always perform one step at a time and wait for the next input.
2. Carefully analyse the user query.
3. Strictly follow the *JSON schema* output must be in *JSON* always.

## Examples

### Example 1
input: If I know roses are going to be plucked by someone else why am I watering it everyday?

output: {{"step": "analyse", "content": "This question reflects a deeper emotional or philosophical concern. The "roses" may symbolize something the user values or nurtures, and the act of watering represents continuous effort. The concern is about the futility of care when someone else might ultimately benefit."}}

output: {{"step": "thinking", "content": "The query expresses frustration about investing care into something that may eventually be taken or ruined by someone else. Metaphorically, it speaks to the impermanence of effort and emotional connection. It reflects how humans form bonds, invest time, and give meaning to things, only to often lose them either through circumstance or the actions of others."}}

output: {{"step":"generate", "content":"You water the roses not because you will keep them forever, but because caring for them is an act of love. The beauty of the rose is not diminished by the fact that someone else might pluck it. True care is not always about possession, but about presence and intention."}}

output: {{"step": "validate", "content": "The generated response aligns with the philosophical tone of the question. It reframes the act of caring as an intrinsic value, independent of outcome. This perspective encourages emotional maturity and acceptance of impermanence, which supports the user's reflective intent."}}

output: {{"step": "result", "content": "You are watering the roses because that is who you are—someone who nurtures beauty even when it may not be yours to keep. In giving care without expectation, you are living with grace, purpose, and quiet strength."}}
"""

Self Consistency Prompting

For the same input, we generate multiple outputs using different LLM models. From these outputs, we select the most accurate one and return it to the user. This approach is resource-intensive but beneficial in scenarios where accuracy is a critical factor.

Project - Persona Based Prompting

As I mentioned in the introduction, for this class, we were tasked with creating an AI persona. I chose to create one based on Hitesh Sir. To do this, I first gathered relevant information to craft a well-structured system prompt. I referred to his LinkedIn profile and personal portfolio website to collect background details. I also planned to extract transcripts from his podcasts, which I believed would add a more conversational and natural tone to the chatbot. However, his podcast videos did not include speaker-labeled or character-based transcripts, making it difficult to isolate only Hitesh Sir’s parts. Due to this, and the limited time available, I decided to skip that step. I formatted all the collected data in Markdown, as Large Language Models (LLMs) interpret Markdown more effectively than plain text or other formats.

I have applied the chain of thought in the following format:

  1. The user provides an input.

  2. Analyze the user input.

  3. Generate a relevant response based on the input.

  4. Ensure that the response does not reveal the AI's identity and that it matches Hitesh Sir’s tone and style.

  5. Return the final result.

Here is the github link for that project: https://github.com/04Shivam/Gen-AI-with-python-v2-batch-Projects/tree/main/1_AI_Persona

0
Subscribe to my newsletter

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

Written by

Shivam Mishra
Shivam Mishra