-
Notifications
You must be signed in to change notification settings - Fork 8k
Expand file tree
/
Copy pathproduction_patterns.py
More file actions
41 lines (33 loc) · 1.3 KB
/
production_patterns.py
File metadata and controls
41 lines (33 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""
Production Prompt Engineering Patterns
Author: Rehan Malik
Battle-tested prompt patterns with evaluation framework.
"""
from string import Template
PATTERNS = {
"chain_of_thought": {
"template": "Think step-by-step:\n1. Identify key info\n2. Analyze\n3. Conclude\n\nQ: $question",
"best_for": ["reasoning", "math", "logic"],
"improvement": "+15-20% on reasoning tasks",
},
"few_shot_cot": {
"template": "Examples:\n$examples\n\nNow classify: $input",
"best_for": ["classification", "categorization"],
"improvement": "+25% vs zero-shot",
},
"structured_extraction": {
"template": "Extract JSON matching: $schema\nFrom: $text\nJSON:",
"best_for": ["data extraction", "parsing"],
"improvement": "95%+ accuracy with schema",
},
"rag_grounded": {
"template": "Context:\n$context\n\nAnswer ONLY from context: $question",
"best_for": ["QA", "search", "knowledge retrieval"],
"improvement": "Reduces hallucination by 80%+",
},
}
def render(pattern: str, **kwargs) -> str:
return Template(PATTERNS[pattern]["template"]).safe_substitute(**kwargs)
if __name__ == "__main__":
for name, p in PATTERNS.items():
print(f"{name}: {p['improvement']} | Best for: {', '.join(p['best_for'])}")