AI Engineer Portal
Your personal operating system for career transition.
Private mode
Exercise
Build a structured prompt template with system/user role separation
Build a structured prompt template with system/user role separation
Prompt injection attacks happen when user-controlled content reaches the system role. The first defense is strict structural separation.
What you are building
Implement a PromptBuilder class that:
- Accepts a
system_template(a string with{variable}placeholders for trusted application data) - Accepts
template_vars(a dict of trusted values to fill in) - Accepts
user_input(raw user text, always kept in the user role — never interpolated into system) - Returns a messages list
[{"role": "system", "content": ...}, {"role": "user", "content": ...}] - Raises
ValueErrorifuser_inputtext appears inside the rendered system content
Requirements
- Template variable substitution should use Python's
str.format_map - If a required template variable is missing, raise
KeyErrorwith a clear message - The user input must never appear in the system message
- Strip leading/trailing whitespace from both rendered messages
Example usage
builder = PromptBuilder(
system_template="You are a support agent for {company}. Tone: {tone}.",
template_vars={"company": "Acme Corp", "tone": "professional"},
)
messages = builder.build(user_input="What is your refund policy?")
# messages == [
# {"role": "system", "content": "You are a support agent for Acme Corp. Tone: professional."},
# {"role": "user", "content": "What is your refund policy?"},
# ]
Prompt Formatting / easy / Step 2 of 5
Practice stage
Prompt boundary discipline
Hint
Treat prompt construction like request composition: trusted instructions, untrusted user input, and context blocks should stay separate.
Success criteria
- - Separates system, user, and context cleanly
- - Avoids string chaos and hidden assumptions
- - Would scale to a real LLM feature
Review checklist
- - Did I preserve trusted versus untrusted boundaries?
- - Would this format survive longer prompts and more context?
- - Can another engineer review the structure quickly?
Practice
Generate a variation
Generate a new exercise variation to deepen understanding or practice a related concept.
Attempt history
Recent submissions
Before you submit, decide what a strong answer should make obvious to the reviewer.
No attempts yet.