{
  "name": "Kreativ: RAG Ingestion Pipeline",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "rag-ingest",
        "responseMode": "responseNode",
        "options": {}
      },
      "name": "Webhook: Ingest\u00e3o RAG",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "const body = $json.body || $json;\nconst content = body.content || '';\nconst fileName = body.fileName || 'upload.pdf';\nconst courseId = parseInt(body.course_int_id || 19, 10);\n\n// Chunking simples (~800 chars)\nconst CHUNK_SIZE = 800;\nconst chunks = [];\nfor (let i = 0; i < content.length; i += CHUNK_SIZE) {\n  chunks.push({\n    json: {\n      content: content.substring(i, i + CHUNK_SIZE),\n      fileName,\n      courseId,\n      index: Math.floor(i / CHUNK_SIZE)\n    }\n  });\n}\nreturn chunks;"
      },
      "name": "Chunking",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.deepseek.com/v1/embeddings",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer <redacted-credential>"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ { model: 'deepseek-embedding', input: $json.content } }}",
        "options": {}
      },
      "name": "Gerar Embedding",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        440,
        0
      ],
      "continueOnFail": true
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "INSERT INTO document_chunks (source_file, chunk_index, content, embedding, metadata)\nVALUES (\n  '{{ $('Chunking').item.json.fileName }}',\n  {{ $('Chunking').item.json.index }},\n  '{{ $('Chunking').item.json.content.replace(/'/g, \"''\") }}',\n  '[{{ $json.data[0].embedding.join(',') }}]',\n  '{\"course_id\": {{ $('Chunking').item.json.courseId }}}'::jsonb\n);",
        "options": {}
      },
      "name": "Salvar no Postgres",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        660,
        0
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={ \"success\": true, \"message\": \"Processamento RAG conclu\u00eddo\" }"
      },
      "name": "Responder",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        880,
        0
      ]
    }
  ],
  "connections": {
    "Webhook: Ingest\u00e3o RAG": {
      "main": [
        [
          {
            "node": "Chunking",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Chunking": {
      "main": [
        [
          {
            "node": "Gerar Embedding",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gerar Embedding": {
      "main": [
        [
          {
            "node": "Salvar no Postgres",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Salvar no Postgres": {
      "main": [
        [
          {
            "node": "Responder",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}