⚙️ OpenAI Setup và First AI Workflow
🎯 Mục tiêu bài học
Sau bài học này, bạn sẽ:
✅ Cấu hình OpenAI credential trong n8n
✅ Xây dựng Text Enhancer workflow với webhook
✅ Tạo AI Summarizer workflow
✅ Biết cách chọn model phù hợp và xử lý lỗi
Hướng dẫn step-by-step setup OpenAI credential trong n8n và build workflow AI đầu tiên.
⚙️ Setup OpenAI Credential
Step 1: Tạo API Key
- Truy cập platform.openai.com
- Vào Settings, API Keys
- Click "Create new secret key"
- Đặt tên: "n8n-integration"
- Copy key (chỉ hiển thị 1 lần)
Step 2: Add Credential trong n8n
- n8n, vào Settings, Credentials
- Click Add Credential
- Search "OpenAI"
- Paste API key
- Test connection
Step 3: Verify
1// Test node: OpenAI Chat Model2{3 "model": "gpt-4o-mini",4 "messages": [5 {6 "role": "user", 7 "content": "Say hello in Vietnamese"8 }9 ],10 "temperature": 0.7,11 "maxTokens": 10012}13// Expected: "Xin chào!"Checkpoint
Các bước để setup OpenAI credential trong n8n là gì?
⚡ First AI Workflow: Text Enhancer
Workflow Setup
Node 1: Webhook
- Method: POST
- Path:
/enhance-text - Response: "Respond to Webhook" node
Node 2: OpenAI
- Operation: Message a Model
- Model: gpt-4o-mini
- Prompt:
1Improve the following text. Make it more professional, 2fix grammar, and improve clarity. Keep the same meaning.3 4Text: {{ $json.body.text }}Node 3: Respond to Webhook
1// Response body2{3 "original": "{{ $('Webhook').item.json.body.text }}",4 "enhanced": "{{ $json.message.content }}"5}Test
1curl -X POST http://localhost:5678/webhook/enhance-text \2 -H "Content-Type: application/json" \3 -d '{"text": "i want to make this text more better and professional like"}'Checkpoint
Text Enhancer workflow gồm những nodes chính nào?
🛠️ Workflow 2: AI Summarizer
1// OpenAI System Prompt2const systemPrompt = `3You are a text summarizer. Create concise summaries with:4- One-line summary (max 20 words)5- Key points (3-5 bullet points)6- Action items (if any)78Output as JSON.9`;1011// User message12const userMessage = `Summarize this: {{ $json.body.text }}`;Checkpoint
AI Summarizer trích xuất những thông tin gì từ text input?
📊 Model Selection Guide
| Model | Speed | Quality | Cost | Best For |
|---|---|---|---|---|
| gpt-4o-mini | Fast | Good | $$ | Daily workflows |
| gpt-4o | Medium | Excellent | $$$$ | Complex tasks |
| gpt-3.5-turbo | Fast | OK | $ | Simple tasks |
Luôn bắt đầu với gpt-4o-mini. Nếu output quality không đạt, switch sang gpt-4o. Không cần dùng model đắt nhất cho mọi task.
Checkpoint
Khi nào nên dùng gpt-4o-mini và khi nào nên dùng gpt-4o?
🔧 Error Handling
1// Add Error Trigger node2// Common errors:3// 1. 429 Too Many Requests → Add delay/retry4// 2. 401 Unauthorized → Check API key5// 3. 500 Server Error → Retry with backoff67// Retry configuration in OpenAI node:8// - Retry On Fail: true9// - Max Retries: 310// - Wait Between Retries: 1000msCheckpoint
Những lỗi phổ biến nào khi dùng OpenAI node và cách xử lý?
📝 Bài Tập Thực Hành
- Setup: Configure OpenAI credential, test với simple prompt
- Text Enhancer: Build webhook workflow, test với 3 different texts
- Summarizer: Build summarizer, test với long article
- Experiment: Thử temperature 0.1 vs 0.9, so sánh kết quả
Checkpoint
Bạn đã hoàn thành các bài tập chưa? So sánh kết quả temperature 0.1 vs 0.9?
🚀 Bài tiếp theo
Prompt Templates — Tạo và quản lý reusable prompt templates cho AI workflows.
