File size: 8,142 Bytes
3417f1f
 
 
 
 
 
 
 
 
 
0ce67b7
 
3417f1f
 
0ce67b7
 
 
 
 
 
 
 
 
 
3417f1f
 
0ce67b7
3417f1f
0ce67b7
3417f1f
0ce67b7
3417f1f
0ce67b7
3417f1f
0ce67b7
 
3417f1f
0ce67b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3417f1f
 
 
0ce67b7
 
3417f1f
 
 
 
 
 
 
 
 
 
 
 
0ce67b7
3417f1f
 
 
 
 
 
 
 
 
 
 
 
 
0ce67b7
 
3417f1f
0ce67b7
 
3417f1f
 
 
 
 
 
 
 
 
 
0ce67b7
 
3417f1f
 
 
 
 
 
0ce67b7
3417f1f
0ce67b7
 
3417f1f
0ce67b7
 
 
 
 
3417f1f
0ce67b7
 
 
 
3417f1f
0ce67b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3417f1f
0ce67b7
 
 
 
 
3417f1f
0ce67b7
 
 
 
 
3417f1f
0ce67b7
 
 
 
 
3417f1f
0ce67b7
3417f1f
0ce67b7
3417f1f
0ce67b7
3417f1f
0ce67b7
 
 
 
 
3417f1f
0ce67b7
3417f1f
0ce67b7
 
 
 
 
 
 
 
3417f1f
0ce67b7
 
 
 
 
 
 
3417f1f
0ce67b7
 
 
 
3417f1f
0ce67b7
 
 
 
3417f1f
0ce67b7
 
3417f1f
0ce67b7
 
3417f1f
0ce67b7
 
 
3417f1f
0ce67b7
3417f1f
0ce67b7
 
 
3417f1f
0ce67b7
3417f1f
0ce67b7
3417f1f
0ce67b7
 
 
 
3417f1f
0ce67b7
 
 
 
 
 
 
 
 
 
 
3417f1f
0ce67b7
3417f1f
0ce67b7
 
3417f1f
 
 
0ce67b7
3417f1f
 
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
305
306
307
308
309
---
language:
- en
license: apache-2.0
tags:
- n8n
- workflow
- code-generation
- qwen2.5
- lora
- workflow-automation
- typescript
base_model: Qwen/Qwen2.5-Coder-1.5B-Instruct
pipeline_tag: text-generation
model-index:
- name: n8n-workflow-generator
  results:
  - task:
      type: text-generation
      name: Workflow Generation
    metrics:
    - type: accuracy
      value: 91.8
      name: Overall Test Score
---

# πŸš€ n8n Workflow Generator v1.0

A fine-tuned **Qwen2.5-Coder-1.5B** model that generates n8n workflows using TypeScript DSL.

## πŸ“Š Performance (Comprehensive Testing)

**Overall Score: 91.8%** ✨ (24 diverse test cases)

### Detailed Results by Category:
| Category | Score | Tests |
|----------|-------|-------|
| Simple Webhook | 92.2% | 3 |
| Conditional Routing | 93.3% | 3 |
| Scheduled Tasks | 95.6% | 3 |
| Form Processing | 93.3% | 2 |
| Multi-Service Integration | 83.3% | 3 |
| Data Processing | 93.3% | 3 |
| Error Handling | 88.9% | 3 |
| Complex Multi-Step | 91.7% | 2 |
| Manual & Email Triggers | 96.7% | 2 |

### Test Score Breakdown:
- **Basic Checks:** 98% (syntax, structure, node types)
- **Structural Checks:** 83% (connections, flow logic)
- **N8N-Specific:** 97% (valid nodes, DSL conventions)

### Grade Distribution:
- 🟒 **A (Excellent):** 83% of test cases
- 🟑 **B (Good):** 13% of test cases
- πŸ”΄ **D (Poor):** 4% of test cases

## 🎯 What It Does

Converts natural language descriptions into production-ready n8n workflows:

**Input:** "Create a webhook that sends data to Slack"

**Output:**
```typescript
const workflow = new Workflow('Webhook to Slack');
const webhook = workflow.add('n8n-nodes-base.webhook', {{
  path: '/data',
  method: 'POST'
}});
const slack = workflow.add('n8n-nodes-base.slack', {{
  channel: '#general',
  text: '={{{{ $json.message }}}}'
}});
webhook.to(slack);
```

## πŸš€ Quick Start

### Option 1: Using LoRA Adapter (Recommended)

```python
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
import torch

# Load base model
base_model = AutoModelForCausalLM.from_pretrained(
    "Qwen/Qwen2.5-Coder-1.5B-Instruct",
    torch_dtype=torch.float16,
    device_map="auto"
)

# Load fine-tuned adapter
model = PeftModel.from_pretrained(base_model, "Nishan30/n8n-workflow-generator")
tokenizer = AutoTokenizer.from_pretrained("Nishan30/n8n-workflow-generator")

# System prompt
system_prompt = """You are an expert n8n workflow generator. Given a user's request, you generate clean, functional TypeScript code using the @n8n-generator/core DSL.

Your output should:
- Only contain the code, no explanations
- Use the Workflow class from @n8n-generator/core
- Use workflow.add() to create nodes
- Use .to() or workflow.connect() for connections
- Be ready to compile directly to n8n JSON"""

# Generate
user_request = "Create a webhook that sends data to Slack"
messages = [
    {{"role": "system", "content": system_prompt}},
    {{"role": "user", "content": user_request}}
]

text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
inputs = tokenizer(text, return_tensors="pt").to(model.device)

outputs = model.generate(
    **inputs,
    max_new_tokens=512,
    temperature=0.3,
    do_sample=True,
    top_p=0.9,
    repetition_penalty=1.1
)

result = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(result)
```

### Option 2: Using Transformers Pipeline

```python
from transformers import pipeline

generator = pipeline(
    "text-generation",
    model="Nishan30/n8n-workflow-generator",
    device_map="auto"
)

prompt = "Create a scheduled workflow that fetches data daily and sends to Slack"
result = generator(prompt, max_new_tokens=512, temperature=0.3)
print(result[0]['generated_text'])
```

## 🌟 Supported Workflow Patterns

### βœ… Triggers
- `webhook` - HTTP endpoints
- `scheduleTrigger` - Cron-based scheduling
- `manualTrigger` - Manual execution
- `formTrigger` - Form submissions
- `emailTrigger` - Email-based triggers

### βœ… Actions & Integrations
- `slack`, `discord`, `telegram` - Messaging
- `gmail`, `email` - Email sending
- `httpRequest` - API calls
- `googleSheets`, `airtable`, `notion` - Databases
- And more...

### βœ… Data Processing
- `if`, `switch` - Conditional logic
- `set`, `filter`, `merge` - Data transformation
- `code` - Custom JavaScript/Python
- `stopAndError` - Error handling

## πŸ“ˆ Training Details

### Dataset
- **Total Examples:** 2,736 workflows
- **Training Set:** 2,462 examples
- **Validation Set:** 274 examples
- **Pattern Coverage:** 7 major workflow patterns
- **Quality:** Curated from production n8n workflows

### Training Configuration
- **Base Model:** Qwen/Qwen2.5-Coder-1.5B-Instruct
- **Method:** LoRA (Low-Rank Adaptation)
- **LoRA Rank:** 16
- **LoRA Alpha:** 16
- **Learning Rate:** 2e-4
- **Batch Size:** 2 (effective: 8 with gradient accumulation)
- **Epochs:** 10
- **Hardware:** NVIDIA Tesla T4 GPU (16GB)
- **Framework:** Transformers + Unsloth

### Optimization
- βœ… 4-bit quantization for memory efficiency
- βœ… Gradient checkpointing
- βœ… Flash Attention 2
- βœ… Early stopping based on validation loss

## 🎨 Example Workflows

### 1. Simple Webhook to Slack
```
User: "Create a webhook that posts to Slack"
Model: [Generates complete TypeScript DSL code]
```

### 2. Scheduled Data Sync
```
User: "Daily workflow that fetches API data and stores in database"
Model: [Generates schedule trigger + HTTP request + database storage]
```

### 3. Form Processing
```
User: "Contact form that validates and sends email"
Model: [Generates form trigger + validation + email sending]
```

### 4. Conditional Routing
```
User: "Route high-priority items to #urgent, others to #general"
Model: [Generates webhook + if condition + dual Slack outputs]
```

## 🌐 Try It Online

**Hugging Face Space:** [Coming Soon]

## πŸ“Š Benchmark Comparison

| Model | Size | Accuracy | Speed | Use Case |
|-------|------|----------|-------|----------|
| **n8n-workflow-generator** | 1.5B | 91.8% | Fast | Production-ready |
| GPT-3.5 (baseline) | 175B | ~85% | Slow | General purpose |
| CodeLlama-7B | 7B | ~88% | Medium | Code generation |

## πŸ”§ Advanced Usage

### Custom System Prompt
```python
custom_prompt = """You are a workflow expert. Generate n8n workflows with:
- Error handling for all HTTP requests
- Descriptive node names
- Production-ready configurations
"""
```

### Batch Generation
```python
requests = [
    "webhook to slack",
    "daily email report",
    "form to database"
]

for req in requests:
    workflow = generate_workflow(req)
    print(workflow)
```

### Integration with n8n
```python
import json
from n8n_generator import compile_to_json

# Generate DSL
dsl_code = model.generate(prompt)

# Compile to n8n JSON
workflow_json = compile_to_json(dsl_code)

# Import to n8n
# POST to http://your-n8n-instance/api/v1/workflows
```

## πŸ“ Limitations

- **Complex Logic:** May struggle with very complex multi-branch workflows (>10 nodes)
- **Custom Nodes:** Only supports built-in n8n nodes
- **Edge Cases:** Occasionally generates invalid node names (~8% of cases)

**Mitigation:** Add post-processing validation layer (see documentation)

## 🚧 Roadmap

- [ ] v1.1: Expand to 7B model for better accuracy (target: 95%+)
- [ ] v1.2: Add support for custom n8n nodes
- [ ] v1.3: Multi-language support (Python, JavaScript execution nodes)
- [ ] v2.0: Fine-tune on user feedback data

## πŸ“„ License

Apache 2.0

## πŸ™ Acknowledgments

Built with:
- [Qwen2.5-Coder](https://huggingface.co/Qwen/Qwen2.5-Coder-1.5B-Instruct) by Alibaba Cloud
- [Hugging Face Transformers](https://github.com/huggingface/transformers)
- [PEFT](https://github.com/huggingface/peft) for LoRA
- [Unsloth](https://github.com/unslothai/unsloth) for training optimization

## πŸ“§ Contact

- **Issues:** [GitHub Issues](https://github.com/Nishan30/n8n-workflow-generator/issues)
- **Discussions:** [Hugging Face Discussions](https://huggingface.co/Nishan30/n8n-workflow-generator/discussions)

---

**⭐ Star this model if you find it useful!**

*Last updated: December 2024*