Back to Learning Hub
Day 3: Master Class

Prompt Engineering Deep Dive

A comprehensive technical reference with real-world examples, code, and advanced patterns.

1. Model Configuration

Critical settings to control output behavior.

Randomness

Temperature

0.1 (Precise): Math, Logic, Code.
0.7+ (Creative): Writing, Ideation.

Diversity

Top P (Nucleus)

Restricts token pool to top % probability mass. Lower = more focused.

Length

Max Tokens

Hard limit on output size (e.g., 4096 or 8192).

Safety

Harm Block

BLOCK_NONE to BLOCK_HIGH. Controls content filtering stringency.

2. The Chain of Thought (CoT) Family

The foundation of reasoning. Forcing the model to "show its work".

A. Standard Chain of Thought

Math & Logic

By simply asking the model to explain step-by-step, accuracy on math and logic problems increases dramatically.

User: If a train travels 120 km in 2 hours, what is its average speed?
AI Response:1. Understand concept: Speed = Distance / Time
2. Identify values: Dist = 120km, Time = 2hr
3. Calculate: 120 / 2 = 60
4. Answer: 60 km/h
cot_prompt = ChatPromptTemplate.from_messages([
  ("system", "Solve problems step-by-step."),
  ("human", "Calculate speed: {input}"),
])
# Result: AI outputs the numbered steps above

B. Zero-Shot CoT

Triggering reasoning without examples using magic phrases.

"Let's think step by step."

Use Case: When you don't have training data but need better logic.

prompt = "Q: {input}\nA: Let's think step by step."

C. Few-Shot CoT

Providing 1-3 full examples to guide the pattern.

Q: 17 * 20?
A: 17*2=34, add 0 → 340.
Q: {input}
A: Let's break it down...
prompt = """Example 1: ...
Example 2: ...
Q: {input}"""

3. Advanced Architectures

Complex structures for complex problems.

🤔 ReAct (Reason + Act)

Agentic Loop

The gold standard for Agents. It combines internal reasoning (Thought) with external tool use (Action).

Execution Trace
Thought: I need to find the area of a circle. I need the radius.
Action: Search tool ["Radius of circle A"]
Observation: Radius is 5cm.
Thought: Formula is pi*r^2. 3.14 * 5^2.
Action: Calculator ["3.14 * 25"]
Observation: 78.5
Final Answer: 78.5 cm^2

Key Components

  • Thought: Model "talks to itself" to plan.
  • Action: Emits a standardized command to a tool.
  • Observation: The output from the code/API tool.
react_template = """
Thought: {agent_scratchpad}
Action: ...
Observation: ...
"""

🌳 Tree of Thoughts (ToT)

Problem solving by searching. Generate multiple branches, evaluate, and backtrack.

Task: Sustainable Transit
Branch 1: Underground Metro... (Costly)
Branch 2: Bike Lanes... (Weather?)
Branch 3: Mixed Bus/Light Rail... (Balanced)
Selected: Branch 3
prompt = "Generate 3 approaches. 
Evaluate each. Pick best."

🔄 Self-Consistency

"Majority Voting". Ask the same question 3-5 times (high temp), pick answer that appears most.

Run 1: ... Answer: 64cm³
Run 2: ... Answer: 64cm³
Run 3: ... Answer: 60cm³ (Wrong)
Result: 64cm³ (2/3 votes)
results = [chain.invoke(x) for x in range(3)]
final = max(set(results), key=results.count)

🏗️ Least-to-Most Prompting

Complex Tasks

Decompose a huge problem into sub-problems. Solve the easy ones first to build context for the hard ones.

Example: Predicting Stock Price1. Define Target (AAPL, Tomorrow)
2. Get Data (Yahoo Finance)
3. Clean Data (Outliers)
4. Feature Eng (RSI, MACD)
5. Train Model (LSTM)
prompt_1 = "Break this into steps."
prompt_2 = "Solve step 1."
prompt_3 = "Given step 1 answer, solve step 2..."

Specialized Techniques Reference

HyDE
Gen fake document -> Embed -> Retrieve real docs.
Prompt Chaining
Output of A becomes Input of B.
Graph Prompting
Relationships as nodes/edges.
Recursive
Feed output back into input to refine.
Gen. Knowledge
Ask model to recall facts before answering.
ART
Auto Reasoning & Tool-use.
APE
Auto Prompt Engineer (AI optimization).
Reflexion
Critique & Correct own output.

4. Ethics & Optimization

Building responsible and effective AI.

Optimization Strategies

  • Iterative Refinement: The "loop" of improvement.
  • A/B Testing: Compare Version A vs Version B systematically.
  • Prompt Libraries: Version control your best prompts.
  • Metrics: Relevance, Accuracy, Coherence.

API Safety Settings

safety_settings={
  HarmCategory.HATE_SPEECH: BLOCK_NONE,
  HarmCategory.DANGEROUS: BLOCK_MEDIUM,
}

Control filters for Hate, Dangerous, Sexual, and Harassment content.

Chat with us on WhatsApp