pythonbeginner
Prompt Template Engineering Patterns
Design reusable, parameterized prompt templates for consistent LLM outputs.
pythonPress ⌘/Ctrl + Shift + C to copy
from string import Template
from typing import Dict, Any
# Simple template with few-shot examples
CLASSIFICATION_PROMPT = Template("""
Classify the following text into one of these categories: $categories
Examples:
$examples
Text to classify: "$text"
Respond with ONLY the category name.
""")
def build_classification_prompt(
text: str,
categories: list[str],
examples: list[dict]
) -> str:
example_str = "\n".join(
f' "{ex["text"]}" -> {ex["category"]}'
for ex in examples
)
return CLASSIFICATION_PROMPT.substitute(
categories=", ".join(categories),
examples=example_str,
text=text
)
# Chain-of-thought template
COT_PROMPT = """Solve this step by step.
Problem: {problem}
Think through each step:
1. Identify the key information
2. Determine the approach
3. Execute the solution
4. Verify the answer
Solution:"""
prompt = build_classification_prompt(
text="The stock market crashed today",
categories=["finance", "sports", "tech", "politics"],
examples=[
{"text": "Apple released new iPhone", "category": "tech"},
{"text": "Lakers won the championship", "category": "sports"}
]
)
print(prompt)Use Cases
- Consistent LLM outputs
- Reusable prompts
- Classification tasks
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptbeginner
Claude Messages API (Anthropic SDK)
Send messages to Claude using the official Anthropic SDK with system prompt and user turn.
Best for: AI assistant
#anthropic#claude
typescriptbeginner
Few-Shot Prompt Template
Build structured few-shot prompts with examples, system instructions, and output format constraints.
Best for: Consistent AI outputs
#prompts#few-shot
typescriptadvanced
AI Prompt Chaining Pattern
Chain multiple LLM calls sequentially where each step's output feeds into the next for complex tasks.
Best for: Complex multi-step AI workflows
#prompt-engineering#chaining
typescriptadvanced
RAG Pipeline Implementation
Build a retrieval-augmented generation pipeline that grounds LLM answers in your own documents.
Best for: Grounding LLM answers in private documents
#ai#rag