{
  "name": "Reranker",
  "nodes": [
    {
      "parameters": {
        "operation": "download",
        "fileId": {
          "__rl": true,
          "value": "1ihasqbcDf8vv_JpQOjFsCc6K-amHK_B5",
          "mode": "list",
          "cachedResultName": "Reglas Golf (Simplificadas).pdf",
          "cachedResultUrl": "https://drive.google.com/file/d/1ihasqbcDf8vv_JpQOjFsCc6K-amHK_B5/view?usp=drivesdk"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        380,
        -380
      ],
      "id": "7e5a111b-7c56-49cc-829a-904165636dec",
      "name": "Download File",
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsonMode": "expressionData",
        "jsonData": "={{ $('Code').item.json.fullText }}",
        "options": {
          "metadata": {
            "metadataValues": [
              {
                "name": "ruleNumber",
                "value": "={{ $json.ruleNumber }}"
              }
            ]
          }
        }
      },
      "type": "@n8n/n8n-nodes-langchain.documentDefaultDataLoader",
      "typeVersion": 1.1,
      "position": [
        940,
        -240
      ],
      "id": "a8041f32-eaf4-411c-a3b5-132c948ef70f",
      "name": "Default Data Loader"
    },
    {
      "parameters": {
        "operation": "pdf",
        "options": {}
      },
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1,
      "position": [
        520,
        -380
      ],
      "id": "7f61c56a-5617-4233-a833-d31da60f8548",
      "name": "Extract from File"
    },
    {
      "parameters": {
        "jsCode": "// \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n// n8n Code Node \u2013 Dividir \u00abRegla X\u00bb en \u00edtems independientes\n// Entrada : 1er \u00edtem \u2192 .json.text  (todas las reglas juntas)\n// Salida  : un \u00edtem por regla, con n\u00famero, t\u00edtulo, texto completo e \u00edndice\n// \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\n/** 1. Obtener el texto completo */\nconst inputText = $input.first().json.text ?? '';\nif (!inputText) {\n\tthrow new Error('El campo json.text est\u00e1 vac\u00edo o no existe');\n}\n\n/** 2. Partir por \u201cRegla N\u201d (look-ahead para conservar el encabezado) */\nconst ruleSections = inputText.split(/(?=Regla\\s+\\d+)/);\n\n/** 3. Limpiar posibles elementos vac\u00edos (antes de la primera regla) */\nconst cleaned = ruleSections.filter(sec => sec.trim().startsWith('Regla'));\n\n/** 4. Crear los \u00edtems de salida */\nconst outputItems = cleaned.map((ruleText, idx) => {\n\n\t// 4-a) N\u00famero de regla\n\tconst nMatch = ruleText.match(/Regla\\s+(\\d+)/);\n\tconst ruleNumber = nMatch ? nMatch[1] : String(idx + 1);\n\n\t// 4-b) T\u00edtulo (lo que va tras el guion y antes del salto de l\u00ednea)\n\t//      Ejemplo: \"Regla 3 - Stroke Play (Juego por Golpes)\"\n\tconst tMatch = ruleText.match(/Regla\\s+\\d+\\s*[-\u2013]\\s*(.+?)(?:\\n|$)/);\n\tconst ruleTitle = tMatch ? tMatch[1].trim() : 'T\u00edtulo no encontrado';\n\n\treturn {\n\t\tjson: {\n\t\t\truleNumber,        // \"3\"\n\t\t\truleTitle,         // \"Stroke Play (Juego por Golpes)\"\n\t\t\tfullText: ruleText.trim(),\n\t\t\toriginalIndex: idx // 0-based\n\t\t},\n\t};\n});\n\n/** 5. Devolver el array de \u00edtems */\nreturn outputItems;\n"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        660,
        -380
      ],
      "id": "45032551-fd6b-4d4e-8f41-62e0107eb8ed",
      "name": "Code"
    },
    {
      "parameters": {
        "mode": "insert",
        "tableName": {
          "__rl": true,
          "value": "documents",
          "mode": "list",
          "cachedResultName": "documents"
        },
        "options": {
          "queryName": "match_documents"
        }
      },
      "type": "@n8n/n8n-nodes-langchain.vectorStoreSupabase",
      "typeVersion": 1.3,
      "position": [
        800,
        -380
      ],
      "id": "4747a478-baba-4a7d-89fa-954878bb252b",
      "name": "Upload to Supabase",
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "mode": "retrieve-as-tool",
        "toolDescription": "Use this tool to search the database",
        "tableName": {
          "__rl": true,
          "value": "documents",
          "mode": "list",
          "cachedResultName": "documents"
        },
        "topK": 20,
        "useReranker": true,
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.vectorStoreSupabase",
      "typeVersion": 1.3,
      "position": [
        320,
        -860
      ],
      "id": "68ec21f6-56fa-47ab-a763-df662b47ce4a",
      "name": "Supabase Vector Store",
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {},
      "type": "@n8n/n8n-nodes-langchain.rerankerCohere",
      "typeVersion": 1,
      "position": [
        1780,
        -720
      ],
      "id": "1da7bad2-878e-42dc-b11c-df22f63a1f97",
      "name": "Reranker Cohere1",
      "credentials": {
        "cohereApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "typeVersion": 1.2,
      "position": [
        1620,
        -720
      ],
      "id": "d17c5210-4179-4213-b64b-f774bd3907ed",
      "name": "Embeddings OpenAI2",
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "mode": "retrieve-as-tool",
        "toolDescription": "Use this tool to search the database",
        "tableName": {
          "__rl": true,
          "value": "documents",
          "mode": "list",
          "cachedResultName": "documents"
        },
        "topK": 20,
        "useReranker": true,
        "options": {
          "metadata": {
            "metadataValues": [
              {
                "name": "ruleNumber",
                "value": "={{ $('Metadata Agent').item.json.output }}"
              }
            ]
          }
        }
      },
      "type": "@n8n/n8n-nodes-langchain.vectorStoreSupabase",
      "typeVersion": 1.3,
      "position": [
        1620,
        -860
      ],
      "id": "f86e7e0b-791f-49af-bb9b-6a25e8c3c412",
      "name": "Supabase Vector Store1",
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "content": "# Vectorizar Documentos \u00ac Metadata\n(Para este ejemplo)",
        "height": 440,
        "width": 1000,
        "color": 2
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        160,
        -520
      ],
      "id": "85508042-db85-4eec-838f-27482666c5f7",
      "name": "Sticky Note"
    },
    {
      "parameters": {
        "content": "# Agente RAG\n",
        "height": 380,
        "width": 620,
        "color": 4
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -360,
        -960
      ],
      "id": "26eab455-f52e-4d59-a424-fe35b6185649",
      "name": "Sticky Note1"
    },
    {
      "parameters": {
        "content": "## Vector Store Tool & Reranker\n",
        "height": 380,
        "width": 380,
        "color": 5
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        260,
        -960
      ],
      "id": "db4d58c3-bb8c-48cc-807d-fd5393333d5c",
      "name": "Sticky Note2"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "typeVersion": 1.2,
      "position": [
        780,
        -240
      ],
      "id": "da3d27df-72de-47db-ad22-f43f10624595",
      "name": "Embeddings OpenAI1",
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "typeVersion": 1.2,
      "position": [
        320,
        -720
      ],
      "id": "eb3a2117-7888-4a0e-a076-682fe551db2f",
      "name": "Embeddings OpenAI",
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "options": {
          "systemMessage": "=# Overview\nYour job is to understand the rule number that the human is requesting and output only the number.\n\n## Example\nInput - what's rule number 27?\nOutput - 27"
        }
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 2,
      "position": [
        780,
        -860
      ],
      "id": "6476531d-7762-457e-90f8-75703e229488",
      "name": "Metadata Agent"
    },
    {
      "parameters": {
        "options": {
          "systemMessage": "=# Overview\nYou are an AI agent who is an expert at the rules of golf. You will receive a question from the human, and you must use your tool called \"Supabase Vector Store\" in order to retrieve information from the database to make sure you are answering the question accurately. "
        }
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 2,
      "position": [
        -120,
        -860
      ],
      "id": "3a708f5e-1e6f-4ec5-98e2-5280a4733c88",
      "name": "RAG Agent"
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $('When chat message received').item.json.chatInput }}",
        "options": {
          "systemMessage": "=# Overview\nYou are an AI agent who is an expert at the rules of golf. You will receive a question from the human, and you must use your tool called \"Supabase Vector Store\" in order to retrieve information from the database to make sure you are answering the question accurately. "
        }
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 2,
      "position": [
        1200,
        -860
      ],
      "id": "e1a32998-3bd1-4e65-91e9-e368c3212df4",
      "name": "RAG Agent 2"
    },
    {
      "parameters": {
        "content": "# Agente RAG\n",
        "height": 380,
        "width": 440,
        "color": 4
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1120,
        -960
      ],
      "id": "a4d590cd-321f-40d5-aea3-acccbc58c30a",
      "name": "Sticky Note3"
    },
    {
      "parameters": {
        "content": "## Vector Store Tool & Reranker Metadata\n",
        "height": 380,
        "width": 380,
        "color": 5
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1560,
        -960
      ],
      "id": "e05c9fec-ccef-4046-b30e-59ac4c6f48dd",
      "name": "Sticky Note4"
    },
    {
      "parameters": {
        "content": "# Agente Metadata\n",
        "height": 380,
        "width": 420,
        "color": 6
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        700,
        -960
      ],
      "id": "01e52d83-9aee-498a-b0ae-14397a003a4e",
      "name": "Sticky Note5"
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        240,
        -380
      ],
      "id": "c4647870-1c31-409d-b355-837647392a29",
      "name": "Manual Trigger"
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1-mini"
        },
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.2,
      "position": [
        -60,
        -720
      ],
      "id": "719114a7-7d7d-4103-8102-fa014b5272f2",
      "name": "OpenAI Chat Model",
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1-mini"
        },
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.2,
      "position": [
        840,
        -720
      ],
      "id": "5ac6fb67-7722-45f9-ac1c-65aae041ec20",
      "name": "OpenAI Chat Model1",
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1-mini"
        },
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.2,
      "position": [
        1260,
        -720
      ],
      "id": "05fb2e6c-e926-40b8-8b8e-9dca514dd5aa",
      "name": "OpenAI Chat Model2",
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {},
      "type": "@n8n/n8n-nodes-langchain.rerankerCohere",
      "typeVersion": 1,
      "position": [
        480,
        -720
      ],
      "id": "7623668f-8554-4702-8da0-798fc5165d3e",
      "name": "Reranker Cohere",
      "credentials": {
        "cohereApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "typeVersion": 1.1,
      "position": [
        -280,
        -860
      ],
      "id": "fec22e6d-4f3c-4183-b28e-899f26f8b708",
      "name": "When chat message received"
    }
  ],
  "connections": {
    "Download File": {
      "main": [
        [
          {
            "node": "Extract from File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Default Data Loader": {
      "ai_document": [
        [
          {
            "node": "Upload to Supabase",
            "type": "ai_document",
            "index": 0
          }
        ]
      ]
    },
    "Extract from File": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code": {
      "main": [
        [
          {
            "node": "Upload to Supabase",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Supabase Vector Store": {
      "ai_tool": [
        [
          {
            "node": "RAG Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Reranker Cohere1": {
      "ai_reranker": [
        [
          {
            "node": "Supabase Vector Store1",
            "type": "ai_reranker",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings OpenAI2": {
      "ai_embedding": [
        [
          {
            "node": "Supabase Vector Store1",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "Supabase Vector Store1": {
      "ai_tool": [
        [
          {
            "node": "RAG Agent 2",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings OpenAI1": {
      "ai_embedding": [
        [
          {
            "node": "Upload to Supabase",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings OpenAI": {
      "ai_embedding": [
        [
          {
            "node": "Supabase Vector Store",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "Metadata Agent": {
      "main": [
        [
          {
            "node": "RAG Agent 2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Download File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "RAG Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model1": {
      "ai_languageModel": [
        [
          {
            "node": "Metadata Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model2": {
      "ai_languageModel": [
        [
          {
            "node": "RAG Agent 2",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Reranker Cohere": {
      "ai_reranker": [
        [
          {
            "node": "Supabase Vector Store",
            "type": "ai_reranker",
            "index": 0
          }
        ]
      ]
    },
    "When chat message received": {
      "main": [
        [
          {
            "node": "RAG Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "0b5f14c0-32be-40c3-98f4-26487be67a4d",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "T0wqayb9Q79XWZrS",
  "tags": []
}