{
  "createdAt": "2025-02-02T15:48:49.139Z",
  "updatedAt": "2025-02-10T12:45:27.037Z",
  "id": "zrMOR7Bph0Qrt5Dl",
  "name": "Siscad DB AI",
  "active": false,
  "nodes": [
    {
      "parameters": {
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "typeVersion": 1.1,
      "position": [
        -1600,
        -440
      ],
      "id": "f9aaa3bc-d856-4f4d-9482-871ad97d249c",
      "name": "When chat message received"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "757c1e74-7caa-40f9-ae01-aa7838367742",
              "name": "tables",
              "value": "['v_members', 'v_plans', 'v_cities', 'v_regions']",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -1400,
        -680
      ],
      "id": "9f84bb57-da53-44ad-b298-d0142d48ef6b",
      "name": "Edit Fields"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "typeVersion": 1,
      "position": [
        -1200,
        -480
      ],
      "id": "4994d636-1500-4ba2-970b-b4111f37b935",
      "name": "Google Gemini Chat Model",
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "{{ $json.text }}",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        -860,
        -680
      ],
      "id": "b1b040b3-9055-463b-98e1-8f82fc3343f8",
      "name": "EXECUTE QUERY EXTRACT SCHEMA",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "return {\n  schema: JSON.stringify($('EXECUTE QUERY EXTRACT SCHEMA').all())\n}"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -640,
        -680
      ],
      "id": "98848bef-4b6b-48aa-a12e-74d60b06d764",
      "name": "PREPARE SCHEMA DBS"
    },
    {
      "parameters": {
        "modelName": "models/gemini-1.0-pro-001",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "typeVersion": 1,
      "position": [
        -420,
        -500
      ],
      "id": "c7bef772-6e8a-44de-8d51-600aa2d783de",
      "name": "Google Gemini Chat Model1",
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "{{ $json.result }}",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        160,
        -680
      ],
      "id": "b39e20ea-0c6a-4429-958a-3b4dd0a8ac19",
      "name": "EXECUTE QUERY TO EXTRACT INFORMATION",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const query =  $input.first().json.text // Certifique-se de que 'query' \u00e9 o nome do campo de entrada\nconst match = query.match(/```sql\\n([\\s\\S]*?)\\n```/);\nreturn [{ json: { result: match ? match[1].trim() : query.trim() } }];\n"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -60,
        -680
      ],
      "id": "fcc30638-db7b-4543-b849-9da3757bdf41",
      "name": "Code"
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=Dado as tabelas a seguir, preciso que voc\u00ea altere a query para utilizada para extrair o schema do banco somente para as tabelas informadas.\n\nTABELAS\n{{ $json.tables }}\n\n### QUERY A SER ALTERADA\nSELECT table_name, column_name, data_type  \nFROM information_schema.columns  \nWHERE table_schema = 'public'  \nAND table_name in ()  \nORDER BY table_name, ordinal_position;\n\n### O QUE FAZER\n1. RETORNAR APENAS A QUERY FINAL\n2. GARANTIR QUE O RETORNO VAI SER EXECUTADO PELO DB COM SUCESSO\n\n### O QUE N\u00c3O FAZER\n1. INCLUIR EXPLICA\u00c7\u00c3O OU OPINI\u00c3O\n2. N\u00c3O INCLUIR EM BLOCO DE C\u00d3DIGO COMO POR EXEMPLO \"```sql\"\n"
      },
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "typeVersion": 1.5,
      "position": [
        -1220,
        -680
      ],
      "id": "1a1e6340-82fe-40cb-8c91-8c6436db7db8",
      "name": "EXTRACT SCHEMA DBS"
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $('When chat message received').last().json.chatInput }}",
        "messages": {
          "messageValues": [
            {
              "message": "=Voc\u00ea agora \u00e9 um DBA experiente em montar queries para extrair as informa\u00e7\u00f5es do banco solicitadas pelo usu\u00e1rio.\n\nPreciso que voc\u00ea analise a solicita\u00e7\u00e3o do usu\u00e1rio e utilizando o schema predefinido elabore a query para extrair as informa\u00e7\u00f5es desejadas.\n\n### SCHEMA DISPON\u00cdVEL\n{{ $json.schema }}\n\n### O QUE VOC\u00ca DEVE FAZER\n1. RETORNAR APENAS A QUERY FINAL;\n2. GARANTIR QUE O RETORNO VAI SER EXECUTADO PELO DB COM SUCESSO;\n3. SEMPRE UTILIZE ILIKE PARA OS CAMPOS VARCHAR, CHAR, STRING, TEXT;\n4. Remova TODOS e quaisquer caracteres antes e depois da query, deixando apenas o c\u00f3digo SQL puro como no exemplo abaixo:\n\"SELECT COUNT(*) AS total FROM table_name;\"\n\n### CONSIDERAC\u00d4ES PARA A CRIACAO DE UMA SQL CORRETA:\n1. SITUACAO DOS SOCIOS:\n  1.1. S\u00f3cios ativos equivale status_id = '2';\n  1.2. S\u00f3cios inativos equivale status_id = '1';\n\n2. INFORMACOES:\n  2.1 TODAS AS INFORMACOES ESTAO CONTIDAS NA TABELA INFORMADA;\n\n3. PLANOS DOS SOCIOS:\n  3.1 Plano especial equivale plan_id = '1';\n  3.2 Plano normal equivale plan_id = '2';\n  3.3 Plano inss equivale plan_id = '3';\n  3.4 Plano contribuinte equivale plan_id = '4';\n  3.5 Plano ipe equivale plan_id = '5';\n\n4. RELACAO ENTRE TABELAS:\n  4.1 Utilizar os nomes dos campos originais das tabelas quando possivel;\n  4.2 NAO DEVERA SER REALIZADA NENHUM RELACIONAMENTO COM OUTRAS TABELAS;\n"
            }
          ]
        }
      },
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "typeVersion": 1.5,
      "position": [
        -440,
        -680
      ],
      "id": "a65584b8-1a69-44ff-a300-305912092f4d",
      "name": "GENERATE QUERY TO EXTRACT DBS"
    }
  ],
  "connections": {
    "When chat message received": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Fields": {
      "main": [
        [
          {
            "node": "EXTRACT SCHEMA DBS",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "EXTRACT SCHEMA DBS",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "EXECUTE QUERY EXTRACT SCHEMA": {
      "main": [
        [
          {
            "node": "PREPARE SCHEMA DBS",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "PREPARE SCHEMA DBS": {
      "main": [
        [
          {
            "node": "GENERATE QUERY TO EXTRACT DBS",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model1": {
      "ai_languageModel": [
        [
          {
            "node": "GENERATE QUERY TO EXTRACT DBS",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Code": {
      "main": [
        [
          {
            "node": "EXECUTE QUERY TO EXTRACT INFORMATION",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "EXECUTE QUERY TO EXTRACT INFORMATION": {
      "main": [
        []
      ]
    },
    "EXTRACT SCHEMA DBS": {
      "main": [
        [
          {
            "node": "EXECUTE QUERY EXTRACT SCHEMA",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GENERATE QUERY TO EXTRACT DBS": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "versionId": "bcb0b7df-d0bc-4d86-ad7a-9ae39de5c605",
  "triggerCount": 0,
  "tags": []
}