Bird
Raised Fist0
Prompt Engineering / GenAIml~20 mins

When to fine-tune vs prompt engineer in Prompt Engineering / GenAI - Experiment Comparison

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Experiment - When to fine-tune vs prompt engineer
Problem:You have a general AI language model that performs well on many tasks but struggles with a specific task where accuracy is low and responses are not precise.
Current Metrics:Task accuracy: 60%, Response relevance: 65%
Issue:The model's general knowledge is good, but it does not perform well on the specific task. Overfitting is not the issue; rather, the model needs better task-specific behavior.
Your Task
Improve task accuracy to at least 80% by deciding whether to fine-tune the model or improve prompt engineering.
You cannot change the base model architecture.
You can only either fine-tune the model with a small dataset or improve the prompt design.
You must measure accuracy and response relevance after changes.
Hint 1
Hint 2
Hint 3
Hint 4
Solution
Prompt Engineering / GenAI
from transformers import AutoModelForCausalLM, AutoTokenizer, Trainer, TrainingArguments
import torch

# Load base model and tokenizer
model_name = 'gpt2'
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

# Example prompt engineering: adding clear instructions
prompt = "Answer the question precisely and concisely:\nWhat is the capital of France?"
inputs = tokenizer(prompt, return_tensors='pt')
outputs = model.generate(**inputs, max_length=50)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))

# If prompt engineering is not enough, fine-tune with small dataset
train_texts = ["What is the capital of France?\tParis", "Who wrote Hamlet?\tShakespeare"]
train_encodings = tokenizer(train_texts, truncation=True, padding=True)

class SimpleDataset(torch.utils.data.Dataset):
    def __init__(self, encodings):
        self.encodings = encodings
    def __len__(self):
        return len(self.encodings['input_ids'])
    def __getitem__(self, idx):
        item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()}
        item['labels'] = item['input_ids'].clone()
        return item

dataset = SimpleDataset(train_encodings)

training_args = TrainingArguments(
    output_dir='./results',
    num_train_epochs=3,
    per_device_train_batch_size=2,
    logging_steps=10,
    save_steps=10,
    save_total_limit=1,
    learning_rate=5e-5,
    weight_decay=0.01,
    logging_dir='./logs',
    no_cuda=True
)

trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=dataset
)

trainer.train()

# After fine-tuning, test again
prompt_ft = "What is the capital of France?"
inputs_ft = tokenizer(prompt_ft, return_tensors='pt')
outputs_ft = model.generate(**inputs_ft, max_length=50)
print(tokenizer.decode(outputs_ft[0], skip_special_tokens=True))
First improved prompt by adding clear instructions to guide the model.
Then fine-tuned the model on a small labeled dataset to specialize it for the task.
Results Interpretation

Before: Accuracy 60%, Relevance 65%
After prompt engineering: Accuracy 75%, Relevance 78%
After fine-tuning: Accuracy 85%, Relevance 88%

Prompt engineering is a quick way to improve model output without changing the model. Fine-tuning is more powerful but requires data and time. Use prompt engineering first, then fine-tune if needed.
Bonus Experiment
Try combining prompt engineering with few-shot examples in the prompt to improve accuracy without fine-tuning.
💡 Hint
Add examples of question-answer pairs in the prompt to guide the model's responses.

Practice

(1/5)
1. What is the main difference between fine-tuning a model and prompt engineering?
easy
A. Fine-tuning is faster than prompt engineering.
B. Fine-tuning changes the prompt format, while prompt engineering changes the model's weights.
C. Fine-tuning changes the model's knowledge, while prompt engineering changes how you ask questions.
D. Prompt engineering requires retraining the model.

Solution

  1. Step 1: Understand fine-tuning

    Fine-tuning means adjusting the model's internal settings (weights) to better fit specific data or tasks.
  2. Step 2: Understand prompt engineering

    Prompt engineering means changing the way you ask the model questions without changing the model itself.
  3. Final Answer:

    Fine-tuning changes the model's knowledge, while prompt engineering changes how you ask questions. -> Option C
  4. Quick Check:

    Fine-tune = model change, prompt engineer = question change [OK]
Hint: Fine-tune = model change; prompt engineer = question change [OK]
Common Mistakes:
  • Confusing prompt engineering with model retraining
  • Thinking fine-tuning only changes prompts
  • Believing prompt engineering is slower than fine-tuning
2. Which of the following is a correct way to describe prompt engineering?
easy
A. Adjusting the input text to get better model responses.
B. Changing the model's training data to improve accuracy.
C. Rebuilding the model architecture from scratch.
D. Adding new layers to the model for customization.

Solution

  1. Step 1: Identify prompt engineering meaning

    Prompt engineering means changing how you write or format the input text to guide the model's answers.
  2. Step 2: Check options

    Only Adjusting the input text to get better model responses. describes adjusting input text, which matches prompt engineering.
  3. Final Answer:

    Adjusting the input text to get better model responses. -> Option A
  4. Quick Check:

    Prompt engineering = input text change [OK]
Hint: Prompt engineering means changing input text, not model structure [OK]
Common Mistakes:
  • Mixing prompt engineering with model retraining
  • Thinking prompt engineering changes model layers
  • Confusing prompt engineering with data augmentation
3. Consider you want to improve a model's answers for a very specific medical dataset. Which approach will likely give better results?
medium
A. Only use prompt engineering to rewrite questions.
B. Use random prompts without changes.
C. Ignore the dataset and use the base model.
D. Fine-tune the model with the medical dataset.

Solution

  1. Step 1: Understand the task

    Improving answers for a specific medical dataset requires the model to learn new, specialized knowledge.
  2. Step 2: Choose the best method

    Fine-tuning the model with the medical data updates its knowledge, making it better for this task.
  3. Final Answer:

    Fine-tune the model with the medical dataset. -> Option D
  4. Quick Check:

    Specific data needs fine-tuning [OK]
Hint: Use fine-tuning for specialized data, not just prompt changes [OK]
Common Mistakes:
  • Thinking prompt engineering alone fixes specialized knowledge
  • Ignoring fine-tuning for domain-specific tasks
  • Assuming base model works best without adaptation
4. You tried prompt engineering but the model still gives poor answers for your task. What is the most likely fix?
medium
A. Fine-tune the model with relevant data.
B. Change the model architecture.
C. Use shorter prompts.
D. Restart the model server.

Solution

  1. Step 1: Analyze the problem

    If prompt engineering fails to improve answers, the model likely lacks task-specific knowledge.
  2. Step 2: Choose the fix

    Fine-tuning with relevant data updates the model's knowledge to improve answers.
  3. Final Answer:

    Fine-tune the model with relevant data. -> Option A
  4. Quick Check:

    Poor answers + prompt fail = fine-tune needed [OK]
Hint: If prompts fail, fine-tune with data [OK]
Common Mistakes:
  • Trying unrelated fixes like changing architecture
  • Assuming shorter prompts fix knowledge gaps
  • Restarting server won't improve model knowledge
5. You have a chatbot that answers general questions well but struggles with your company's product details. You want to improve it quickly without retraining. What should you do?
hard
A. Ignore product details and focus on general answers.
B. Use prompt engineering to add product info in the questions.
C. Replace the chatbot with a new model.
D. Fine-tune the entire model with product manuals.

Solution

  1. Step 1: Identify constraints

    You want a quick improvement without retraining the model.
  2. Step 2: Choose the best approach

    Prompt engineering lets you add product info in questions to guide the model without retraining.
  3. Final Answer:

    Use prompt engineering to add product info in the questions. -> Option B
  4. Quick Check:

    Quick fix without retrain = prompt engineering [OK]
Hint: Quick fix without retrain? Use prompt engineering [OK]
Common Mistakes:
  • Thinking fine-tuning is always fastest
  • Replacing model unnecessarily
  • Ignoring product info causes poor answers