-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Expand file tree
/
Copy pathproduction_prompt_patterns.py
More file actions
108 lines (85 loc) · 3.37 KB
/
production_prompt_patterns.py
File metadata and controls
108 lines (85 loc) · 3.37 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
Production Prompt Engineering Patterns
Author: Rehan Malik
Tested prompt patterns from deploying LLM features at enterprise scale.
Each pattern includes the template, use case, and performance notes.
"""
PATTERNS = {
"chain_of_thought": {
"description": "Forces step-by-step reasoning for complex problems",
"template": """Think through this step-by-step:
1. First, identify the key information
2. Then, analyze the relationships
3. Consider edge cases
4. Provide your final answer
Question: {question}
Step-by-step reasoning:""",
"best_for": ["math", "logic", "multi-step analysis"],
"improvement": "+15-20% accuracy on reasoning tasks",
},
"few_shot_with_cot": {
"description": "Combines examples with chain-of-thought reasoning",
"template": """Classify the customer inquiry. Show your reasoning.
Example 1:
Inquiry: "My payment failed but I was still charged"
Reasoning: Customer reports a payment issue with unexpected charge. This is a billing/payment problem.
Category: billing_issue
Example 2:
Inquiry: "How do I export my data?"
Reasoning: Customer asking about platform functionality. This is a feature question.
Category: feature_question
Now classify:
Inquiry: "{inquiry}"
Reasoning:""",
"best_for": ["classification", "categorization"],
"improvement": "+25% vs zero-shot on domain-specific classification",
},
"structured_extraction": {
"description": "Extracts structured data from unstructured text",
"template": """Extract information from the text below into JSON format.
Only include fields that can be determined from the text.
Use null for fields that cannot be determined.
Required fields:
{schema}
Text:
{text}
JSON output:""",
"best_for": ["data extraction", "parsing", "form filling"],
"improvement": "95%+ extraction accuracy with schema enforcement",
},
"self_consistency": {
"description": "Generate multiple answers and pick majority",
"template": """Answer this question {n_samples} different ways,
then select the most common answer.
Question: {question}
Attempt 1:
Attempt 2:
Attempt 3:
Most consistent answer:""",
"best_for": ["factual QA", "reasoning under uncertainty"],
"improvement": "+10% accuracy over single-pass CoT",
},
"role_based": {
"description": "Assigns expert persona for domain-specific tasks",
"template": """You are a {role} with {years} years of experience.
Your expertise includes: {expertise}.
Given your background, {task}
Important: Base your response only on established {domain} knowledge.
If uncertain, clearly state your confidence level.""",
"best_for": ["domain expertise", "technical analysis", "advisory"],
"improvement": "Significant quality boost on specialized domains",
},
}
def render_pattern(pattern_name: str, **kwargs) -> str:
"""Render a prompt pattern with variables."""
if pattern_name not in PATTERNS:
raise ValueError(f"Unknown pattern: {pattern_name}")
template = PATTERNS[pattern_name]["template"]
return template.format(**kwargs)
if __name__ == "__main__":
for name, pattern in PATTERNS.items():
print(f"\n{'='*50}")
print(f"Pattern: {name}")
print(f"Best for: {', '.join(pattern['best_for'])}")
print(f"Expected improvement: {pattern['improvement']}")
print(f"{'='*50}")