MS SQL Server chatbot

Hello,

I want to use a simple n8n workflow to query my local MS SQL Server database via chat message.

N8N runs under Docker locally on my computer.

I would like to use Ollama.

The workflow itself is running only the SQL statements are not created and passed to the MS SQL Node.

Can anyone help me?

My workflow consists of the following parts:

Trigger: Chat Trigger

AI Agent as Tool Agent

→ Chat Model = Ollama Chat Model (mistral:7b)

→ Memory = Simple Memory

→ Tool: Microsoft SQL (Execute Query)

All connections are functional

My workflow:

Has anyone built something like this before? We want to give our users the possibility to make queries independently via chat.

Greetings
Charly

Hi Charly_Ch,
I’d love to help. Just for reference, I am going to be using the following template…

:closed_lock_with_key::llama::robot: Private & Local Ollama Self-Hosted Al Assistant: 🔐🦙🤖 Private & Local Ollama Self-Hosted AI Assistant | n8n workflow template

…as a guide. Thanks to Joe: https://n8n.io/creators/joe/ for an awesome template.

I’m sending this message right away to allow you to work in parallel while I’m thinking/working, too.

My first thought as a starter reply is that it looks like your workflow is missing the response flow from Joe’s template.

Here’s the full template from Joe here in this chat (also available at here https://n8n.io/workflows/2729-private-and-local-ollama-self-hosted-ai-assistant/:

I will keep working on this, but I’m wondering if that helps?
-Tyler

Are you running n8n, Ollama & MS SQL in a multi-container application: Multi-container applications | Docker Docs all together? Just confirming from your “All connections are functional” comment.

Hi tsellhorn,

Thank you very much for your help.
This is my actual State:

{
  "name": "SQL AI Agent - funktioniert fast",
  "nodes": [
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "USE datawarehouse;\n\nSELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE\nFROM INFORMATION_SCHEMA.COLUMNS\nWHERE TABLE_SCHEMA = 'dbo'\nORDER BY TABLE_NAME, ORDINAL_POSITION;\n"
      },
      "id": "ba2700cc-b96d-4bfc-b04c-a91dea98709f",
      "name": "Get DB Schema",
      "type": "n8n-nodes-base.microsoftSql",
      "typeVersion": 1,
      "position": [
        1500,
        140
      ],
      "alwaysOutputData": true,
      "credentials": {
        "microsoftSql": {
          "id": "a643CpWdK4y4gPDH",
          "name": "Microsoft SQL account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const inputItems = $input.all(); // Alle Zeilen aus dem vorherigen Node\nconst tableMap = {}; // Objekt zur Gruppierung der Spalten pro Tabelle\n\nfor (const item of inputItems) {\n  const { TABLE_NAME, COLUMN_NAME, DATA_TYPE } = item.json;\n\n  if (!tableMap[TABLE_NAME]) {\n    tableMap[TABLE_NAME] = [];\n  }\n\n  tableMap[TABLE_NAME].push(`- ${COLUMN_NAME}: ${DATA_TYPE}`);\n}\n\n// Formatiertes Schema erstellen\nlet promptSchema = 'Die Datenbank `datawarehouse` enthält folgende Tabellen:\\n\\n';\nfor (const [table, columns] of Object.entries(tableMap)) {\n  promptSchema += `Tabelle: ${table}\\n${columns.join('\\n')}\\n\\n`;\n}\n\n// Prompt als neues JSON zurückgeben\nreturn [\n  {\n    json: {\n      promptSchema\n    }\n  }\n];\n"
      },
      "id": "ad63946b-45dd-451b-8b81-31fb29d69891",
      "name": "Format Schema Text",
      "type": "n8n-nodes-base.code",
      "typeVersion": 1,
      "position": [
        1740,
        120
      ]
    },
    {
      "parameters": {},
      "id": "88d28e22-e4d4-4096-b9ed-a8343accc1fc",
      "name": "Chat Trigger",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "typeVersion": 1,
      "position": [
        1200,
        140
      ],
      "webhookId": "dynamic-sql-agent"
    },
    {
      "parameters": {
        "model": "mistral:7b",
        "options": {}
      },
      "id": "aff1c0ca-566d-4352-a917-6d8dfcf71e91",
      "name": "Ollama Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOllama",
      "typeVersion": 1,
      "position": [
        1920,
        440
      ],
      "credentials": {
        "ollamaApi": {
          "id": "b1F3VB0Aj4eGL5Jl",
          "name": "Ollama account"
        }
      }
    },
    {
      "parameters": {},
      "id": "1412e4a5-464a-4f9b-a524-30e6929e2fa9",
      "name": "Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1,
      "position": [
        2120,
        420
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=Hier ist das Datenbankschema der `datawarehouse`-Datenbank:\n\n{{ $json.promptSchema }}\n\nBasierend auf der folgenden Benutzerfrage:\n\"{{ $json.userMessage }}\"\n\nFinde die am besten passende Tabelle und generiere eine SQL-Abfrage, um die Frage zu beantworten.\n\n- Nutze COUNT(*) wenn nach Anzahl gefragt wird\n- Gib nur die SQL-Abfrage zurück, ohne Erklärung\n- Nutze Tabellennamen und Spaltennamen exakt wie im Schema\n\nWenn die Tabelle \"Auftraege\" erwähnt wird, ist wahrscheinlich die Tabelle `auftraege` gemeint – nutze dein bestes Sprachverständnis, um passende Tabellen zuzuordnen.\n",
        "options": {}
      },
      "id": "c2c16866-40ef-42b8-804e-82cdd45c70a2",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 1.8,
      "position": [
        2000,
        180
      ],
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "antwort",
              "value": "Ergebnis: {{ $json[0] || 'Keine Daten zurückgegeben' }}"
            }
          ]
        },
        "options": {}
      },
      "id": "0a3f0ed2-3876-448e-b5ce-2496a65075c9",
      "name": "Antwort formatieren",
      "type": "n8n-nodes-base.set",
      "typeVersion": 2,
      "position": [
        2780,
        180
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery"
      },
      "type": "n8n-nodes-base.microsoftSql",
      "typeVersion": 1.1,
      "position": [
        2540,
        180
      ],
      "id": "e7327c46-c182-4e63-b06e-c48e3d7fd27c",
      "name": "Microsoft SQL",
      "credentials": {
        "microsoftSql": {
          "id": "a643CpWdK4y4gPDH",
          "name": "Microsoft SQL account"
        }
      }
    },
    {
      "parameters": {
        "mode": "raw",
        "jsonOutput": "{{ $json.output }}",
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        2340,
        180
      ],
      "id": "bbad8b5e-7351-4a59-bed8-00480bbcb3d3",
      "name": "Edit Fields"
    }
  ],
  "pinData": {},
  "connections": {
    "Chat Trigger": {
      "main": [
        [
          {
            "node": "Get DB Schema",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get DB Schema": {
      "main": [
        [
          {
            "node": "Format Schema Text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Schema Text": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ollama Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Antwort formatieren": {
      "main": [
        []
      ]
    },
    "Microsoft SQL": {
      "main": [
        [
          {
            "node": "Antwort formatieren",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Fields": {
      "main": [
        [
          {
            "node": "Microsoft SQL",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "b0b06bea-4002-412b-9848-a96fe2087066",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "ad5ba5777f8d48f5b9b814aaa0019bfd1a2b000bb69aaf3c9d3245cf58e109be"
  },
  "id": "jrwGIbPNHkv1DlFO",
  "tags": []
}```


The Workflow is running in all Modules (Ollama, SQL..).
My problem is that my AI agent doesn't really learn. A question “How many orders are there” is implemented correctly by the part. 
( SELECT COUNT(*) FROM datawarehouse.dbo.orders;)
But if I ask another more detailed query, the correct statement does not come up.

Greetings from Germany

Hello @tsellhorn

Thank you very much for your help.

This is my actual workflow:

It is working, but

My problem is that my AI agent doesn’t really learn. A question “How many orders are there” is implemented correctly by the part.
( SELECT COUNT(*) FROM datawarehouse.dbo.orders;)
But if I ask another more detailed query, the correct statement does not come up.