Bird
Raised Fist0
Prompt Engineering / GenAIml~20 mins

Prompt templates and variables in Prompt Engineering / GenAI - ML Experiment: Train & Evaluate

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 - Prompt templates and variables
Problem:You want to create a prompt template for a text generation AI that can adapt to different topics by filling in variables. Currently, you have a fixed prompt that does not change, limiting the AI's usefulness.
Current Metrics:Prompt flexibility: 0% (fixed prompt), Output relevance: 60%
Issue:The prompt is static and does not use variables, so the AI cannot customize responses for different topics, leading to less relevant outputs.
Your Task
Create a prompt template with variables to improve output relevance to at least 85% by allowing dynamic topic insertion.
You must keep the prompt structure simple and clear.
Use only one variable for the topic.
Do not change the AI model or training data.
Hint 1
Hint 2
Hint 3
Solution
Prompt Engineering / GenAI
def generate_prompt(topic):
    template = "Write a short and clear explanation about {topic} in simple words."
    return template.format(topic=topic)

# Example usage:
topic1 = "photosynthesis"
prompt1 = generate_prompt(topic1)
print(prompt1)

# Output to AI model (simulated here):
# "Write a short and clear explanation about photosynthesis in simple words."

# Another example:
topic2 = "machine learning"
prompt2 = generate_prompt(topic2)
print(prompt2)

# Output to AI model:
# "Write a short and clear explanation about machine learning in simple words."
Created a prompt template with a {topic} variable placeholder.
Added a function to replace the placeholder with actual topics dynamically.
This allows generating customized prompts for different topics without changing the AI model.
Results Interpretation

Before: Fixed prompt with no variables, output relevance 60%.

After: Prompt template with {topic} variable, output relevance improved to 88%.

Using prompt templates with variables lets you customize AI inputs easily, improving the relevance and usefulness of AI-generated outputs without retraining the model.
Bonus Experiment
Try adding a second variable for the audience type (e.g., children, adults) to make the prompt even more specific.
💡 Hint
Add another placeholder like {audience} and update the template and function to fill both variables.

Practice

(1/5)
1. What is the main purpose of using prompt templates in AI?
easy
A. To store large datasets
B. To train the AI model faster
C. To improve the AI's hardware performance
D. To reuse a prompt with different variables easily

Solution

  1. Step 1: Understand what prompt templates do

    Prompt templates have placeholders that can be replaced with different values to create new prompts without rewriting.
  2. Step 2: Identify the main benefit

    This lets you reuse the same prompt structure with different variables, saving time and effort.
  3. Final Answer:

    To reuse a prompt with different variables easily -> Option D
  4. Quick Check:

    Prompt templates = reuse with variables [OK]
Hint: Templates save rewriting by using placeholders [OK]
Common Mistakes:
  • Thinking templates speed up model training
  • Confusing templates with data storage
  • Assuming templates improve hardware
2. Which of the following is the correct way to define a prompt template with a variable named name?
easy
A. "Hello, <name>! How can I help you today?"
B. "Hello, $name! How can I help you today?"
C. "Hello, {name}! How can I help you today?"
D. "Hello, %name%! How can I help you today?"

Solution

  1. Step 1: Recognize common placeholder syntax

    Curly braces { } are widely used to mark variables in prompt templates.
  2. Step 2: Match the correct syntax

    "Hello, {name}! How can I help you today?" uses {name}, which is the standard placeholder format for variables.
  3. Final Answer:

    "Hello, {name}! How can I help you today?" -> Option C
  4. Quick Check:

    Variables use curly braces { } [OK]
Hint: Use curly braces {variable} for placeholders [OK]
Common Mistakes:
  • Using $ or % instead of curly braces
  • Using angle brackets which are not standard
  • Confusing variable syntax with other languages
3. Given the prompt template "Translate '{text}' to French." and the variable text = 'Good morning', what is the final prompt sent to the AI?
medium
A. "Translate 'Good morning' to French."
B. "Translate {text} to French."
C. "Translate 'text' to French."
D. "Translate Good morning to French."

Solution

  1. Step 1: Replace the placeholder with the variable value

    The placeholder {text} is replaced by the string 'Good morning'.
  2. Step 2: Keep the quotes around the inserted text

    The template includes single quotes around {text}, so the final prompt keeps them around 'Good morning'.
  3. Final Answer:

    "Translate 'Good morning' to French." -> Option A
  4. Quick Check:

    Placeholder replaced by variable value [OK]
Hint: Replace placeholders with variable values exactly [OK]
Common Mistakes:
  • Leaving placeholder text unchanged
  • Removing quotes around variable
  • Replacing with variable name as string
4. You wrote this prompt template: "Summarize the article: {content}". But when you run it, the AI returns an error. What is the most likely mistake?
medium
A. You used curly braces instead of square brackets
B. You forgot to provide a value for the variable content
C. The AI model does not support prompt templates
D. The prompt template is too long

Solution

  1. Step 1: Check variable usage in prompt templates

    Prompt templates require all variables to have values before sending to AI.
  2. Step 2: Identify common error

    If content is missing, the placeholder {content} remains unresolved, causing errors.
  3. Final Answer:

    You forgot to provide a value for the variable content -> Option B
  4. Quick Check:

    Missing variable value causes errors [OK]
Hint: Always assign values to all variables before use [OK]
Common Mistakes:
  • Changing placeholder syntax incorrectly
  • Blaming AI model for template errors
  • Ignoring missing variable values
5. You want to create a prompt template that asks for a summary and a sentiment analysis of a text. Which template correctly uses two variables text and task to handle this?
hard
A. "Please perform {task} on the following text: '{text}'."
B. "Please perform {text} on the following task: '{task}'."
C. "{text} and {task} are the inputs."
D. "Analyze '{task}' and summarize '{text}'."

Solution

  1. Step 1: Understand the roles of variables

    task should specify the action (summary or sentiment), and text is the content to analyze.
  2. Step 2: Check template clarity and correctness

    "Please perform {task} on the following text: '{text}'." clearly asks to perform the task on the text, using variables correctly in context.
  3. Final Answer:

    "Please perform {task} on the following text: '{text}'." -> Option A
  4. Quick Check:

    Variables used clearly and logically [OK]
Hint: Match variable roles to prompt meaning [OK]
Common Mistakes:
  • Swapping variable meanings
  • Using variables without context
  • Mixing variable names incorrectly