📝 Prompt Templates trong n8n
🎯 Mục tiêu bài học
Sau bài học này, bạn sẽ:
✅ Hiểu tại sao cần Prompt Templates trong AI workflows
✅ Biết 3 phương pháp tạo templates trong n8n
✅ Áp dụng Few-Shot và Chain of Thought prompting
✅ Quản lý templates hiệu quả với best practices
Prompt engineering là chìa khóa để AI output chất lượng cao. Bài này cover cách tạo reusable templates trong n8n.
🔍 Tại sao cần Prompt Templates?
Checkpoint
Prompt Templates mang lại những lợi ích gì cho AI workflows?
📋 Basic Template Pattern
1[ROLE]2You are a {role} specializing in {specialty}.3 4[TASK]5{task_description}6 7[INPUT]8{user_input}9 10[OUTPUT FORMAT]11Respond in JSON format:12{13 "result": "...",14 "confidence": 0.0-1.015}16 17[CONSTRAINTS]18- Maximum {max_words} words19- Language: {language}20- Tone: {tone}Checkpoint
Một prompt template chuẩn bao gồm những phần nào?
🛠️ Template Implementation trong n8n
Method 1: Code Node Templates
1// Code node: Prepare Prompt2const templates = {3 emailReply: {4 system: `You are a professional email assistant. 5Reply to emails using a ${$json.tone || 'friendly'} tone.6Language: ${$json.language || 'Vietnamese'}.`,7 user: `Reply to this email:\n\n${$json.emailContent}`8 },9 10 summarize: {11 system: `You are a summarizer. Create concise summaries.12Format: bullet points. Max: ${$json.maxPoints || 5} points.`,13 user: `Summarize:\n\n${$json.text}`14 },15 16 classify: {17 system: `Classify the input into one of: ${$json.categories}.18Respond with JSON: {"category": "...", "confidence": 0.0-1.0}`,19 user: $json.text20 }21};2223const template = templates[$json.templateName];24return { system: template.system, user: template.user };Method 2: n8n Expressions
1// Trong OpenAI node - System Message2You are a {{ $json.role }} assistant.3Your task: {{ $json.task }}4Tone: {{ $json.tone }}5Language: {{ $json.language }}6 7// User Message 8{{ $json.content }}Method 3: Sub-workflow Templates
Checkpoint
Ba phương pháp implement prompt templates trong n8n là gì?
🧠 Few-Shot Prompting
1// Few-shot template cho classification2const fewShotPrompt = `3Classify the customer feedback as: positive, negative, neutral.45Examples:6Input: "Great product, fast shipping!"7Output: {"sentiment": "positive", "confidence": 0.95}89Input: "Product broke after 2 days"10Output: {"sentiment": "negative", "confidence": 0.90}1112Input: "It's okay, nothing special"13Output: {"sentiment": "neutral", "confidence": 0.80}1415Now classify:16Input: "${$json.feedback}"17Output:`;Checkpoint
Few-shot prompting hoạt động như thế nào và khi nào nên sử dụng?
💡 Chain of Thought Template
1const cotTemplate = `2Analyze the customer support ticket step by step:34Ticket: "${$json.ticket}"56Step 1: Identify the main issue7Step 2: Determine urgency (low/medium/high/critical)8Step 3: Suggest category (billing/technical/general/complaint)9Step 4: Draft a helpful response1011Provide your analysis as JSON:12{13 "issue": "...",14 "urgency": "...",15 "category": "...",16 "response": "..."17}`;Checkpoint
Chain of Thought khác gì so với direct prompting?
📊 Output Formatting Templates
1// JSON output2const jsonTemplate = `3Extract information and return as JSON:4{5 "name": "extracted name",6 "email": "extracted email",7 "phone": "extracted phone or null",8 "company": "extracted company or null"9}1011Text: ${$json.text}`;1213// Markdown output14const markdownTemplate = `15Create a summary in Markdown format:16## Summary17[one paragraph]1819## Key Points20- point 121- point 22223## Action Items24- [ ] action 12526Content: ${$json.content}`;Checkpoint
Khi nào nên dùng JSON output và khi nào nên dùng Markdown output?
🌟 Template Management Best Practices
- Version control: Lưu templates trong Code nodes, dễ track changes
- Variables: Dùng placeholders cho dynamic content
- Testing: Test mỗi template với nhiều inputs khác nhau
- Documentation: Comment rõ purpose của mỗi template
- Reuse: Dùng sub-workflows cho templates dùng nhiều lần
Checkpoint
Nguyên tắc quan trọng nhất khi quản lý prompt templates là gì?
📝 Bài Tập Thực Hành
- Tạo email reply template với 3 tone options (formal, friendly, casual)
- Build few-shot classification template cho product reviews
- Tạo CoT template cho customer support ticket analysis
- Build sub-workflow template reusable cho text summarization
Checkpoint
Bạn đã hoàn thành các bài tập chưa? Template nào hiệu quả nhất?
🚀 Bài tiếp theo
Email Classification nâng cao — Xây dựng hệ thống phân loại email thông minh với AI.
