3.5. Prompt Templates
💡 First Principle: Hardcoding prompts is like hardcoding SQL queries—it works until you need to change something or test variations. Prompt templates extract the structure ("Summarize this document: {document}") from the variable content, letting you version-control the template, swap in different inputs for testing, and reuse the same pattern across applications. When the exam shows prompt code, look for whether variables are injected via templates or concatenated as strings—templates are the production pattern.
Prompt templates are essential for production applications where you need consistent prompt patterns with variable inputs. Azure AI Foundry supports Jinja2-style templates.
đź”§ Implementation Reference: Prompt Templates
| Item | Value |
|---|---|
| Template Syntax | Jinja2 ({{ variable }}) |
| Location | Azure AI Foundry prompt flow |
| File Format | .jinja2 or inline in flow |
Template Pattern:
system:
You are a {{ persona }} assistant. Follow these rules:
{% for rule in rules %}
- {{ rule }}
{% endfor %}
user:
{{ user_query }}
Using Templates in Code:
from jinja2 import Template
template = Template("""
You are a helpful assistant for {{ company }}.
Answer questions about: {{ topics | join(', ') }}
""")
prompt = template.render(
company="Contoso",
topics=["products", "pricing", "support"]
)
⚠️ Exam Trap: Templates use {{ }} for variables and {% %} for logic (loops, conditionals).