-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathxml-prompting-mastery
More file actions
304 lines (257 loc) 路 9.77 KB
/
Copy pathxml-prompting-mastery
File metadata and controls
304 lines (257 loc) 路 9.77 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# XML Prompting Mastery Guide
## Why XML Prompting Works
XML prompting provides superior results because:
1. **Clear Separation**: Distinguishes instructions from context from data
2. **Machine Readability**: AI models parse structured data more accurately
3. **Consistency**: Standardized tags create repeatable patterns
4. **Debugging**: Easy to identify which part of the prompt caused issues
5. **Composability**: Templates can be combined and reused
## Universal XML Template Structure
```xml
<instructions>
[Clear, specific requirements with constraints]
</instructions>
<context>
<project_info>
[Current system state and technology stack]
</project_info>
<existing_patterns>
[Reference implementations and established conventions]
</existing_patterns>
<business_rules>
[Domain-specific requirements and validation rules]
</business_rules>
</context>
<constraints>
<technical>
[Code quality, performance, security requirements]
</technical>
<architectural>
[System design principles and patterns to follow]
</architectural>
<operational>
[Deployment, monitoring, maintenance considerations]
</operational>
</constraints>
<examples>
<success_pattern>
[Reference to well-implemented similar feature]
</success_pattern>
<anti_pattern>
[What to avoid based on past experience]
</anti_pattern>
</examples>
<output_format>
[Specific structure and deliverable requirements]
</output_format>
<validation_criteria>
[How to measure success and quality gates]
</validation_criteria>
```
## Advanced XML Patterns
### Multi-Shot Learning Template
```xml
<examples>
<example id="authentication_success">
<situation>User login system with JWT tokens</situation>
<approach>
- JWT with refresh token rotation
- Rate limiting on login attempts
- Secure password hashing with bcrypt
- Session management with Redis
</approach>
<implementation>
- POST /api/auth/login with validation
- Middleware for token verification
- Automatic token refresh handling
- Graceful logout with token cleanup
</implementation>
<outcome>99.9% uptime, zero security incidents, <2s response time</outcome>
<lessons>Token rotation prevents long-term compromise, rate limiting stops brute force</lessons>
</example>
<example id="authentication_failure">
<situation>Previous session-based authentication</situation>
<approach>Server-side sessions with cookies</approach>
<problems>
- Session fixation vulnerabilities
- Difficult horizontal scaling
- Complex cleanup processes
- Poor mobile experience
</problems>
<lessons>Stateless authentication scales better and provides better security</lessons>
</example>
</examples>
<instructions>
Based on the success patterns and avoiding the failure patterns above,
implement authentication for [specific system] that handles [specific requirements]
</instructions>
```
### Chain of Thought Template
```xml
<thinking>
Let me analyze this step by step:
1. Requirements analysis:
- [Break down the specific requirements]
- [Identify critical success factors]
- [Note technical constraints]
2. Architecture considerations:
- [Evaluate different technical approaches]
- [Consider scalability and performance implications]
- [Assess security and compliance requirements]
3. Implementation strategy:
- [Choose optimal approach with justification]
- [Plan implementation phases]
- [Identify risks and mitigation strategies]
4. Quality assurance approach:
- [Define testing strategy]
- [Plan monitoring and observability]
- [Establish success metrics]
</thinking>
<solution>
Based on my analysis above, here's the recommended approach:
[Provide the detailed technical solution]
</solution>
```
### Conditional Decision Template
```xml
<decision_framework>
<criteria>
<performance>Weight: 0.3 | Requirement: <100ms response time</performance>
<scalability>Weight: 0.25 | Requirement: Handle 10K concurrent users</scalability>
<maintainability>Weight: 0.25 | Requirement: Easy to debug and extend</maintainability>
<cost>Weight: 0.2 | Requirement: <$1000/month infrastructure</cost>
</criteria>
<options>
<option id="microservices">
<performance>Score: 8</performance>
<scalability>Score: 10</scalability>
<maintainability>Score: 6</maintainability>
<cost>Score: 4</cost>
<weighted_score>7.1</weighted_score>
</option>
<option id="monolithic">
<performance>Score: 9</performance>
<scalability>Score: 7</scalability>
<maintainability>Score: 8</maintainability>
<cost>Score: 9</cost>
<weighted_score>8.05</weighted_score>
</option>
</options>
<recommendation>
Based on weighted criteria, recommend monolithic architecture
with clear service boundaries for future extraction if needed.
</recommendation>
</decision_framework>
```
## Specialized Domain Templates
### API Design Template
```xml
<api_design_request>
<resource_specification>
- Entity: User Management System
- Core Operations: Create, Read, Update, Delete, Search users
- Relationships: Users -> Profiles (1:1), Users -> Organizations (N:M)
- Business Rules: Email uniqueness, role-based permissions, audit trail
</resource_specification>
<technical_requirements>
- Protocol: REST with JSON payloads
- Authentication: JWT tokens with role-based access
- Validation: Request/response schema validation
- Rate Limiting: 100 requests/minute per user
- Pagination: Cursor-based for performance
- Versioning: URL path versioning (v1, v2)
</technical_requirements>
<existing_patterns>
- Error Response Format: {error: {code, message, details}}
- Success Response Format: {data, meta: {pagination, timestamps}}
- Authentication Header: Authorization: Bearer <token>
- API Route Structure: /api/v1/[resource]/[id]/[sub-resource]
</existing_patterns>
<deliverables>
1. OpenAPI 3.0 specification with complete schemas
2. Request/response examples for all endpoints
3. Error response documentation with HTTP status codes
4. Rate limiting and authentication specifications
5. Integration test examples
</deliverables>
</api_design_request>
```
### Database Schema Template
```xml
<database_design_request>
<business_requirements>
- Domain: E-commerce order management
- Entities: Users, Products, Orders, OrderItems, Payments
- Relationships: Complex many-to-many with historical tracking
- Volume: 1M users, 100K products, 10K orders/day
- Query Patterns: User order history, product inventory, sales reporting
</business_requirements>
<technical_constraints>
- Database: PostgreSQL 15+ with JSONB support
- Performance: <100ms for common queries
- Consistency: ACID compliance for financial transactions
- Scalability: Read replicas for reporting queries
- Backup: Point-in-time recovery capability
</technical_constraints>
<existing_conventions>
- Table Naming: snake_case, plural nouns
- Column Naming: snake_case with descriptive names
- Primary Keys: UUID v4 for distributed systems
- Timestamps: created_at, updated_at on all tables
- Foreign Keys: Explicit constraints with cascade rules
- Indexes: Composite indexes for common query patterns
</existing_conventions>
<deliverables>
1. Entity Relationship Diagram (ERD)
2. Table definitions with complete column specifications
3. Index strategy with performance justifications
4. Migration scripts with rollback procedures
5. Query optimization recommendations
</deliverables>
</database_design_request>
```
## XML Prompting Best Practices
### Tag Organization Principles
1. **Hierarchical Clarity**: Use nested tags only when logical relationships exist
2. **Consistent Naming**: Establish tag vocabulary and stick to it across projects
3. **Semantic Meaning**: Tag names should clearly indicate content purpose
4. **Balanced Granularity**: Not too broad (lose specificity) or too narrow (excessive nesting)
### Content Structuring Guidelines
```xml
<!-- Good: Clear, specific, actionable -->
<constraints>
<performance>API responses must complete within 200ms</performance>
<security>All endpoints require JWT authentication</security>
<compatibility>Must work with existing React 18 components</compatibility>
</constraints>
<!-- Avoid: Vague, mixed concerns -->
<requirements>
Make it fast and secure and compatible with everything
</requirements>
```
### Error Prevention Strategies
1. **Always close tags properly**: Use XML validators during template creation
2. **Separate instruction types**: Don't mix "what to do" with "how to do it"
3. **Include concrete examples**: Abstract instructions lead to hallucinations
4. **Test with variations**: Use same template with different inputs to verify robustness
### Template Composition Techniques
```xml
<!-- Base template -->
<base_request>
<instructions>[Specific task]</instructions>
<context>[Project context]</context>
<constraints>[Technical constraints]</constraints>
</base_request>
<!-- Specialized extensions -->
<security_requirements>
<authentication>[Auth patterns]</authentication>
<authorization>[Permission patterns]</authorization>
<audit_trail>[Logging requirements]</audit_trail>
</security_requirements>
<performance_requirements>
<response_time>[Timing constraints]</response_time>
<throughput>[Volume requirements]</throughput>
<resource_usage>[Memory/CPU limits]</resource_usage>
</performance_requirements>
```
XML prompting transforms AI interaction from conversational guesswork into precise engineering specifications, resulting in more predictable, higher-quality outputs that align with your architectural intentions.