{
  "name": "Carga Documentos",
  "nodes": [
    {
      "parameters": {
        "operation": "download",
        "fileId": {
          "__rl": true,
          "value": "={{ $json.file_id }}",
          "mode": "id"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        740,
        -480
      ],
      "id": "6703a6b3-b1b8-44d7-beec-f363df2feeb1",
      "name": "Google Drive",
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "pdf",
        "options": {}
      },
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1,
      "position": [
        980,
        -500
      ],
      "id": "9244f085-7bdd-4b19-a914-3397b518c359",
      "name": "Extract from File"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "05b1870d-6565-4112-b463-8aa8ef23b680",
              "name": "file_id",
              "value": "={{ $json.id }}",
              "type": "string"
            },
            {
              "id": "b4ca517f-ee09-46c5-8ebe-63e9634b3a34",
              "name": "name",
              "value": "={{ $json.name }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        480,
        -480
      ],
      "id": "77dadb46-e444-41ae-986d-dc65d65ef616",
      "name": "Edit Fields"
    },
    {
      "parameters": {
        "modelId": {
          "__rl": true,
          "value": "gpt-4o-mini",
          "mode": "list",
          "cachedResultName": "GPT-4O-MINI"
        },
        "messages": {
          "values": [
            {
              "content": "=Te voy a pasar el nombre de un documento y tu objetivo es dar una de las siguientes salidas con el formato que te describo.\n\nEl nombre del documento es el siguiente: {{ $('Edit Fields').item.json.name }}\n\nDebes clasificar con una de las siguientes opciones que te describo a continuaci\u00f3n:\n\nley_enjuiciamiento_criminal: si el nombre del documento hace referencia a la ley de enjuiciamiento criminal.\ncodigo_penal: si el nombre del documento hace referencia al codigo penal.\nley_arrendamientos_urbanos: si el nombre del documento hace referencia a la ley de arrendamientos urbanos.\ncodigo_civil: si el nombre del documento hace referencia al codigo civil.\nley_enjuiciamento_civil: si el nombre del documento hace referencia a la ley de enjuiciamento civil.\nley_concursal: si el nombre del documento hace referencia a la ley concursal o al texto refundido de la ley concursal.\nley_ordenacion_edificacion: si el nombre del documento hace referencia a la ley de ordenacion de edificacion.\nley_juridiccion_voluntaria: si el nombre del documento hace referencia a la ley de juridiccion voluntaria.\nmateria_eficiencia_servicio_publico_justicia: si el nombre del documento hace referencia a materia de eficiencia del servicio p\u00fablico de justicia.\n\nTu salida siempre debe ser una de las opciones anteriores. Si no coincide, responde una salida vac\u00eda.\n\n\n"
            }
          ]
        },
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.8,
      "position": [
        1220,
        -600
      ],
      "id": "71ba3399-5a7c-4721-add9-618dbb12b912",
      "name": "OpenAI",
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "a0ba821d-654b-4e48-b834-22e1607d2611",
              "leftValue": "={{ $json.message.content }}",
              "rightValue": "ley_enjuiciamiento_criminal",
              "operator": {
                "type": "string",
                "operation": "equals",
                "name": "filter.operator.equals"
              }
            },
            {
              "id": "8a084dea-323a-415f-8fd3-705d6d94cad5",
              "leftValue": "={{ $json.message.content }}",
              "rightValue": "codigo_penal",
              "operator": {
                "type": "string",
                "operation": "equals",
                "name": "filter.operator.equals"
              }
            },
            {
              "id": "58f2bb7e-480c-4bd8-81e0-79764cdcbeb5",
              "leftValue": "={{ $json.message.content }}",
              "rightValue": "ley_arrendamientos_urbanos",
              "operator": {
                "type": "string",
                "operation": "equals",
                "name": "filter.operator.equals"
              }
            },
            {
              "id": "d0dd8f91-e79f-4bb1-9b42-37a4b9056f14",
              "leftValue": "={{ $json.message.content }}",
              "rightValue": "codigo_civil",
              "operator": {
                "type": "string",
                "operation": "equals",
                "name": "filter.operator.equals"
              }
            },
            {
              "id": "edf02c9d-694f-4d3f-8318-cd088714d41b",
              "leftValue": "={{ $json.message.content }}",
              "rightValue": "ley_enjuiciamento_civil",
              "operator": {
                "type": "string",
                "operation": "equals",
                "name": "filter.operator.equals"
              }
            },
            {
              "id": "bfebd1f4-2c33-478b-a3dd-d8fb38590f89",
              "leftValue": "={{ $json.message.content }}",
              "rightValue": "ley_concursal",
              "operator": {
                "type": "string",
                "operation": "equals",
                "name": "filter.operator.equals"
              }
            },
            {
              "id": "39ee704d-f107-47fa-91fc-e417f5940a23",
              "leftValue": "={{ $json.message.content }}",
              "rightValue": "ley_ordenacion_edificacion",
              "operator": {
                "type": "string",
                "operation": "equals",
                "name": "filter.operator.equals"
              }
            },
            {
              "id": "50c6da33-9e03-469b-8827-a09d96012c87",
              "leftValue": "={{ $json.message.content }}",
              "rightValue": "ley_juridiccion_voluntaria",
              "operator": {
                "type": "string",
                "operation": "equals",
                "name": "filter.operator.equals"
              }
            },
            {
              "id": "8929ffb9-0deb-4bfc-b7b8-697c38e1e7b4",
              "leftValue": "={{ $json.message.content }}",
              "rightValue": "materia_eficiencia_servicio_publico_justicia",
              "operator": {
                "type": "string",
                "operation": "equals",
                "name": "filter.operator.equals"
              }
            }
          ],
          "combinator": "or"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1560,
        -480
      ],
      "id": "d64dfa6d-3077-49e4-a3a2-3f932f6b9e44",
      "name": "If"
    },
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "triggerOn": "specificFolder",
        "folderToWatch": {
          "__rl": true,
          "value": "1ZjDPs93DTbgcN6hipMGafP13UwgeTN5U",
          "mode": "list",
          "cachedResultName": "RAG",
          "cachedResultUrl": "https://drive.google.com/drive/folders/1ZjDPs93DTbgcN6hipMGafP13UwgeTN5U"
        },
        "event": "fileCreated",
        "options": {
          "fileType": "all"
        }
      },
      "type": "n8n-nodes-base.googleDriveTrigger",
      "typeVersion": 1,
      "position": [
        120,
        -540
      ],
      "id": "cac78163-0431-4520-a25a-24181e48dc36",
      "name": "Google Drive Trigger",
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "triggerOn": "specificFolder",
        "folderToWatch": {
          "__rl": true,
          "value": "1ZjDPs93DTbgcN6hipMGafP13UwgeTN5U",
          "mode": "list",
          "cachedResultName": "RAG",
          "cachedResultUrl": "https://drive.google.com/drive/folders/1ZjDPs93DTbgcN6hipMGafP13UwgeTN5U"
        },
        "event": "fileUpdated",
        "options": {}
      },
      "type": "n8n-nodes-base.googleDriveTrigger",
      "typeVersion": 1,
      "position": [
        120,
        -360
      ],
      "id": "070351f7-8d4b-4c67-b161-b7f4861cf87c",
      "name": "Google Drive Trigger1",
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "model": "text-embedding-3-large",
        "options": {}
      },
      "id": "27fe7a45-894a-401a-99e5-394d3fba0333",
      "name": "text-embeddings-3-large",
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "position": [
        2440,
        500
      ],
      "typeVersion": 1,
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "text": "={{ $('Extract from File').item.json.text }}",
        "attributes": {
          "attributes": [
            {
              "name": "overarching_theme",
              "description": "Resume el tema o temas principales tratados en la secci\u00f3n \"Tema general\"."
            },
            {
              "name": "recurring_topics",
              "description": "Enumera los temas recurrentes mencionados en la secci\u00f3n \"Temas comunes\" como un array de String."
            },
            {
              "name": "pain_points",
              "description": "Resume las frustraciones o los retos del usuario mencionados en la secci\u00f3n \"Puntos de dolor\" como una serie de arrays de String."
            },
            {
              "name": "analytical_insights",
              "description": "Extrae una lista de observaciones anal\u00edticas clave de la secci\u00f3n Perspectivas anal\u00edticas, incluidos los cambios de tono o comportamiento."
            },
            {
              "name": "conclusion",
              "description": "Resume las conclusiones extra\u00eddas sobre los hilos del usuario y su enfoque general."
            },
            {
              "name": "keywords",
              "description": "Genere una lista de 10 palabras clave que capten la esencia del documento (por ejemplo, \"codigoPenal\", \"codigoCivil\")."
            }
          ]
        },
        "options": {
          "systemPromptTemplate": "Eres un experto en algoritmos de extracci\u00f3n.\nExtrae del texto s\u00f3lo la informaci\u00f3n relevante.\nSi no conoces el valor de un atributo que se te pide extraer, puedes omitir el valor del atributo."
        }
      },
      "id": "c1c7a60e-1b84-4dae-a70c-6468f45f7628",
      "name": "Extract Meta Data",
      "type": "@n8n/n8n-nodes-langchain.informationExtractor",
      "position": [
        2320,
        -500
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "mode": "combine",
        "combineBy": "combineAll",
        "options": {}
      },
      "id": "b427e5e1-8a13-40e6-afb6-6d0a1f24b824",
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "position": [
        2420,
        20
      ],
      "typeVersion": 3
    },
    {
      "parameters": {
        "modelName": "models/gemini-2.0-flash-exp",
        "options": {
          "temperature": 0.4
        }
      },
      "id": "b8154c29-9538-49af-979b-3dec2cd33cc2",
      "name": "Google Gemini Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        2140,
        -300
      ],
      "typeVersion": 1,
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chunkSize": 2000,
        "chunkOverlap": 400
      },
      "id": "5888ab01-4f24-4ae3-a96c-3a71cfe01b8b",
      "name": "Token Splitter",
      "type": "@n8n/n8n-nodes-langchain.textSplitterTokenSplitter",
      "position": [
        2760,
        660
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "options": {
          "metadata": {
            "metadataValues": [
              {
                "name": "tipo",
                "value": "={{ $('If').item.json.message.content }}"
              },
              {
                "name": "=overarching_theme",
                "value": "={{ $('Extract Meta Data').item.json.output.overarching_theme }}"
              },
              {
                "name": "recurring_topics",
                "value": "={{ $('Extract Meta Data').item.json.output.recurring_topics }}"
              },
              {
                "name": "pain_points",
                "value": "={{ $('Extract Meta Data').item.json.output.pain_points }}"
              },
              {
                "name": "analytical_insights",
                "value": "={{ $('Extract Meta Data').item.json.output.analytical_insights }}"
              },
              {
                "name": "conclusion",
                "value": "={{ $('Extract Meta Data').item.json.output.conclusion }}"
              },
              {
                "name": "keywords",
                "value": "={{ $('Extract Meta Data').item.json.output.keywords }}"
              }
            ]
          }
        }
      },
      "id": "2348a793-b250-4f98-a666-5629d699994a",
      "name": "Data Loader",
      "type": "@n8n/n8n-nodes-langchain.documentDefaultDataLoader",
      "position": [
        2660,
        520
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "subject": "Carga de Documentos",
        "emailType": "text",
        "message": "=El documento cargado no es v\u00e1lido para introducirlo en la base de conocimiento.\n\nPor favor, suba un documento v\u00e1lido o revise si el nombre del documento es correcto.",
        "options": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        1600,
        420
      ],
      "id": "b92a6815-c249-49ce-8ddf-0cc2e3fa0d11",
      "name": "Documento no v\u00e1lido",
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "subject": "Carga de Documentos",
        "emailType": "text",
        "message": "=El/los documento/s han sido cargados correctamente.",
        "options": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        3140,
        140
      ],
      "id": "cd6eb813-3d96-41b0-b8d8-064e1d32e27f",
      "name": "Correcto",
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "subject": "Carga de Documentos",
        "emailType": "text",
        "message": "=Hubo un error al cargar el documento. Por favor, int\u00e9ntelo de nuevo.",
        "options": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        3220,
        360
      ],
      "id": "3082c6c2-0cc8-40b3-857e-d47092639a66",
      "name": "Error",
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "mode": "insert",
        "qdrantCollection": {
          "__rl": true,
          "value": "=documentos",
          "mode": "id"
        },
        "options": {}
      },
      "id": "36935655-dbc3-4cd5-b241-95d969f610cc",
      "name": "Qdrant Vector Store",
      "type": "@n8n/n8n-nodes-langchain.vectorStoreQdrant",
      "position": [
        2560,
        280
      ],
      "executeOnce": false,
      "retryOnFail": false,
      "typeVersion": 1,
      "credentials": {
        "qdrantApi": {
          "name": "<your credential>"
        }
      },
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "aggregate": "aggregateAllItemData",
        "options": {}
      },
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [
        2920,
        180
      ],
      "id": "0917a21e-eafa-4d7e-9698-35a2aca8e438",
      "name": "Aggregate"
    },
    {
      "parameters": {
        "aggregate": "aggregateAllItemData",
        "options": {}
      },
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [
        2980,
        380
      ],
      "id": "cd6fd715-651a-4759-8838-063231d7f9f1",
      "name": "Aggregate1"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://93097718-55f0-4f60-a5e0-a67694f0438b.europe-west3-0.gcp.cloud.qdrant.io:6333/collections/documentos/points/delete",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "api-key",
              "value": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhY2Nlc3MiOiJtIn0.63G2hMlr71E9FmzPqx_11NyGQXFcHkhhQtTcsohsuLQ"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"filter\": {\n    \"must\": [\n      {\n        \"key\": \"metadata.tipo\",\n        \"match\": { \"value\": \"{{ $json.message.content }}\" }\n      }\n    ]\n  }\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1920,
        -500
      ],
      "id": "0712b952-52b6-483f-acda-9cbbe89ae16e",
      "name": "Eliminar documentos",
      "onError": "continueRegularOutput"
    }
  ],
  "connections": {
    "Google Drive": {
      "main": [
        [
          {
            "node": "Extract from File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract from File": {
      "main": [
        [
          {
            "node": "OpenAI",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Edit Fields": {
      "main": [
        [
          {
            "node": "Google Drive",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "Eliminar documentos",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Documento no v\u00e1lido",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Drive Trigger": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Drive Trigger1": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "text-embeddings-3-large": {
      "ai_embedding": [
        [
          {
            "node": "Qdrant Vector Store",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "Extract Meta Data": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Qdrant Vector Store",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Extract Meta Data",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Token Splitter": {
      "ai_textSplitter": [
        [
          {
            "node": "Data Loader",
            "type": "ai_textSplitter",
            "index": 0
          }
        ]
      ]
    },
    "Data Loader": {
      "ai_document": [
        [
          {
            "node": "Qdrant Vector Store",
            "type": "ai_document",
            "index": 0
          }
        ]
      ]
    },
    "Qdrant Vector Store": {
      "main": [
        [
          {
            "node": "Aggregate",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Aggregate1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate": {
      "main": [
        [
          {
            "node": "Correcto",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate1": {
      "main": [
        [
          {
            "node": "Error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Eliminar documentos": {
      "main": [
        [
          {
            "node": "Extract Meta Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "timezone": "Europe/Madrid",
    "callerPolicy": "workflowsFromSameOwner",
    "errorWorkflow": "nAWcsFqEIMIvT733"
  },
  "versionId": "a3e7b9c8-d1ef-4372-ae85-e8be3b1a683b",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "Wq7mLRIBpdk2DtHU",
  "tags": []
}