{
  "createdAt": "2025-02-02T23:10:53.666Z",
  "updatedAt": "2025-02-02T23:51:06.618Z",
  "id": "IFIaIqhBYGyynXk8",
  "name": "Siscad DB AI Memory Buffer",
  "active": false,
  "nodes": [
    {
      "parameters": {
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "typeVersion": 1.1,
      "position": [
        -1680,
        -440
      ],
      "id": "31cd2649-ff24-4da5-80b9-facbdcb179c5",
      "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": "13d647c3-5ea7-4723-98c3-cd158f187f60",
      "name": "Edit Fields"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "typeVersion": 1,
      "position": [
        -1200,
        -480
      ],
      "id": "735cbb81-eace-41d8-b1c7-5e80406e4164",
      "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": "a8cd36f9-4452-4d1d-ad42-8d5b2224dede",
      "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": "a0524ee4-74b7-47c4-8edd-2f5ca105a9ed",
      "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": "c8e6cacb-f008-4261-a909-a121b1e7d6d5",
      "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": "0a922c22-7ba6-4d09-b73c-d530d53921fd",
      "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": "f5266a90-27bd-47af-b526-4491bcc0212a",
      "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": "011b1b9a-4325-42bd-956d-3261efe1d6bd",
      "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": "3c689753-fc33-4fd2-a24a-e6d16f6a8e65",
      "name": "GENERATE QUERY TO EXTRACT DBS"
    },
    {
      "parameters": {
        "agent": "conversationalAgent",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 1.7,
      "position": [
        -1680,
        -960
      ],
      "id": "8830bc9d-8f19-4a6d-ba02-4c678f51d372",
      "name": "AI Agent"
    },
    {
      "parameters": {
        "modelName": "models/gemini-1.0-pro-001",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "typeVersion": 1,
      "position": [
        -1660,
        -740
      ],
      "id": "972735d1-e577-4ed3-8278-92ec76c5f365",
      "name": "Google Gemini Chat Model2",
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {},
      "type": "@n8n/n8n-nodes-langchain.memoryRedisChat",
      "typeVersion": 1.4,
      "position": [
        -1500,
        -740
      ],
      "id": "ca59e2e5-45e9-4625-b1ec-8c16d7041cb4",
      "name": "Redis Chat Memory",
      "credentials": {
        "redis": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "modelName": "models/gemini-1.0-pro-001",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "typeVersion": 1,
      "position": [
        240,
        -160
      ],
      "id": "bad432fa-a5e7-4e25-bd5b-ee27737fdf82",
      "name": "Google Gemini Chat Model3",
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=### UTILIZAR AS INFORMA\u00c7OES ABAIXO\n\n{{ JSON.stringify($('EXECUTE QUERY TO EXTRACT INFORMATION').all())}}",
        "messages": {
          "messageValues": [
            {
              "message": "FORMATAR AS INFORMA\u00c7\u00d5ES A SEGUIR DE MANEIRA APRESENTAVEL PARA O WHATSAPP BASEADO NA SOLICITACAO DO USUARIO."
            }
          ]
        }
      },
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "typeVersion": 1.5,
      "position": [
        140,
        -380
      ],
      "id": "2f4a8cb6-00f6-460a-9b66-3aaccb5b931e",
      "name": "FORMAT ANSWER"
    }
  ],
  "connections": {
    "When chat message received": {
      "main": [
        [
          {
            "node": "AI Agent",
            "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": [
        [
          {
            "node": "FORMAT ANSWER",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "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
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model2": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Redis Chat Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model3": {
      "ai_languageModel": [
        [
          {
            "node": "FORMAT ANSWER",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "FORMAT ANSWER": {
      "main": [
        []
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "versionId": "8dabf28e-d6d1-4b4e-a21c-3a1c0db01654",
  "triggerCount": 0,
  "tags": []
}