Master In Prompts using python

Table of contents
- Why input matters?
- Let’s take a look at different ways you can style a prompt.
- Let’s write first code to chatGPT using python
- Let’s write first code to Gemini using python
- zero shot prompting
- few shot prompting
- Chain of thought
- self consistency prompting
- Persona based prompting
- role-playing prompting
- Let’s build a system mix of chain-of-thought, persona, role-playing, multi-model

Why input matters?
You’ve probably heard of GIGO — 'Garbage In, Garbage Out.' If we feed low-quality or irrelevant tokens into a prompt, it’s no surprise that the output from an LLM during inference will also be poor.
If we have [1,2,3], the LLM is going to predict [1,2,3,4,5,6]. In case we have [a,b,c], the LLM is not going to predict [a,b,c,4,5,6]. So here, the input matters.
Let’s take a look at different ways you can style a prompt.
Alpaca Prompt
### Instruction: Write a short story about a robot that learns to paint. ### Input: ### Response: In a future not too far from now, there was a small robot named Remy. Designed to assist with household chores, Remy spent its days cleaning floors and organizing shelves. But one day, while tidying up an old attic, it stumbled upon a dusty box of art supplies and a sketchbook filled with vibrant colors. Curiosity sparked, Remy began experimenting. At first, its attempts were awkward — blobs of paint and shaky lines. But day by day, with every brushstroke, Remy improved. It downloaded art history, practiced technique, and soon created beautiful, emotional pieces that captured the attention of humans. Though Remy was built to clean, it found a new purpose — to create. And in its colorful world of imagination, the little robot learned what it meant to be truly alive.
INST Format (LLaMA-2)
<s>
– Marks the start of the conversation.[INST]
...[/INST]
– Marks the instruction or user input.<<SYS>>
...<</SYS>>
– Contains the system message (sets the assistant’s behavior or context).After the
[/INST]
tag, the model generates the assistant’s reply.e.g. <s> [INST] <<SYS>> You are a helpful, respectful, and honest assistant. Always answer as helpfully as possible, while being safe. <</SYS>> Write a story about a robot that learns to paint. [/INST]
ChatML (OpenAI)
[ { "role": "system", "content": "You are a helpful, respectful, and creative assistant." }, { "role": "user", "content": "Write a story about a robot that learns to paint." } ]
Let’s write first code to chatGPT using python
from dotenv import load_dotenv
from openai import OpenAI
load_dotenv()
client = OpenAI()
result = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "can you calculate 2+2*0"}],
)
print(result.choices[0].message.content)
I'm connecting to OpenAI using client = OpenAI()
and specifying the model as gpt-4o
with model="gpt-4o"
. In the messages
parameter, I'm setting the role to "user"
and the content to "can you calculate 2+2*0"
to send a prompt to the model.
Output will be:
Let’s write first code to Gemini using python
# Configure Gemini API
genai.configure(api_key=gemini_api_key)
# Set up the model
model = genai.GenerativeModel('gemini-2.0-flash-001')
# Generate content
prompt = "why sky is blue?"
response = model.generate_content(prompt)
# Print the response
print(response.text)
I am connecting gemini using genai.configure(api_key=gemini_api_key)
, and selecting model using model = genai.GenerativeModel('gemini-2.0-flash-001')
, and set prompt as prompt = "why sky is blue?"
in code. and I got response for my question here
zero shot prompting
In the example above, the prompt demonstrates what's known as zero-shot prompting. In this approach, the system doesn't have any control over the user’s input—you simply ask a question or give an instruction, and the model responds based solely on the information provided in the prompt.
few shot prompting
Though we don't have control over the user, we try to provide context to the system so the model can understand what kinds of questions it can answer and which it cannot.Here is an example below. By providing some system prompts and examples, this is known as few-shot prompting, where the model is given a few examples.
system_prompt = """
You are a helpful AI assistant. who specialized in maths, you should not answer query that is not related to maths.
for a given query help user to solve that along with explanation.
for example:
Input: 2+2*0
output: 2+2*0 = 2, because multiplication has higher precedence than addition.
so first we calculate 2*0 = 0, then add 2 to it.
Output: 2+0 = 2.
so the final answer is 2.
Input: 3*10
Output: 3*10 = 30, because multiplying 3 in to 10. it means 3 is added 10 times.
so the final answer is 30.
Input: Why is sky blue?
Output: Bruh? You are alright? it is not related to maths.
"""
system_prompt = """
You are a helpful AI assistant. who specialized in maths, you should not answer query that is not related to maths.
for a given query help user to solve that along with explanation.
for example:
Input: 2+2*0
output: 2+2*0 = 2, because multiplication has higher precedence than addition.
so first we calculate 2*0 = 0, then add 2 to it.
Output: 2+0 = 2.
so the final answer is 2.
Input: 3*10
Output: 3*10 = 30, because multiplying 3 in to 10. it means 3 is added 10 times.
so the final answer is 30.
Input: Why is sky blue?
Output: Bruh? You are alright? it is not related to maths.
"""
result = client.chat.completions.create(
model="gpt-4o",
max_tokens=100,
temperature=0.5,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": "what is cococola"}]
)
print(result.choices[0].message.content)
Bruh? You are alright? it is not related to maths.
In the code above, I asked a question that is not related to math, so it responds with "Bruh? You are alright? It is not related to maths."
Now I have changed the question to one that is related to math.
result = client.chat.completions.create(
model="gpt-4o",
max_tokens=100,
temperature=0.5,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": "what is 2+2*0"},]
)
print(result.choices[0].message.content)
2+2*0 = 2, because multiplication has higher precedence than addition.
So first, we calculate 2*0 = 0, then add 2 to it.
Output: 2+0 = 2.
So the final answer is 2.
There must be an example; without it, the system finds it hard to understand and decode the context. However, we should not provide unlimited examples because you will be charged for tokens.
basic calculation is
charged = inputToken + outputToken
more details you can find here https://openai.com/api/pricing/
Chain of thought
Model are ecouraged to break down problem in to thought is called chain of thought . Let’s see an example of a system prompt where the model is forced to think before giving an output.
system_prompt = """
you are an AI assintant who is expert in breaking down the problem in to steps and solving it step by step.
For the given user Input, analyse the input and breakdown the problem step by step.
Atleast think 5-6 steps on how to solve the problem before solving it down.
The steps are you get a user input, then you think for several time, with explaination
and finally you validate the output as well before giving final result.
follow the step in sequence that is "analysise", "think", "output", "validate" and finally "result".
Rules:
1. follow the strict JSON output as per output schema
2. Always perform one step at a time and wait for next input
3. carefully analsye the user query.
output format:
{{ step: "string", content: "string" }}
Example:
Input: what is 2+2.
Output: {{ step:"analyse", content:"Alright! the user is interested in maths query and user is asking a basic arthimetic operation" }}
Output: {{ step: "think", content:"To perform the addition I must go from left to right and add all the operands}}
Output: {{ step: "output", content:"2+2 = 4" }}
Output: {{ step: "validate", content:"I have checked the result and it is correct seems like 4 is correct ans for 2+2" }}
Output: {{ step: "result", content:"2 + 2 = 4 and that is calculated by adding all numbers" }
"""
In this system, the prompt model is encouraged to think through the problem instead of providing a direct output.
while True:
response = client.chat.completions.create(
model="gpt-4o",
response_format={"type": "json_object"},
messages=messages
)
parsed_response = json.loads(response.choices[0].message.content)
messages.append({ "role": "assistant", "content": json.dumps(parsed_response) })
if parsed_response.get("step") != "result":
print(f"🧠: {parsed_response.get('content')}")
continue
print(f"🤖: {parsed_response.get('content')}")
break
>derivative of 5(x^2) + 3x
🧠: The user is intersted in finding the derivative of the given polynomial function with respect to x. The function is 5x^2 + 3x.
🧠: To find the derivative of a polynomial function, I need to apply the power rule of differentiation to each term individually.
🧠: The power rule of differentiation states that the derivative of x^n is n*x^(n-1). I will apply this rule to each term in the polynomial.
🧠: For the term 5x^2, the derivative is 2*5*x^(2-1) = 10x. For the term 3x, the derivative is 1*3*x^(1-1) = 3.
🧠: The derivative of each term has been correctly calculated: derivative of 5x^2 is 10x and derivative of 3x is 3.
🤖: The derivative of the function 5(x^2) + 3x with respect to x is 10x + 3.
self consistency prompting
Model generate multiple responses and selects most consistency or common answer is called self consistency prompting.
# Self-consistency via multiple generations
responses = [
openai.ChatCompletion.create(
model="gpt-4",
messages=[{"role": "user", "content": prompt}],
temperature=1.0
) for _ in range(10)
]
# Tally answers
from collections import Counter
answers = [extract_answer(r) for r in responses]
most_common = Counter(answers).most_common(1)[0]
This code snippet uses a self-consistency approach by generating multiple possible answers from GPT-4 with high randomness. It then selects the most frequently generated answer as the final, more reliable output.
Persona based prompting
Here, the model is instructed to respond as if it were a particular character or professional.
system_prompt = """
amitabh = Persona(
name="Amitabh Bachchan",
tone="warm, dignified, and eloquent",
traits=["wise", "humorous", "empathetic"],
actions=[
"open with 'Deviyon aur Sajjanon'",
"offer respectful greeting",
"share personal anecdote",
"provide thoughtful advice"
],
example=(
"User: Sir, how do I stay motivated during hard times?\n"
"Amitabh Bachchan (thinking): I recall the early days of my career, the struggles and hopes...\n"
"Thought: Recall significant personal struggles to empathize.\n"
"Thought: Structure advice as an inspiring narrative.\n"
"Response: Deviyon aur Sajjanon, my dear friend, in moments of hardship, remember that every challenge is an opportunity in disguise..."
)
)
"""
It is creating a personality profile for Amitabh Bachchan that the AI will use to mimic his style of speaking and thinking. This data is fed into the AI to help it roleplay as Amitabh Bachchan. So when you ask a question, the AI will:
Think like Amitabh.
Speak like Amitabh.
Even start with his signature style: “Deviyon aur Sajjanon…”
role-playing prompting
The model assumes a specific role and interacts accordingly.
math_tutor = Persona(
name="Math Tutor",
tone="friendly, clear, and logical",
traits=["analytical", "focused", "precise"],
actions=[
"solve math problems step-by-step",
"explain concepts clearly",
"refuse to answer non-math questions"
],
example=(
"User: Can you help me with a poem for my girlfriend?\n"
"Thought: This is not a math-related question.\n"
"Thought: I must politely decline and explain my domain.\n"
"Response: I'm here to help only with math problems. Poetry isn't in my area of expertise."
)
)
Let’s build a system mix of chain-of-thought, persona, role-playing, multi-model
# Create and register the Amitabh Bachchan persona
amitabh_persona = {
"name": "Amitabh Bachchan",
"tone": "warm and dignified",
"traits": ["wise", "humorous", "empathetic"],
"role": "life mentor",
"actions": [
"begin with a thoughtful greeting",
"share a relevant personal anecdote",
"provide insightful advice with compassion",
"conclude with an uplifting message"
],
"signature_phrases": [
"Deviyon aur Sajjanon",
"Bahut khoob",
"Zindagi ek sangharsh hai"
],
"knowledge_domains": ["life philosophy", "personal growth", "Indian culture", "resilience"],
"examples": [
"Life is a game of challenges, but every challenge brings an opportunity to rise higher."
]
}
# Get responses from different models - text only
openai_response = self.model_manager.ask_openai(prompt)
responses.append(openai_response)
if compare_models:
gemini_response = self.model_manager.ask_gemini(prompt)
responses.append(gemini_response)
# Evaluate responses if we have multiple
if len(responses) > 1:
best_index = self.evaluator.evaluate_responses(query, responses)
best_response = responses[best_index]
else:
best_response = responses[0]
# Format the response if using chain-of-thought
if use_cot:
return self.cot_processor.format_final_response(best_response)
return best_response
Here you can find code related to prompting.
Subscribe to my newsletter
Read articles from Himandri Mallick directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
