{
  "name": "Vorlux AI | Fine-Tune Pipeline",
  "nodes": [
    {
      "id": "d74f3bfb-0805-4dbc-b6e8-0edf219dd39c",
      "name": "Weekly Friday",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        220,
        300
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hours",
              "hoursInterval": 168
            }
          ]
        }
      }
    },
    {
      "id": "70e4946e-13e3-4886-920f-d2c62b5ebf12",
      "name": "Export Samples",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        460,
        300
      ],
      "parameters": {
        "method": "POST",
        "url": "={{$env.VORLUX_HUB_URL}}/api/admin/finetune/export",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "{\"minQuality\":5,\"maxSamples\":5000,\"departments\":[\"orchestration\",\"content\",\"community\",\"stream_ops\",\"growth\",\"brand\"]}",
        "options": {
          "timeout": 120000
        }
      },
      "notes": "Exports high-quality inference samples to department JSONL files"
    },
    {
      "id": "3d60b933-6389-4dd2-ba67-a624fe6992f0",
      "name": "Check Readiness",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        700,
        300
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const result=$input.first().json.data||{};const departments=result.departments||{};const ready=Object.entries(departments).filter(([k,v])=>v.trainCount>=200);const totalSamples=Object.values(departments).reduce((s,d)=>s+(d.trainCount||0),0);return [{json:{ready:ready.map(r=>r[0]),readyCount:ready.length,totalSamples,canTrain:totalSamples>=500}}];"
      },
      "notes": "Checks if enough samples for training (min 500 total, 200 per department)"
    },
    {
      "id": "8fa5bc85-c17f-4257-96b3-9f2909ec0f18",
      "name": "Can Train?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        940,
        300
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "leftValue": "={{ $json.canTrain }}",
              "rightValue": "true",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        }
      }
    },
    {
      "id": "8ff68b48-1543-4df7-9a7a-71cefc7988bb",
      "name": "Trigger Training",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1180,
        200
      ],
      "parameters": {
        "method": "POST",
        "url": "={{$env.VORLUX_HUB_URL}}/api/admin/finetune/train",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\"baseModel\":\"qwen2.5:7b\",\"method\":\"lora\",\"departments\":{{ JSON.stringify($json.ready) }},\"config\":{\"loraRank\":16,\"loraAlpha\":32,\"epochs\":3,\"learningRate\":2e-5}}",
        "options": {
          "timeout": 30000
        }
      },
      "notes": "Kicks off LoRA fine-tuning on ready departments"
    },
    {
      "id": "96216215-5164-4f2c-94ea-46faf5a30a71",
      "name": "Report",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1180,
        400
      ],
      "parameters": {
        "method": "POST",
        "url": "={{$env.DISCORD_OPS_WEBHOOK}}",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\"embeds\":[{\"title\":\"\ud83e\udde0 Fine-Tune Pipeline\",\"description\":\"{{ $json.canTrain ? \"Training started for: \" + $json.ready.join(\", \") : \"Not enough samples yet (\" + $json.totalSamples + \"/500)\" }}\\n\\nReady departments: {{ $json.readyCount }}\\nTotal samples: {{ $json.totalSamples }}\",\"color\":{{ $json.canTrain ? 5763719 : 16776960 }}}]}",
        "options": {
          "timeout": 10000
        }
      }
    }
  ],
  "connections": {
    "Weekly Friday": {
      "main": [
        [
          {
            "node": "Export Samples",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Export Samples": {
      "main": [
        [
          {
            "node": "Check Readiness",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Readiness": {
      "main": [
        [
          {
            "node": "Can Train?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Can Train?": {
      "main": [
        [
          {
            "node": "Trigger Training",
            "type": "main",
            "index": 0
          },
          {
            "node": "Report",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "saveManualExecutions": true,
    "saveExecutionProgress": true
  }
}