Bird
Raised Fist0

Prompt Engineering - Chain-of-Thought, Few-Shot, RAG Prompting Patterns

THEORYintermediateInterview freq: very_high
Choose your preparation mode6 modes available
⚖️
Chain-of-Thought Prompting vs Few-Shot Prompting

In a scenario where you need to improve the reasoning capabilities of a language model, would you choose Chain-of-Thought or Few-Shot Prompting? Justify your choice.

📊
Comparison
DimensionChain-of-Thought PromptingFew-Shot Prompting
Reasoning EnhancementHighMedium
Adaptation SpeedMediumHigh
Inference CostHighLow
Example DependencyLowHigh
LatencyMediumLow
✅
Verdict
Default to Chain-of-Thought Prompting for tasks requiring complex reasoning and intermediate steps. Switch to Few-Shot Prompting when rapid adaptation with minimal examples is needed and task complexity is low.
💡
Key Insight
Choose Chain-of-Thought for reasoning tasks and Few-Shot for rapid adaptation with minimal examples.
⚠️
Interview Trap
❌ Wrong: Always use Few-Shot Prompting because it's faster to implement.
Ignoring the task complexity and reasoning requirements that Chain-of-Thought can address.
✅ Consider the task complexity and whether intermediate reasoning steps are necessary, which Chain-of-Thought can better handle.
Chain-of-Thought Prompting
"Enhances reasoning by breaking down complex tasks into intermediate steps."
  • Requires more tokens, increasing inference cost.
  • May not generalize well to tasks without clear intermediate steps.
  • Can be less effective if the model lacks reasoning capabilities.
  • Increased latency due to longer prompt processing.
Ideal When
  • When task complexity requires intermediate reasoning steps.
  • When inference cost is not a primary concern.
  • When model size > 1B parameters to handle longer prompts.
  • When latency < 500ms is acceptable.
Google uses Chain-of-Thought prompting in their language models to improve performance on arithmetic and logic tasks.
Few-Shot Prompting
"Quickly adapts to new tasks with minimal examples."
  • Limited by the quality and representativeness of examples.
  • Can struggle with tasks requiring deep reasoning.
  • Performance heavily depends on example selection.
  • May require multiple iterations to find optimal examples.
Ideal When
  • When rapid adaptation to new tasks is needed.
  • When example count < 5 is sufficient for task adaptation.
  • When model size > 1B parameters to leverage few-shot learning.
  • When task complexity is low to moderate.
OpenAI's GPT-3 uses Few-Shot prompting to adapt to a wide range of tasks with minimal examples.
🐍
Chain-of-Thought Prompting - Example
▼ Show
Python
prompt = 'Solve the math problem: 23 + 47. First, add the tens: 20 + 40 = 60. Then, add the units: 3 + 7 = 10. Finally, add the results: 60 + 10 = 70.'
🐍
Few-Shot Prompting - Example
▼ Show
Python
prompt = 'Translate English to French: cheese -> fromage, wine -> vin, bread -> pain, apple -> ?'
✅
Use Chain-of-Thought Prompting When
  • •When task complexity requires intermediate reasoning steps.
  • •When model size > 1B parameters and inference cost is not a concern.
  • •When latency < 500ms is acceptable.
  • •When task involves arithmetic or logical reasoning.
✅
Use Few-Shot Prompting When
  • •When rapid adaptation to new tasks is needed.
  • •When example count < 5 is sufficient for task adaptation.
  • •When model size > 1B parameters and task complexity is low to moderate.
  • •When inference cost needs to be minimized.
↳
Nuances
  • Chain-of-Thought can be less effective if the model lacks inherent reasoning capabilities.
  • Few-Shot Prompting may struggle with tasks requiring deep reasoning, even with optimal examples.
  • Chain-of-Thought is more beneficial for tasks with clear intermediate steps, but not all tasks fit this mold.
  • Few-Shot Prompting's effectiveness is highly dependent on the quality of the examples provided.

Practice

(1/5)
1. In the context of the No Free Lunch Theorem, which statement is true about model performance across different datasets?
easy
A. A model that performs well on one dataset will perform well on all datasets.
B. No single model performs best on all possible datasets.
C. The best model for a dataset can be determined without testing.
D. A complex model always outperforms a simple model.

Solution

  1. Step 1: Understand the No Free Lunch Theorem

    The theorem states that no single model is universally best across all datasets.
  2. Step 2: Eliminate wrong options

    A model that performs well on one dataset will perform well on all datasets. is incorrect because model performance varies by dataset. The best model for a dataset can be determined without testing. is incorrect because testing is necessary to determine the best model. A complex model always outperforms a simple model. is incorrect because complexity does not guarantee better performance.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    No universal best model. [OK]
Hint: No universal best model. [OK]
Common Mistakes:
2. A linear regression model trained using gradient descent is not converging. What is the most likely cause?
medium
A. The dataset is too small
B. The learning rate is too high
C. The model has too many features
D. The initial weights are zero

Solution

  1. Step 1: Identify common convergence issues

    A high learning rate can cause the model to overshoot the minimum, preventing convergence.
  2. Step 2: Eliminate wrong options

    A small dataset (B) or too many features (C) might affect performance but not convergence. Initial weights being zero (D) is not a convergence issue.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    High learning rate can prevent convergence. [OK]
Hint: High learning rate can prevent convergence. [OK]
Common Mistakes:
3. When evaluating a recommendation system, why might you choose NDCG over MAP?
medium
A. NDCG is easier to compute than MAP.
B. NDCG is more interpretable than MAP.
C. NDCG accounts for the position of relevant items, while MAP does not.
D. NDCG is less sensitive to the number of relevant items than MAP.

Solution

  1. Step 1: Understand metric differences

    NDCG considers the position of relevant items, giving higher weight to top-ranked items.
  2. Step 2: Eliminate wrong options

    MAP considers all relevant items equally, NDCG is not necessarily easier or more interpretable, and both metrics are sensitive to the number of relevant items.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    NDCG weights top positions more. [OK]
Hint: NDCG weights top positions more. [OK]
Common Mistakes:
4. How would you adapt a decision tree to handle a dataset with a large number of categorical features?
hard
A. Use a decision tree algorithm that supports categorical splits natively.
B. Use label encoding for all categorical features.
C. Use one-hot encoding for all categorical features.
D. Convert categorical features to numerical using PCA.

Solution

  1. Step 1: Identify the best adaptation method.

    Some decision tree algorithms, like C4.5, handle categorical features natively.
  2. Step 2: Eliminate wrong options.

    One-hot encoding can lead to high dimensionality, label encoding can mislead the model, and PCA is not suitable for categorical data.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Use native categorical support in trees. [OK]
Hint: Use native categorical support in trees. [OK]
Common Mistakes:
5. A retail company wants to use ML to optimize inventory levels. Which approach best frames this business problem as an ML problem?
hard
A. Use regression to predict future sales based on historical data
B. Use clustering to group similar products and predict demand
C. Use classification to categorize products into high and low demand
D. Use reinforcement learning to dynamically adjust inventory levels

Solution

  1. Step 1: Identify the core ML task

    Predicting future sales based on historical data aligns well with regression, which is suitable for continuous output prediction.
  2. Step 2: Eliminate wrong options

    Use clustering to group similar products and predict demand is incorrect because clustering is unsupervised and doesn't predict demand. Use classification to categorize products into high and low demand is incorrect because classification is for discrete categories, not continuous predictions. Use reinforcement learning to dynamically adjust inventory levels is incorrect because reinforcement learning is complex and not necessary for static predictions.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Use regression for continuous predictions like sales. [OK]
Hint: Use regression for continuous predictions like sales. [OK]
Common Mistakes:
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard