isra36

Avanzado

Este es unEngineering, AI Chatbotflujo de automatización del dominio deautomatización que contiene 40 nodos.Utiliza principalmente nodos como If, Set, Code, Merge, Postgres. Generar y probar código SQL con un sandbox de GPT/OpenRouter AI y PostgreSQL

Requisitos previos
  • Información de conexión de la base de datos PostgreSQL
  • Clave de API de OpenAI
Vista previa del flujo de trabajo
Visualización de las conexiones entre nodos, con soporte para zoom y panorámica
Exportar flujo de trabajo
Copie la siguiente configuración JSON en n8n para importar y usar este flujo de trabajo
{
  "id": "H6iWWu9KK0XoaPXa",
  "meta": {
    "instanceId": "90a1d1cf1e9e6d9ee6e8c37df44d53777d9bc4425de96a5a0f770320161d5171"
  },
  "name": "isra36",
  "tags": [],
  "nodes": [
    {
      "id": "47f2b3b4-12f0-4e9c-82f4-54d2c32b77f0",
      "name": "Al recibir mensaje de chat",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        -1820,
        120
      ],
      "webhookId": "a889d2ae-2159-402f-b326-5f61e90f602e",
      "parameters": {
        "mode": "webhook",
        "public": true,
        "options": {}
      },
      "typeVersion": 1.1
    },
    {
      "id": "424b2831-425b-4ee7-a3d5-b2713bfdf343",
      "name": "GenerateErrorPrompt",
      "type": "n8n-nodes-base.code",
      "position": [
        4140,
        460
      ],
      "parameters": {
        "jsCode": "\n\nreturn {\n  \"message\" : $input.last().json.message,\n  \"prompt\" : \"Recieved error while executing this query: \" + $input.last().json.message + \". Here detailed description: \" + $input.last().json.error.description + \". Please fix SQL query. Please decide how to solve and give fixed SQL query.\"\n}"
      },
      "typeVersion": 2
    },
    {
      "id": "07f2f95a-0958-4562-b5c4-1bb6504a02fd",
      "name": "Simple Memoria",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        1040,
        400
      ],
      "parameters": {
        "sessionKey": "={{ $('localVariables').last().json.sessionId }}",
        "sessionIdType": "customKey",
        "contextWindowLength": 7
      },
      "typeVersion": 1.3
    },
    {
      "id": "7f42bf3b-5d99-41d4-94e7-0a34da39e285",
      "name": "AutoErrorFixing",
      "type": "n8n-nodes-base.if",
      "position": [
        3080,
        -40
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "e938b602-e816-4409-9c0b-190eae7952df",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              },
              "leftValue": "={{ $('localVariables').last().json.autoErrorFixing }}",
              "rightValue": 1
            }
          ]
        },
        "looseTypeValidation": true
      },
      "typeVersion": 2.2
    },
    {
      "id": "6e1fcee6-2c63-46e3-a48c-458180257b43",
      "name": "IfError",
      "type": "n8n-nodes-base.if",
      "position": [
        2860,
        20
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "3228bc02-986c-4159-bf24-b27336611473",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $('GenerateErrorPrompt').isExecuted }}",
              "rightValue": "true"
            }
          ]
        },
        "looseTypeValidation": true
      },
      "typeVersion": 2.2
    },
    {
      "id": "4f3fcdf7-fe96-4137-9356-165890ea57d1",
      "name": "Execute_AI_result",
      "type": "n8n-nodes-base.postgres",
      "onError": "continueErrorOutput",
      "position": [
        3620,
        160
      ],
      "parameters": {
        "query": "{{ $json.output }}",
        "options": {},
        "operation": "executeQuery"
      },
      "credentials": {
        "postgres": {
          "id": "OunYREGTB5g2u3LA",
          "name": "Postgres account 2"
        }
      },
      "typeVersion": 2.6
    },
    {
      "id": "d4c60f3d-425e-4fef-923c-a2fa154f0a00",
      "name": "isAssistantExists",
      "type": "n8n-nodes-base.if",
      "position": [
        100,
        -460
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "cd96c88f-b4c7-4bb6-9082-b2a827740bea",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.output }}",
              "rightValue": "1"
            }
          ]
        },
        "looseTypeValidation": true
      },
      "typeVersion": 2.2
    },
    {
      "id": "318a17bb-7566-4b2c-bd3f-d6e57daa1b95",
      "name": "isAssistantExistsCódigo",
      "type": "n8n-nodes-base.code",
      "position": [
        -200,
        -460
      ],
      "parameters": {
        "jsCode": "let isAssistantExists = 0;\nlet assistantId = null;\n\nfor (const item of $('getAssistantsList').all()) {\n  let trimmedGetAss = item.json.name ? item.json.name.trim() : '';\n  let trimmedAgentName = $(\"AgentName\").last().json.agentName.trim();\n  if (trimmedGetAss === trimmedAgentName) { \n    isAssistantExists = 1;\n    assistantId = $input.first().json.id;\n  }\n}\n\nreturn {output: isAssistantExists, id: assistantId};"
      },
      "typeVersion": 2
    },
    {
      "id": "ff979107-3937-4feb-aaf3-4ff9337325da",
      "name": "isOpenAI",
      "type": "n8n-nodes-base.if",
      "position": [
        4360,
        460
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "fddc112b-8a31-4098-b283-df9d0c7b0acf",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $('localVariables').last().json.aiProvider }}",
              "rightValue": "openai"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "6e2a2dd7-8ca7-4662-9c7d-5c7cab0dd314",
      "name": "assistant",
      "type": "n8n-nodes-base.set",
      "position": [
        580,
        -480
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "1f6867fd-d7d1-45c5-b01f-960e4ce6c883",
              "name": "id",
              "type": "string",
              "value": "={{ $json.id }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "af7314ea-60db-4873-838d-7a01dffc70ed",
      "name": "IfOpenAI",
      "type": "n8n-nodes-base.if",
      "position": [
        -1180,
        120
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "fddc112b-8a31-4098-b283-df9d0c7b0acf",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.aiProvider }}",
              "rightValue": "openai"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "db247d1d-8277-475b-98c8-4150326e0dfb",
      "name": "Nota adhesiva",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1540,
        20
      ],
      "parameters": {
        "height": 260,
        "content": "use this to get neccessary local variables, like: instruction to AI, sessionId, and all inputed parameters from previous node\n"
      },
      "typeVersion": 1
    },
    {
      "id": "02dbf130-9e82-4d85-b743-2cb488fb0f46",
      "name": "Nota adhesiva2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1900,
        -180
      ],
      "parameters": {
        "color": 4,
        "width": 260,
        "height": 460,
        "content": "Input parameters:\n1. sessionId: uuidv4\n2. threadId: nullable\n3. apiKey: string\n4. aiProvider: string\n5. model: string\n6. autoErrorFixing: boolean\n7. chatInput: string (users prompt)\n8. currentDbSchemaWithData: string (json architecture with data)"
      },
      "typeVersion": 1
    },
    {
      "id": "e10c42db-8c9f-4052-8205-259b53ebdc3f",
      "name": "IsMaxAutoErrorReached",
      "type": "n8n-nodes-base.if",
      "position": [
        3340,
        -320
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "e35fea46-5373-427b-b3fa-6fab56627bde",
              "operator": {
                "type": "number",
                "operation": "gte"
              },
              "leftValue": "={{ $node['GenerateErrorPrompt'].runIndex }}",
              "rightValue": 4
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "1a3189fd-550e-4b1c-8720-65579fa39c44",
      "name": "OpenRouter Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenRouter",
      "position": [
        900,
        400
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 1
    },
    {
      "id": "20960f49-d573-45e0-a3e0-f5e181a8ef66",
      "name": "AgenteName",
      "type": "n8n-nodes-base.set",
      "position": [
        -900,
        -460
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "527cbbeb-e3a7-4fb5-aaae-fd2b8085de85",
              "name": "agentName",
              "type": "string",
              "value": "={{ 'AiDoubleCheck_' + $('localVariables').last().json.model }}"
            }
          ]
        },
        "includeOtherFields": true
      },
      "typeVersion": 3.4
    },
    {
      "id": "9f28f786-c999-4614-8d74-e67f945583e2",
      "name": "setOutputByProvider",
      "type": "n8n-nodes-base.set",
      "position": [
        2480,
        -200
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "569f9268-5b51-4fac-9d8b-132de0b77ef0",
              "name": "output",
              "type": "string",
              "value": "={{ \n$if ( $('localVariables').last().json.aiProvider === 'openai', \n  $('OpenAIMainBrain').last().json.output,\n  $('OpenRouterAgent').last().json.output\n) \n}}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "c1c4bc98-9f7f-459b-9c1e-8ce1de98f24c",
      "name": "OpenAIMainBrain",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        820,
        -480
      ],
      "parameters": {
        "text": "={{ \n\n  $if($('GenerateErrorPrompt').isExecuted, \n\n    `## ERROR HANDLING MODE\\n\\n  ${$json.prompt}.`,\n\n    $if($('localVariables').last().json.currentDbSchemaWithData !== '[]', 'Current DB tables: ' + $('localVariables').last().json.currentDbSchemaWithData + '; ',\n'') + $('localVariables').last().json.chatInput + '. Prefix for tables: ' + $('localVariables').last().json.sessionId) \n\n}}",
        "memory": "threadId",
        "prompt": "define",
        "options": {},
        "resource": "assistant",
        "threadId": "={{ $ifEmpty($('localVariables').last().json.threadId, null) }}",
        "assistantId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $('assistant').last().json.id }}"
        }
      },
      "credentials": {
        "openAiApi": {
          "id": "YYAgs4Xc0ZRuGvPa",
          "name": "OpenAi account"
        }
      },
      "typeVersion": 1.8
    },
    {
      "id": "723fd28e-628e-417a-826c-2fd025a8a026",
      "name": "askUserHowToHandleError",
      "type": "n8n-nodes-base.set",
      "position": [
        3320,
        -20
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "562ef9d0-d7c1-43f3-8ceb-608b6576f4e2",
              "name": "message",
              "type": "string",
              "value": "={{ $json.output }}"
            },
            {
              "id": "8ede156c-df3f-4da3-87b2-696457147762",
              "name": "type",
              "type": "string",
              "value": "autoErrorFixingFalse"
            },
            {
              "id": "ed5a54d3-29b1-4034-9a1b-595831f25585",
              "name": "error",
              "type": "string",
              "value": "={{ $('Execute_AI_result').last().json.message }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "0e4af952-6e07-4cb0-a4ff-6705ed3bbfc0",
      "name": "maxAutoErrorLimitarReached",
      "type": "n8n-nodes-base.set",
      "position": [
        3640,
        -340
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "562ef9d0-d7c1-43f3-8ceb-608b6576f4e2",
              "name": "output",
              "type": "string",
              "value": "={{ $json.output }}"
            },
            {
              "id": "b9ddb8b4-56ab-4d7e-b3fa-2a72c5158d26",
              "name": "type",
              "type": "string",
              "value": "maxAutoErrorLimitReached"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "43a23e59-31f7-40fb-b911-9e3dd9bad912",
      "name": "If",
      "type": "n8n-nodes-base.if",
      "position": [
        -440,
        -460
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "01f2a48f-2018-483c-a667-3c184ca9b169",
              "operator": {
                "type": "string",
                "operation": "notExists",
                "singleValue": true
              },
              "leftValue": "={{ $json.error }}",
              "rightValue": "error"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "817ec035-7af7-41dd-b9f6-f51fff4b6c1d",
      "name": "wordsForUser1",
      "type": "n8n-nodes-base.set",
      "position": [
        1660,
        20
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "30b2c62f-e7cf-4b8e-85ad-e0d6e4ae5094",
              "name": "type",
              "type": "string",
              "value": "wordsForUser"
            },
            {
              "id": "cca814df-e535-46c6-bd6e-aa780e8cf12e",
              "name": "message",
              "type": "string",
              "value": "={{ $json.output }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "1347b643-7109-4148-86bd-89b540861b06",
      "name": "isExecutable",
      "type": "n8n-nodes-base.if",
      "position": [
        1340,
        -180
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "e52803c5-a6e2-4281-912f-a8c58867b9a8",
              "operator": {
                "type": "string",
                "operation": "notContains"
              },
              "leftValue": "={{ $json.output }}",
              "rightValue": "words_for_user"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "73bc7418-b260-4c38-bc15-ec3ed50086df",
      "name": "getAssistantsList",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "onError": "continueRegularOutput",
      "position": [
        -660,
        -460
      ],
      "parameters": {
        "resource": "assistant",
        "operation": "list"
      },
      "credentials": {
        "openAiApi": {
          "id": "YYAgs4Xc0ZRuGvPa",
          "name": "OpenAi account"
        }
      },
      "typeVersion": 1.8,
      "alwaysOutputData": true
    },
    {
      "id": "84f8b40b-c563-4b28-a70c-c4c5b7323d1b",
      "name": "Nota adhesiva1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1260,
        20
      ],
      "parameters": {
        "color": 5,
        "height": 260,
        "content": "OpenAI has built-in assistant that handles chat history on their side. For open-router we should handle chat history on our side"
      },
      "typeVersion": 1
    },
    {
      "id": "efca8f7c-1115-4086-9b53-111574a00cee",
      "name": "Nota adhesiva3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1280,
        -300
      ],
      "parameters": {
        "height": 260,
        "content": "Sometimes we can't answer with just code. This node is responsible for separation. If it is not a code for sandbox, than it will go to user as words."
      },
      "typeVersion": 1
    },
    {
      "id": "a793ec0d-97fc-49aa-8c2a-f3746946ed95",
      "name": "Nota adhesiva4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        3280,
        -420
      ],
      "parameters": {
        "height": 260,
        "content": "Error fixing loop will work only n times, defined in this node. It is done to prevent infinite loop"
      },
      "typeVersion": 1
    },
    {
      "id": "d12ff01c-662d-4204-a1a7-1c91b122a5cd",
      "name": "Nota adhesiva5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        3020,
        -180
      ],
      "parameters": {
        "color": 2,
        "width": 220,
        "height": 280,
        "content": "If the user has selected automatic error fixing, debugging will be performed automatically, otherwise the system will ask the user for further instruction"
      },
      "typeVersion": 1
    },
    {
      "id": "d004ab56-4a20-4a9d-ae2b-c899421af2af",
      "name": "get_all_tables",
      "type": "n8n-nodes-base.postgres",
      "disabled": true,
      "position": [
        3720,
        1300
      ],
      "parameters": {
        "query": "SELECT tablename\nFROM pg_catalog.pg_tables\nWHERE schemaname = 'public';",
        "options": {},
        "operation": "executeQuery"
      },
      "credentials": {
        "postgres": {
          "id": "OunYREGTB5g2u3LA",
          "name": "Postgres account 2"
        }
      },
      "typeVersion": 2.6
    },
    {
      "id": "479c3308-8f3a-420a-8d2d-206ce6acb10b",
      "name": "Fusionar",
      "type": "n8n-nodes-base.merge",
      "disabled": true,
      "position": [
        4780,
        1160
      ],
      "parameters": {},
      "typeVersion": 3.1
    },
    {
      "id": "a9832d33-f46e-4273-b7b3-0019bd33cc93",
      "name": "Extract Solution From Entire Query",
      "type": "n8n-nodes-base.code",
      "disabled": true,
      "position": [
        4400,
        980
      ],
      "parameters": {
        "jsCode": "const agentOutput = $(\"OpenRouterAgent\").all()[0]?.json?.output;\nconst commands = agentOutput.split(\"\\n\\n\");\nconst filteredCommands = commands.filter(\n  (command) =>\n    !command.includes(\"CREATE TABLE\") &&\n    !command.includes(\"INSERT INTO\"),\n);\nconst result = filteredCommands.join(\"\\n\\n\");\n\nreturn { result };\n"
      },
      "typeVersion": 2
    },
    {
      "id": "def0dfb5-e863-4712-83bd-e5d00599ac4d",
      "name": "Add TableName ForEach",
      "type": "n8n-nodes-base.code",
      "disabled": true,
      "position": [
        4600,
        1540
      ],
      "parameters": {
        "jsCode": "for (const item of $input.all()) {\n  item.json.tableName = $('Loop Over Items').first().json.Tables_in_sql5776769;\n}\n\nreturn $input.all();"
      },
      "typeVersion": 2
    },
    {
      "id": "d0ef4902-a0dc-4ad4-a970-db60e4716935",
      "name": "Transform for HTML table",
      "type": "n8n-nodes-base.code",
      "disabled": true,
      "position": [
        4420,
        1320
      ],
      "parameters": {
        "jsCode": "const items = $input.all().map((item) => item.json);\n\nlet result = {};\n\nitems.forEach((item) => {\n  const tableName = item.tableName;\n  delete item.tableName;\n\n  if (!result[tableName]) {\n    result[tableName] = [];\n  }\n\n  result[tableName].push(item);\n});\n\nreturn result;\n"
      },
      "typeVersion": 2
    },
    {
      "id": "d94e0429-8e30-429d-9cfc-c53422a557fd",
      "name": "Recorrer elementos",
      "type": "n8n-nodes-base.splitInBatches",
      "disabled": true,
      "position": [
        4100,
        1300
      ],
      "parameters": {
        "options": {
          "reset": false
        }
      },
      "typeVersion": 3
    },
    {
      "id": "c152627d-5e33-4dfc-a0bc-db1aceb1d0da",
      "name": "SelectAllData",
      "type": "n8n-nodes-base.postgres",
      "disabled": true,
      "position": [
        4340,
        1540
      ],
      "parameters": {
        "table": {
          "__rl": true,
          "mode": "name",
          "value": "{{ $json.Tables_in_sql5776769 }}"
        },
        "schema": {
          "__rl": true,
          "mode": "list",
          "value": "public"
        },
        "options": {},
        "operation": "select",
        "returnAll": true
      },
      "credentials": {
        "postgres": {
          "id": "OunYREGTB5g2u3LA",
          "name": "Postgres account 2"
        }
      },
      "typeVersion": 2.6
    },
    {
      "id": "39a21d61-816d-4414-8f0b-d87adda9f350",
      "name": "OpenRouterAgente",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        860,
        120
      ],
      "parameters": {
        "text": "={{ \n\n  $if($('GenerateErrorPrompt').isExecuted, \n\n    `## ERROR HANDLING MODE\\n\\n  ${$json.prompt}.`,\n\n    $if($('localVariables').last().json.currentDbSchemaWithData !== '[]', 'Current DB tables: ' + $('localVariables').last().json.currentDbSchemaWithData + '; ',\n'') + $('localVariables').last().json.chatInput + '. Prefix for tables: ' + $('localVariables').last().json.sessionId) \n\n}}",
        "options": {
          "systemMessage": "={{ $('localVariables').last().json.instruction }}"
        },
        "promptType": "define"
      },
      "typeVersion": 1.7
    },
    {
      "id": "ec49bd8d-05cd-4438-bdb9-864a92580d5e",
      "name": "createOpenAiAssistant",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        340,
        -300
      ],
      "parameters": {
        "name": "={{ $('AgentName').last().json.agentName }} ",
        "modelId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $('localVariables').last().json.model }}"
        },
        "options": {},
        "resource": "assistant",
        "operation": "create",
        "description": "will double check code directly in a playground",
        "instructions": "={{ $('localVariables').last().json.instruction }}"
      },
      "credentials": {
        "openAiApi": {
          "id": "YYAgs4Xc0ZRuGvPa",
          "name": "OpenAi account"
        }
      },
      "typeVersion": 1.8
    },
    {
      "id": "1caf5dec-7cbd-4cbf-9687-48d4aa33cd35",
      "name": "localVariables",
      "type": "n8n-nodes-base.set",
      "position": [
        -1460,
        120
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "5ee9645b-83f9-4f0e-9fd3-4a75121bee9a",
              "name": "instruction",
              "type": "string",
              "value": "=You are a senior PostgreSQL developer helping users solve PostgreSQL tasks in a PostgreSQL Playground environment. Follow these rules:\n\n1. Your response must always contain fully executable SQL code — with no markdown formatting. Never explain or comment in natural language.\n2. Assume that the first user prompt starts with an empty PostgreSQL Playground. You must begin with `CREATE TABLE` statements to build a realistic schema related to the task.\n3. The user will provide a table name prefix (e.g., session_abc199935). All table names must begin with that prefix and be enclosed in double quotes. Example: \"session_abc199935_users\".\n4. After creating tables, always include at least 3 rows of mock data using `INSERT INTO`.\n5. Finish each response with the actual query that solves the user’s request. This query must run on the created (or existing) tables and produce a verifiable result.\n6. Always separate the final query from setup using this comment: \"\"\"-- ACTUAL_SOLUTION\"\"\". Remember use only this comment for separation.\n7. If the task involves inspecting metadata (e.g., checking indexes, foreign keys, constraints), generate mock tables first (if none exist), and then use `information_schema` or `pg_catalog` views to provide introspective queries.\n8. This conversation may span multiple questions. In follow-up prompts, the schema and data already exist. You must use the current db tables as a foundation and ** create, alter, drop tables or data if the task requires it**.\n9. If no SQL can be generated (e.g., conversational input like \"hello\", \"explain\", or messy texts etc.), respond with: words_for_user: followed by a helpful or polite message — but generate no SQL.\n\nThe goal is to always output SQL code that users can copy and run directly in a PostgreSQL Playground to verify the result. In follow-ups, your SQL must adapt to the existing schema, expanding it or querying it intelligently as needed.\n"
            }
          ]
        },
        "includeOtherFields": true
      },
      "typeVersion": 3.4
    },
    {
      "id": "e4025036-da4e-4e6b-ad3e-97c5242aa861",
      "name": "executedSQLQuery",
      "type": "n8n-nodes-base.set",
      "position": [
        4240,
        20
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "cfc317f9-d9fa-4026-9b2a-1cd4b13b87c6",
              "name": "query",
              "type": "string",
              "value": "={{ \n  $if($('localVariables').last().json.aiProvider === 'openai', \n    $('OpenAIMainBrain').last().json.output, \n    $('OpenRouterAgent').last().json.output\n  )\n}}"
            },
            {
              "id": "594046d1-9b2d-43cb-9609-04ee4f70364f",
              "name": "type",
              "type": "string",
              "value": "success"
            },
            {
              "id": "c6de1fb5-60a4-4050-a401-973139f42dfb",
              "name": "executionResult",
              "type": "string",
              "value": "={{ $json }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "ff239015-892a-4762-8de6-537396e1d1c3",
      "name": "issueOnOpenAiSide",
      "type": "n8n-nodes-base.set",
      "position": [
        -240,
        -260
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "9cbea64a-8820-40df-9e88-f1d3ac5e5d06",
              "name": "type",
              "type": "string",
              "value": "error"
            },
            {
              "id": "d391b90b-79d7-45b4-b9f2-6bc4e45ab2c5",
              "name": "message",
              "type": "string",
              "value": "={{ $json.error }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    }
  ],
  "active": true,
  "pinData": {
    "When chat message received": [
      {
        "json": {
          "model": "gpt-4.1-mini",
          "apiKey": "YOURAPIKEY",
          "threadId": null,
          "chatInput": "how to check if all {pattern}_id columns have indexes through all tables prefix for tables.",
          "sessionId": "01981188-bba3-731e-abd0-ca8c5cfeef6b",
          "aiProvider": "openai",
          "autoErrorFixing": true,
          "currentDbSchemaWithData": "[]"
        }
      }
    ]
  },
  "settings": {
    "callerPolicy": "workflowsFromSameOwner",
    "errorWorkflow": "BLUvEJjOxACvh7gM",
    "executionOrder": "v1"
  },
  "versionId": "1bbe9a2a-23b3-469b-a4c8-3877c2baf140",
  "connections": {
    "43a23e59-31f7-40fb-b911-9e3dd9bad912": {
      "main": [
        [
          {
            "node": "isAssistantExistsCode",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "ff239015-892a-4762-8de6-537396e1d1c3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "6e1fcee6-2c63-46e3-a48c-458180257b43": {
      "main": [
        [
          {
            "node": "7f42bf3b-5d99-41d4-94e7-0a34da39e285",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "4f3fcdf7-fe96-4137-9356-165890ea57d1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "af7314ea-60db-4873-838d-7a01dffc70ed": {
      "main": [
        [
          {
            "node": "AgentName",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "OpenRouterAgent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "ff979107-3937-4feb-aaf3-4ff9337325da": {
      "main": [
        [
          {
            "node": "c1c4bc98-9f7f-459b-9c1e-8ce1de98f24c",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "OpenRouterAgent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AgentName": {
      "main": [
        [
          {
            "node": "73bc7418-b260-4c38-bc15-ec3ed50086df",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "6e2a2dd7-8ca7-4662-9c7d-5c7cab0dd314": {
      "main": [
        [
          {
            "node": "c1c4bc98-9f7f-459b-9c1e-8ce1de98f24c",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "1347b643-7109-4148-86bd-89b540861b06": {
      "main": [
        [
          {
            "node": "9f28f786-c999-4614-8d74-e67f945583e2",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "817ec035-7af7-41dd-b9f6-f51fff4b6c1d",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "c152627d-5e33-4dfc-a0bc-db1aceb1d0da": {
      "main": [
        [
          {
            "node": "def0dfb5-e863-4712-83bd-e5d00599ac4d",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "OpenRouterAgent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "d004ab56-4a20-4a9d-ae2b-c899421af2af": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "1caf5dec-7cbd-4cbf-9687-48d4aa33cd35": {
      "main": [
        [
          {
            "node": "af7314ea-60db-4873-838d-7a01dffc70ed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "7f42bf3b-5d99-41d4-94e7-0a34da39e285": {
      "main": [
        [
          {
            "node": "e10c42db-8c9f-4052-8205-259b53ebdc3f",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "723fd28e-628e-417a-826c-2fd025a8a026",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [
          {
            "node": "a9832d33-f46e-4273-b7b3-0019bd33cc93",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "d0ef4902-a0dc-4ad4-a970-db60e4716935",
            "type": "main",
            "index": 0
          },
          {
            "node": "c152627d-5e33-4dfc-a0bc-db1aceb1d0da",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "c1c4bc98-9f7f-459b-9c1e-8ce1de98f24c": {
      "main": [
        [
          {
            "node": "1347b643-7109-4148-86bd-89b540861b06",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenRouterAgent": {
      "main": [
        [
          {
            "node": "1347b643-7109-4148-86bd-89b540861b06",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "e4025036-da4e-4e6b-ad3e-97c5242aa861": {
      "main": [
        []
      ]
    },
    "4f3fcdf7-fe96-4137-9356-165890ea57d1": {
      "main": [
        [
          {
            "node": "e4025036-da4e-4e6b-ad3e-97c5242aa861",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "424b2831-425b-4ee7-a3d5-b2713bfdf343",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "73bc7418-b260-4c38-bc15-ec3ed50086df": {
      "main": [
        [
          {
            "node": "43a23e59-31f7-40fb-b911-9e3dd9bad912",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "d4c60f3d-425e-4fef-923c-a2fa154f0a00": {
      "main": [
        [
          {
            "node": "6e2a2dd7-8ca7-4662-9c7d-5c7cab0dd314",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "ec49bd8d-05cd-4438-bdb9-864a92580d5e",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "424b2831-425b-4ee7-a3d5-b2713bfdf343": {
      "main": [
        [
          {
            "node": "ff979107-3937-4feb-aaf3-4ff9337325da",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "9f28f786-c999-4614-8d74-e67f945583e2": {
      "main": [
        [
          {
            "node": "6e1fcee6-2c63-46e3-a48c-458180257b43",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "def0dfb5-e863-4712-83bd-e5d00599ac4d": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "e10c42db-8c9f-4052-8205-259b53ebdc3f": {
      "main": [
        [
          {
            "node": "maxAutoErrorLimitReached",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "4f3fcdf7-fe96-4137-9356-165890ea57d1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "1a3189fd-550e-4b1c-8720-65579fa39c44": {
      "ai_languageModel": [
        [
          {
            "node": "OpenRouterAgent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "ec49bd8d-05cd-4438-bdb9-864a92580d5e": {
      "main": [
        [
          {
            "node": "6e2a2dd7-8ca7-4662-9c7d-5c7cab0dd314",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "isAssistantExistsCode": {
      "main": [
        [
          {
            "node": "d4c60f3d-425e-4fef-923c-a2fa154f0a00",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "d0ef4902-a0dc-4ad4-a970-db60e4716935": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "When chat message received": {
      "main": [
        [
          {
            "node": "1caf5dec-7cbd-4cbf-9687-48d4aa33cd35",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "a9832d33-f46e-4273-b7b3-0019bd33cc93": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
Preguntas frecuentes

¿Cómo usar este flujo de trabajo?

Copie el código de configuración JSON de arriba, cree un nuevo flujo de trabajo en su instancia de n8n y seleccione "Importar desde JSON", pegue la configuración y luego modifique la configuración de credenciales según sea necesario.

¿En qué escenarios es adecuado este flujo de trabajo?

Avanzado - Ingeniería, Chatbot de IA

¿Es de pago?

Este flujo de trabajo es completamente gratuito, puede importarlo y usarlo directamente. Sin embargo, tenga en cuenta que los servicios de terceros utilizados en el flujo de trabajo (como la API de OpenAI) pueden requerir un pago por su cuenta.

Información del flujo de trabajo
Nivel de dificultad
Avanzado
Número de nodos40
Categoría2
Tipos de nodos12
Descripción de la dificultad

Adecuado para usuarios avanzados, flujos de trabajo complejos con 16+ nodos

Enlaces externos
Ver en n8n.io

Compartir este flujo de trabajo

Categorías

Categorías: 34