MinAI - Về trang chủ
Lý thuyết
5/1330 phút
Đang tải...

Prompt Templates trong n8n

Tạo và quản lý reusable prompt templates cho AI workflows

📝 Prompt Templates trong n8n

0

🎯 Mục tiêu bài học

TB5 min

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.

1

🔍 Tại sao cần Prompt Templates?

TB5 min
Diagram
Đang vẽ diagram...

Checkpoint

Prompt Templates mang lại những lợi ích gì cho AI workflows?

2

📋 Basic Template Pattern

TB5 min
Ví dụ
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.0
15}
16
17[CONSTRAINTS]
18- Maximum {max_words} words
19- Language: {language}
20- Tone: {tone}

Checkpoint

Một prompt template chuẩn bao gồm những phần nào?

3

🛠️ Template Implementation trong n8n

TB5 min

Method 1: Code Node Templates

JavaScript
1// Code node: Prepare Prompt
2const 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.text
20 }
21};
22
23const template = templates[$json.templateName];
24return { system: template.system, user: template.user };

Method 2: n8n Expressions

Ví dụ
1// Trong OpenAI node - System Message
2You 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

Diagram
Đang vẽ diagram...

Checkpoint

Ba phương pháp implement prompt templates trong n8n là gì?

4

🧠 Few-Shot Prompting

TB5 min
JavaScript
1// Few-shot template cho classification
2const fewShotPrompt = `
3Classify the customer feedback as: positive, negative, neutral.
4
5Examples:
6Input: "Great product, fast shipping!"
7Output: {"sentiment": "positive", "confidence": 0.95}
8
9Input: "Product broke after 2 days"
10Output: {"sentiment": "negative", "confidence": 0.90}
11
12Input: "It's okay, nothing special"
13Output: {"sentiment": "neutral", "confidence": 0.80}
14
15Now 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?

5

💡 Chain of Thought Template

TB5 min
JavaScript
1const cotTemplate = `
2Analyze the customer support ticket step by step:
3
4Ticket: "${$json.ticket}"
5
6Step 1: Identify the main issue
7Step 2: Determine urgency (low/medium/high/critical)
8Step 3: Suggest category (billing/technical/general/complaint)
9Step 4: Draft a helpful response
10
11Provide 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?

6

📊 Output Formatting Templates

TB5 min
JavaScript
1// JSON output
2const 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}
10
11Text: ${$json.text}`;
12
13// Markdown output
14const markdownTemplate = `
15Create a summary in Markdown format:
16## Summary
17[one paragraph]
18
19## Key Points
20- point 1
21- point 2
22
23## Action Items
24- [ ] action 1
25
26Content: ${$json.content}`;

Checkpoint

Khi nào nên dùng JSON output và khi nào nên dùng Markdown output?

7

🌟 Template Management Best Practices

TB5 min
Best Practices
  1. Version control: Lưu templates trong Code nodes, dễ track changes
  2. Variables: Dùng placeholders cho dynamic content
  3. Testing: Test mỗi template với nhiều inputs khác nhau
  4. Documentation: Comment rõ purpose của mỗi template
  5. 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ì?

8

📝 Bài Tập Thực Hành

TB5 min
Exercises
  1. Tạo email reply template với 3 tone options (formal, friendly, casual)
  2. Build few-shot classification template cho product reviews
  3. Tạo CoT template cho customer support ticket analysis
  4. 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.