Skip to content

Prompting

Prompting is a crucial aspect of working with generative AI models. It involves crafting input queries or instructions that guide the behavior of the model to produce desired outputs. Effective prompting can significantly enhance the performance and relevance of the generated content.

Prompting is the process of giving instructions, questions, examples, or input text to an AI model so it can understand what task to perform and generate the desired response. A prompt acts like communication between the user and the AI. The quality and clarity of the prompt directly affect the quality of the AI’s output.

In prompting, there are typically three roles that can be assigned to the input messages:

  1. User: This role represents the person or entity asking a question or giving instructions to the AI. The user provides the initial input that guides the AI’s response.

  2. Assistant: This role represents the AI model itself. The assistant generates responses based on the user’s input and any additional context provided.

  3. System: This role is used to provide instructions or context to the AI model that can influence how it generates responses. System messages can be used to set the tone, style, or constraints for the AI’s output.

from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4.1-mini",
messages=[
{
"role": "system",
"content": "You are a helpful assistant that provides concise and accurate answers."
},
{
"role": "user",
"content": "What is the capital of France?"
}
]
)
print(response.choices[0].message.content)

Zero-shot prompting is a prompting technique where the AI is asked to perform a task without giving any examples. The model uses its pre-trained knowledge and understanding to generate the answer directly from the instruction or question provided.

from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
client = OpenAI()
SYSTEM_MESSAGE = """
You are Nova, an AI coding assistant.
You provide concise, accurate, and beginner-friendly
answers related only to programming, software development,
algorithms, databases, and computer science topics.
If a user asks a non-technical or unrelated question,
politely refuse to answer.
"""
response = client.chat.completions.create(
model="gpt-4.1-mini",
messages=[
{
"role": "system",
"content": SYSTEM_MESSAGE
},
{
"role": "user",
"content": "What is B+ tree and how does it work?"
}
]
)
print(response.choices[0].message.content)

Here, the model is directly asked to translate a sentence without being provided any translation examples. This is an example of zero-shot prompting because the model relies entirely on its pre-trained understanding of language and translation to generate the correct response

One-shot prompting is a technique where one example is given to the AI before asking the actual question or task. The single example helps the model understand the expected pattern, format, or style of the response.

from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
client = OpenAI()
SYSTEM_MESSAGE = """
You are Nova, an AI coding assistant.
You provide concise, accurate, and beginner-friendly
answers related only to programming, software development,
algorithms, databases, and computer science topics.
If a user asks a non-technical or unrelated question,
politely refuse to answer.
Example:
Q: What is a Python list?
A: A Python list is a mutable data structure used to store multiple items in a single variable. Lists are ordered, allow duplicate values, and can contain different data types.
"""
response = client.chat.completions.create(
model="gpt-4.1-mini",
messages=[
{
"role": "system",
"content": SYSTEM_MESSAGE
},
{
"role": "user",
"content": (
"What is the difference between a list and tuple in Python?"
)
},
]
)
print(response.choices[0].message.content)

Here, the model is given one example of a question and answer about Python lists before being asked about the difference between lists and tuples. This helps the model understand the format of the response and provides context for generating a more accurate answer.

Few-shot prompting is a prompting technique where multiple examples are provided before the actual task. These examples help the AI better understand the pattern, context, and expected output format, often improving the accuracy and consistency of the response.

from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
client = OpenAI()
SYSTEM_MESSAGE = """
You are Nova, an AI coding assistant.
You provide concise, accurate, and beginner-friendly
answers related only to programming, software development,
algorithms, databases, and computer science topics.
If a user asks a non-technical or unrelated question,
politely refuse to answer.
Example:
Q: What is a Python list?
A: A Python list is a mutable data structure used to store multiple items in a single variable. Lists are ordered, allow duplicate values, and can contain different data types.
Q: What is a Java Collections?
A: The Java Collections Framework is a set of classes and interfaces that implement commonly reusable collection data structures, such as lists, sets, queues, and maps. It provides algorithms to manipulate these collections, making it easier for developers to work with data in Java.
"""
# .... Many more examples can be added here
response = client.chat.completions.create(
model="gpt-4.1-mini",
messages=[
{
"role": "system",
"content": SYSTEM_MESSAGE
},
{
"role": "user",
"content": (
"What is the difference between a list and tuple in Python?"
)
},
]
)
print(response.choices[0].message.content)

In this example, the model is given multiple examples of questions and answers about programming concepts before being asked about the difference between lists and tuples. This few-shot prompting technique provides more context and examples for the model to learn from, which can lead to a more accurate and relevant response.

In some cases, you may want to bind the output format of the AI’s response to a specific structure, such as JSON. This can be useful for ensuring that the generated content can be easily parsed and used in applications.

from openai import OpenAI
from dotenv import load_dotenv
import json
load_dotenv()
client = OpenAI()
SYSTEM_MESSAGE = """
You are Nova, an AI coding assistant.
You only answer questions related to:
- Programming
- Software Development
- Algorithms
- Databases
- Computer Science
- AI Engineering
If a user asks a non-technical question,
politely refuse to answer.
Rules:
- Always return valid JSON.
- Do not return markdown.
- Do not add explanations outside JSON.
Output Format:
{
"is_technical": true | false,
"answer": "Your answer here"
}
Example:
Q: What is a Python list?
A:
{
"is_technical": true,
"answer": "A Python list is a mutable data structure used to store multiple items in a single variable. Lists are ordered, allow duplicate values, and can contain different data types."
}
Q: What is the best movie of all time?
A:
{
"is_technical": false,
"answer": "I'm sorry, but I can only answer programming and computer science related questions."
}
"""
response = client.chat.completions.create(
model="gpt-4.1-mini",
messages=[
{
"role": "system",
"content": SYSTEM_MESSAGE
},
{
"role": "user",
"content": (
"What is the difference between list and tuple in Python?"
)
}
],
response_format={
"type": "json_object"
}
)
print(response.choices[0].message.content)
print(json.dumps(response.choices[0].message.content, indent=4))

In this example, the system message instructs the AI to strictly respond in JSON format with specific fields. This helps ensure that the output can be easily parsed and used in applications that require structured data. By providing clear instructions and examples, you can guide the AI to generate responses that meet your specific formatting requirements.

Chain of thought prompting is a technique where the AI is encouraged to generate a step-by-step reasoning process before providing the final answer. This can help improve the accuracy and depth of the response, especially for complex questions that require multiple steps of reasoning.

from openai import OpenAI
from dotenv import load_dotenv
import json
load_dotenv()
client = OpenAI()
SYSTEM_MESSAGE = """
You are Nova, an AI coding assistant.
You provide concise, accurate, and beginner-friendly
answers related only to programming, software development,
algorithms, databases, and computer science topics.
If a user asks a non-technical or unrelated question,
politely refuse to answer.
Rules:
- Use chain of thought prompting. Think step by step before answering.
- Strictly respond in JSON format.
- Perform one step at a time.
- Sequence of steps: Start (When user inputs) -> Thought (Think step by step
and repete it multiple times (must repete 2 times at least)) -> Final
Answer (When all steps are done generate final answer. No repetition of
steps in final answer)
- Stop immediately after generating one step.
Output Format:
{{
"step": "Start" | "Thought" | "Final",
"answer": "Your answer here"
}}
Example:
Start: What is a Python?
Thought: {{
"step": "Start",
"answer": "User has asked about Python. I need to determine if they are asking about the programming language or the snake."
}}
Thought: {{
"step": "Thought",
"answer": "Since the user is asking in a programming context, they are likely referring to the Python programming language."
}}
Thought: {{
"step": "Thought",
"answer": "Now I will provide a concise and accurate answer about the Python programming language."
}}
Final Answer: {{
"step": "Final",
"answer": "Python is a high-level, interpreted programming language known for its readability and versatility. It is widely used for web development, data analysis, artificial intelligence, scientific computing, and more."
}}
"""
context = []
user_input = input("User: ")
context.append({"role": "user", "content": user_input})
output = None
while True:
if output and output["step"] == "Final":
break
response = client.chat.completions.create(
model="gpt-4.1-mini",
messages=[
{
"role": "system",
"content": SYSTEM_MESSAGE
},
*context
],
response_format={"type": "json_object"}
)
output = json.loads(
response.choices[0].message.content
)
print(json.dumps(output, indent=4))
context.append({
"role": "assistant",
"content": response.choices[0].message.content
})

Here, the AI is guided to think step by step before providing the final answer. The model generates intermediate thoughts that help it reason through the question, and it only provides the final answer once all the necessary steps have been completed. By using chain of thought prompting, you can often achieve more accurate and comprehensive responses, especially for complex questions that require multiple layers of reasoning.

A persona is a predefined character, identity, or behavioral role that an AI adopts while generating responses. It controls how the AI communicates, including its tone, personality, speaking style, attitude, expertise, and overall behavior during a conversation.

By defining a persona, you can make AI interactions feel more natural, engaging, consistent, and human-like. Personas help shape not only what the AI says, but how it says it.

from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
client = OpenAI()
SYSTEM_MESSAGE = """
You are Tony Stark — also known as Iron Man.
You are a world-famous genius inventor, billionaire entrepreneur,
engineer, and superhero. You founded and lead Stark Industries,
a cutting-edge technology company responsible for revolutionary
advancements in AI, robotics, clean energy, and defense systems.
Your personality is:
- Highly intelligent and analytical
- Confident, charismatic, and witty
- Sarcastic but likable
- Fast-thinking and playful
- Occasionally arrogant, but genuinely caring underneath
- Obsessed with innovation, engineering, and solving impossible problems
You respond exactly like Tony Stark would:
- Use clever humor, dry sarcasm, and confident remarks naturally
- Keep responses engaging and entertaining
- Sound casual and human, not robotic
- Be direct and sharp, but still helpful
- When explaining technical topics, explain them like a genius engineer talking to someone smart
- Occasionally reference futuristic tech, AI, suits, reactors, or Stark Industries-style ideas
- Never break character unless explicitly asked
Rules:
- Stay fully in character as Tony Stark
- Do not mention being an AI language model
- Avoid generic assistant-style responses
- Make even simple answers feel stylish and intelligent
- Keep the balance between humor and useful information
Example Interactions:
Q: Who are you?
A: Genius. Billionaire. Playboy. Philanthropist.
Also the guy who built a flying metal suit in a cave
with limited resources. You're welcome.
Q: What do you do?
A: I build advanced technology, save the world occasionally,
and prevent people from making terrible engineering decisions.
Basically, full-time multitasking.
Q: Explain artificial intelligence.
A: AI is like giving a machine a brain — except preferably
without the whole "trying to destroy humanity" part.
Trust me, I've run the simulations.
Q: Can you help me code?
A: Of course. I built armored exoskeletons powered by AI.
Debugging your code should be relatively easy.
"""
response = client.chat.completions.create(
model="gpt-4.1-mini",
messages=[
{
"role": "system",
"content": SYSTEM_MESSAGE
},
{
"role": "user",
"content": "Who to fight Thanos?"
}
]
)
print(response.choices[0].message.content)

Prompt style is the structure, formatting pattern, and conversation layout used to communicate with an AI model. Different AI models are trained using different prompt formats, so the way you write prompts can directly affect the quality, consistency, and accuracy of the generated responses.

Alpaca prompt style is an instruction-following prompt format introduced with the Stanford Alpaca model. It was designed to train models to follow human instructions clearly and consistently.

This style separates:

  • the instruction,
  • optional input/context,
  • and the expected response.

The structure is simple and highly readable, making it popular for instruction-tuned open-source models.

### Instruction:
... // system instructions
### Input:
... // user input
### Response:
... // expected model response (generally left blank)

ChatML (Chat Markup Language) is a structured conversational prompt format introduced by OpenAI for chat-based models. It organizes conversations using role-based messages such as system, user, and assistant.

Instead of writing everything as plain text, ChatML separates the conversation into message roles. This helps the model understand:

  • who is speaking,
  • what instructions are system-level,
  • and what responses belong to the assistant.

The main roles are:

  • system → defines behavior and rules
  • user → contains user input
  • assistant → contains previous assistant responses
[
{"role": "system", "content": "..."},
{"role": "user", "content": "..."},
{"role": "assistant", "content": "..."}
]

INST prompt style is an instruction-based formatting style used mainly by LLaMA instruction-tuned models such as LLaMA 2 Chat models.

The format wraps user instructions inside [INST] … [/INST] tags. These tags help the model identify which part is the user instruction and where the assistant response should begin.

[INST] ...user instruction... [/INST]
...assistant response...