Concept #107Mediumextended-ai-concepts

What are the different types of prompt engineering?

#gen-ai#prompt-engineering

Answer

Types of Prompt Engineering

Prompt engineering encompasses many techniques for eliciting better responses from LLMs. Here's a comprehensive taxonomy.

1. Zero-Shot Prompting

No examples — just a direct instruction:

python
prompt = "Classify the sentiment of this review: 'The product broke after one day.'"
# → "Negative"

2. Few-Shot Prompting

Provide examples to guide the model:

python
prompt = '''Classify sentiment:

Review: "Amazing product, love it!" → Positive
Review: "Terrible, broke on day one." → Negative
Review: "It's okay, nothing special." → Neutral

Review: "Best purchase I've made this year!" → '''
# → "Positive"

3. Chain-of-Thought (CoT)

Ask the model to reason step by step:

python
prompt = '''
Q: A farmer has 15 sheep. All but 8 die. How many are left?
A: Let me think step by step.
- "All but 8" means 8 survive
- 8 sheep are left
Answer: 8

Q: A jar has 12 cookies. You eat 3 and give 4 to a friend. How many remain?
A: '''

4. Zero-Shot CoT

Simply add "Let's think step by step":

python
prompt = "What is 25% of 480? Let's think step by step."
# Model reasons: 25% = 1/4, 480/4 = 120

5. Self-Consistency

Generate multiple reasoning paths, take the majority answer:

python
answers = []
for _ in range(5):
    answer = llm.invoke("Solve: " + problem, temperature=0.7)
    answers.append(extract_answer(answer))
final_answer = max(set(answers), key=answers.count)  # Majority vote

6. Tree of Thoughts (ToT)

Explore multiple reasoning branches, prune dead ends:

text
Problem
  ├── Approach A → evaluate → continue if promising
  ├── Approach B → evaluate → abandon if poor
  └── Approach C → evaluate → continue

7. Role Prompting

Assign a persona to shift behavior:

python
system = "You are a Socratic tutor. Never give direct answers. Guide students with questions."
user = "What is machine learning?"
# → "What do you think makes a system 'learn'? What's different about..."

8. ReAct (Reason + Act)

Interleave reasoning with action:

text
Thought: I need to find the current stock price
Action: search_web("AAPL stock price today")
Observation: AAPL is trading at $189.42
Thought: Now I can answer
Answer: Apple stock is currently $189.42

9. Prompt Chaining

Break complex tasks into sequential prompts:

python
step1 = llm.invoke(f"Extract the key facts from: {document}")
step2 = llm.invoke(f"Identify contradictions in: {step1}")
step3 = llm.invoke(f"Write a summary highlighting: {step2}")

10. Maieutic Prompting

Ask the model to explain its reasoning, then verify consistency:

python
answer = llm.invoke(question)
explanation = llm.invoke(f"Explain why: {answer}")
verification = llm.invoke(f"Is this explanation consistent with: {answer}? {explanation}")

Techniques Comparison

TechniqueBest ForComplexity
Zero-shotSimple tasks, good modelsLow
Few-shotConsistent format neededLow
CoTMath, logic, multi-stepMedium
Self-consistencyHigh-stakes decisionsHigh
ToTComplex reasoningHigh
Role promptingBehavior shapingLow
ReActAgent tasks with toolsMedium
Prompt chainingComplex multi-part tasksMedium