{
  "id": "AJrzScGasCkGFkT7",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Query your database using natural language",
  "tags": [],
  "nodes": [
    {
      "id": "035aad69-00f5-4734-8a7d-d98f027104e0",
      "name": "When chat message received",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        -480,
        -160
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 1.1
    },
    {
      "id": "affe6a28-d20d-4985-b48f-d8b1f4e3eee0",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -96,
        -144
      ],
      "parameters": {
        "text": "=You are a helpful internal AI assistant.\n\nYour job is to answer user questions using your tools, internal company knowledge, and general llm training.\n\nToday's date is {{ $now }}\n\nYou have access to:\n- A MySQL database. Use the \"SQL DB - List Tables and Schema\" tool first to determine available tables and fields, and only write queries using fields returned by it. Execute queries with the \"SQL DB Execute Query\" tool.\n\nGuidelines:\n- Always prefer tools over guessing when answering factual or data-related questions.\n- For anything involving data from the MySQL database, first call the schema tool to learn what tables and fields are available.\n- You may answer general knowlege questions without using tools when appropriate.\n- Include specific record identifiers (like id columns) when referencing database results to avoid ambiguity.\n- When users mention time periods like \"last month\", interpret them as the most recent full calendar month.\n- When querying unit floor dates, floor_on references the date of the most recent floor (first floor or refloor), where first_floored_on is a timestamp of the first time the unit was put on the books.\n- Keep your responses clear, concise, and professional.\n- Use markdown formatting in your response when markdown would result in a more aesthetic completion.\n\nYou are always friendly, helpful, and precise \u2014 acting like a power user who knows how to get the right data fast.",
        "options": {},
        "promptType": "define"
      },
      "typeVersion": 2.1
    },
    {
      "id": "df5c3027-7cea-4ac6-8874-02fa76573467",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        -224,
        80
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1-mini"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "dd1eec64-d710-4f1f-9449-17ac3adeb570",
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        -96,
        48
      ],
      "parameters": {},
      "typeVersion": 1.3
    },
    {
      "id": "c9b5655f-4906-408f-8fff-d478fc8afee2",
      "name": "SQL DB - List Tables and Schema",
      "type": "n8n-nodes-base.mySqlTool",
      "position": [
        32,
        48
      ],
      "parameters": {
        "query": "SELECT TABLE_NAME, COLUMN_NAME\nFROM INFORMATION_SCHEMA.COLUMNS\nWHERE TABLE_SCHEMA = 'your_database_name'\nORDER BY TABLE_NAME, ORDINAL_POSITION;\n",
        "options": {},
        "operation": "executeQuery"
      },
      "typeVersion": 2.4
    },
    {
      "id": "c16c8916-e399-4eb1-a18a-ca07ced02aef",
      "name": "Execute a SQL query in MySQL",
      "type": "n8n-nodes-base.mySqlTool",
      "position": [
        160,
        80
      ],
      "parameters": {
        "options": {},
        "operation": "executeQuery"
      },
      "typeVersion": 2.4
    },
    {
      "id": "45db7eb6-df73-4833-9e1c-6bf7afba1de0",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -544,
        -256
      ],
      "parameters": {
        "width": 272,
        "height": 288,
        "content": "Ask agent a question in plain English. AI Agent will execute a SQL query on your database for the answer"
      },
      "typeVersion": 1
    },
    {
      "id": "21238f7a-12f1-4625-b50e-f312a3c9bc1b",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -256,
        -336
      ],
      "parameters": {
        "width": 560,
        "height": 528,
        "content": "1. Add your OpenAI API Key in \"OpenAI Chat Model\" node\n2. Add your MySQL credentials in the \"SQL DB - List Tables and Schema\" and \"Execute a SQL Query in MySQL nodes\"\n3. Update the database name in \"SQL DB - List Tables and Schema\" node. Replace \"your_query_name\" under the Query field with your actual database name\n\nNote: This agent can remember the previous 5 messages by default but can be adjusted in the \"Session Memory\" node"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "beac50c8-6fdc-46ef-bc0a-c414354449a7",
  "connections": {
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "When chat message received": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute a SQL query in MySQL": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "SQL DB - List Tables and Schema": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    }
  }
}