IA a n8n
6.1. AI Nodes a N8N
N8N té suport natiu per a IA amb nodes especialitzats.
Nodes d'IA disponibles:
- OpenAI (GPT-4, GPT-3.5, DALL-E, Whisper)
- Anthropic Claude
- Google PaLM/Gemini
- Hugging Face
- Cohere
- AI Agent (LangChain)
- Text Classifier
- Sentiment Analysis
Exemple: OpenAI GPT-4
[Trigger: New Support Ticket]
↓
[OpenAI Chat Model]
Model: gpt-4
System Message: "You are a customer support specialist..."
User Message: {{$json.ticket_description}}
Temperature: 0.3
Max Tokens: 500
↓
[Parse AI Response]
↓
[Update Ticket with AI Suggestion]
6.2. Casos d'ús amb IA
1. Anàlisi de Sentiment
Pipeline de sentiment analysis:
[Get Customer Reviews]
↓
[Split In Batches: 50]
↓
[OpenAI Chat Model]
Prompt: "Analyze sentiment of this review and return JSON:
{sentiment: 'positive'|'negative'|'neutral',
confidence: 0-1,
key_topics: []}"
Review: {{$json.review_text}}
↓
[Parse JSON Response]
↓
[IF: Negative sentiment]
├─ true → [Alert Customer Success Team]
└─ false → [Store in Analytics DB]
2. Extracció d'Informació
[Receive Invoice PDF]
↓
[Extract Text from PDF]
↓
[OpenAI Chat Model]
Prompt: "Extract invoice data as JSON:
{
invoice_number: '',
date: '',
vendor: '',
total_amount: 0,
line_items: []
}"
Text: {{$json.pdf_text}}
↓
[Validate Extracted Data]
↓
[Save to Accounting System]
3. Classificació de Textos
// Code Node: Batch classification
const texts = $input.all();
const batches = [];
// Agrupa en lots de 10
for (let i = 0; i < texts.length; i += 10) {
batches.push(texts.slice(i, i + 10));
}
// Classifica cada lot
const results = [];
for (const batch of batches) {
const prompt = `Classify these texts into categories: Tech, Finance, Health, Other
${batch.map((t, i) => `${i+1}. ${t.json.text}`).join('\n')}
Return JSON array: [{text_id: 1, category: 'Tech'}, ...]`;
const response = await openai.complete(prompt);
results.push(...response);
}
return results.map(r => ({json: r}));
4. Generació de Resums
[Daily News Articles] (100 articles)
↓
[Filter: Technology category]
↓
[OpenAI Chat Model]
System: "Summarize tech news in 2-3 sentences"
Articles: {{$json.articles}}
↓
[Combine Summaries]
↓
[Generate Email Newsletter]
↓
[Send to Subscribers]
6.3. AI en pipelines de dades
Enriquiment intel·ligent:
[Raw Customer Data]
↓
[OpenAI: Standardize Company Names]
Prompt: "Standardize company name: {{$json.company_raw}}"
Examples: "MSFT → Microsoft, GOOGL → Google"
↓
[OpenAI: Infer Industry]
Prompt: "Based on company name and description,
classify industry"
↓
[OpenAI: Generate Company Summary]
↓
[Enriched Data]
Detecció d'anomalies:
// Code Node: AI-powered anomaly detection
const metrics = $input.all();
const historicalData = metrics.slice(0, -1);
const currentData = metrics[metrics.length - 1];
const prompt = `
Historical metrics (mean±std):
${JSON.stringify(calculateStats(historicalData))}
Current metrics:
${JSON.stringify(currentData)}
Analyze if current metrics show anomalies. Return JSON:
{
is_anomaly: boolean,
anomalous_fields: [],
severity: 'low'|'medium'|'high',
explanation: ''
}
`;
const aiAnalysis = await openai.complete(prompt);
if (aiAnalysis.is_anomaly) {
// Trigger alert
}
Neteja intel·ligent: