Files
nusquama 72a2883de0 creation
2026-03-10 12:13:57 +08:00

92 lines
6.8 KiB
Markdown

Optimize n8n workflow JSON using Azure OpenAI GPT-4o-mini
https://n8nworkflows.xyz/workflows/optimize-n8n-workflow-json-using-azure-openai-gpt-4o-mini-13788
# Optimize n8n workflow JSON using Azure OpenAI GPT-4o-mini
# Reference Document: AI JSON Optimiser Workflow
## 1. Workflow Overview
The **AI JSON Optimiser** is a specialized automation designed to refine and improve n8n workflow JSON files. It accepts an existing workflow via a POST webhook, processes it through an AI agent powered by Azure OpenAI (GPT-4o-mini), and returns a cleaned, optimized, and import-ready version of the workflow as a downloadable file.
The workflow is organized into three main functional blocks:
* **1.1 Input & Extraction:** Receives the raw JSON data and validates the payload structure.
* **1.2 AI Optimization Engine:** Uses a Large Language Model (LLM) to perform structural and performance-based improvements on the JSON.
* **1.3 Output Processing & Delivery:** Sanitizes the AI's response, converts it back into a valid n8n file format, and delivers it to the requester.
---
## 2. Block-by-Block Analysis
### 2.1 Input & Extraction
**Overview:** This block acts as the interface for the workflow. It captures the incoming HTTP request and ensures that the data provided is a valid n8n workflow object before passing it to the AI.
* **Nodes Involved:**
* `Receive Workflow JSON` (Webhook)
* `Extract & Validate Workflow Body` (Code)
* **Node Details:**
* **Receive Workflow JSON:** A Webhook node configured for `POST` requests. It expects a JSON body. If the request is successful, it waits for the `Respond to Webhook` node to finish before sending the response.
* **Extract & Validate Workflow Body:** A JavaScript Code node that performs a safety check. It verifies that `body.workflow` exists. If the input is empty or malformed, it throws a specific error to prevent the AI from processing null data.
### 2.2 AI Optimization Engine
**Overview:** The core of the workflow. This block leverages generative AI to analyze node connections, identify redundancies, and apply best practices for n8n development.
* **Nodes Involved:**
* `Optimize Workflow via AI Agent` (AI Agent)
* `Azure OpenAI Chat Model1` (Azure Chat Model)
* **Node Details:**
* **Optimize Workflow via AI Agent:** An AI Agent node (LangChain) using a "Define" prompt type. The system message contains strict instructions: return *only* raw JSON, remove redundant nodes, improve naming, ensure logical positioning (200px spacing), and preserve functionality.
* **Azure OpenAI Chat Model1:** Connects to the `gpt-4o-mini` deployment. It provides the intelligence for the optimization tasks.
* **Edge Cases:** AI might occasionally include markdown code blocks (```json ... ```) despite instructions, which requires the next block to handle cleanup.
### 2.3 Output Processing & Delivery
**Overview:** This block ensures the AI's text output is transformed back into a functional system file. It handles parsing and the final binary file generation.
* **Nodes Involved:**
* `Parse, Strip & Validate Optimized JSON` (Code)
* `Convert Optimized Workflow to File` (Convert to File)
* `Return Optimized File to Caller` (Respond to Webhook)
* **Node Details:**
* **Parse, Strip & Validate Optimized JSON:** A JavaScript Code node that cleans the AI's string. It trims whitespace, removes markdown backticks if present, and executes `JSON.parse()`. It also validates that the result contains essential n8n keys like `nodes` and `connections`.
* **Convert Optimized Workflow to File:** Converts the cleaned JSON object into a binary file format (`.json`).
* **Return Optimized File to Caller:** Finalizes the HTTP request by sending the binary file back to the user.
---
## 3. Summary Table
| Node Name | Node Type | Functional Role | Input Node(s) | Output Node(s) | Sticky Note |
| :--- | :--- | :--- | :--- | :--- | :--- |
| Receive Workflow JSON | Webhook | Entry Point | (None) | Extract & Validate... | ## 📥 Input & Extraction Receives the POST request... |
| Extract & Validate... | Code | Validation | Receive Workflow JSON | Optimize Workflow... | ## 📥 Input & Extraction Receives the POST request... |
| Optimize Workflow... | AI Agent | AI Processing | Extract & Validate... | Parse, Strip & Validate... | ## 🤖 AI Optimization Engine The AI Agent sends the full workflow... |
| Azure OpenAI Chat Model1 | Azure Chat Model | AI Engine | (None) | Optimize Workflow... | ⚠️ **Azure OpenAI Credentials Required** This node requires a valid azureOpenAiApi... |
| Parse, Strip & Validate... | Code | Data Sanitization | Optimize Workflow... | Convert Optimized... | ## 📤 Output Processing & Delivery Strips any accidental markdown... |
| Convert Optimized... | Convert to File | File Generation | Parse, Strip & Validate... | Return Optimized File... | ## 📤 Output Processing & Delivery Strips any accidental markdown... |
| Return Optimized File... | Respond to Webhook | HTTP Response | Convert Optimized... | (None) | ## 📤 Output Processing & Delivery Strips any accidental markdown... |
---
## 4. Reproducing the Workflow from Scratch
1. **Webhook Setup:** Create a **Webhook** node. Set the method to `POST` and set `Response Mode` to `When Last Node Finishes`.
2. **Input Validation:** Add a **Code** node. Use JavaScript to extract `json.body.workflow` and add basic `if (!workflow) throw Error(...)` logic to prevent empty runs.
3. **AI Integration:**
* Add an **AI Agent** node. Set the `Prompt Type` to `Define`.
* Inside the Agent, add an **Azure Chat Model** node. Connect your `azureOpenAiApi` credentials and set the Model name to `gpt-4o-mini`.
* In the Agent's **System Message**, paste instructions requiring the model to return raw n8n JSON, optimize for speed/clarity, and maintain a 200px node spacing.
4. **Cleaning Logic:** Add another **Code** node. Use a script to check if the output is a string, remove ```json tags using `.replace()`, and then use `JSON.parse()` to turn it back into an object.
5. **File Conversion:** Add a **Convert to File** node. Set the operation to `Convert to JSON` (which creates a binary file from the JSON input).
6. **Final Response:** Add a **Respond to Webhook** node. Set the `Respond With` parameter to `Binary File`.
7. **Connections:** Link the nodes sequentially as described in the summary table.
---
## 5. General Notes & Resources
| Note Content | Context or Link |
| :--- | :--- |
| Full setup instructions and overview of the internal logic | Included in the "📋 Overview" Sticky Note in the workflow. |
| Critical: Deployment naming must match `gpt-4o-mini` | Mandatory for the Azure Chat Model node configuration. |
| Optimal Node Spacing | The AI is instructed to maintain a 200px minimum spacing for visual clarity. |