8
n8n 中文网amn8n.com

使用自然语言查询您的数据库

中级

这是一个Engineering, AI RAG领域的自动化工作流,包含 8 个节点。主要使用 MySqlTool, Agent, ChatTrigger, LmChatOpenAi, MemoryBufferWindow 等节点。 使用 GPT AI 通过自然语言查询 MySQL 数据库

前置要求
  • MySQL 数据库连接信息
  • OpenAI API Key
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "AJrzScGasCkGFkT7",
  "meta": {
    "instanceId": "bdc54da2c96840612a04bf3fd3a4cd97a7a7bd7c1152bbe41d5615f09311c097",
    "templateCredsSetupCompleted": true
  },
  "name": "使用自然语言查询您的数据库",
  "tags": [],
  "nodes": [
    {
      "id": "035aad69-00f5-4734-8a7d-d98f027104e0",
      "name": "当收到聊天消息时",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        -480,
        -160
      ],
      "webhookId": "b0546272-69b8-4684-9812-c2d2de0c9965",
      "parameters": {
        "options": {}
      },
      "typeVersion": 1.1
    },
    {
      "id": "affe6a28-d20d-4985-b48f-d8b1f4e3eee0",
      "name": "AI 代理",
      "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 — 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 聊天模型",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        -224,
        80
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1-mini"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "id": "yM0BC2G15bWPjEUz",
          "name": "OpenAi account"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "dd1eec64-d710-4f1f-9449-17ac3adeb570",
      "name": "简单记忆",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        -96,
        48
      ],
      "parameters": {},
      "typeVersion": 1.3
    },
    {
      "id": "c9b5655f-4906-408f-8fff-d478fc8afee2",
      "name": "SQL 数据库 - 列出表和架构",
      "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": "在 MySQL 中执行 SQL 查询",
      "type": "n8n-nodes-base.mySqlTool",
      "position": [
        160,
        80
      ],
      "parameters": {
        "options": {},
        "operation": "executeQuery"
      },
      "typeVersion": 2.4
    },
    {
      "id": "45db7eb6-df73-4833-9e1c-6bf7afba1de0",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -544,
        -256
      ],
      "parameters": {
        "width": 272,
        "height": 288,
        "content": "用简单英语向智能体提问。AI 智能体将在您的数据库上执行 SQL 查询以获取答案"
      },
      "typeVersion": 1
    },
    {
      "id": "21238f7a-12f1-4625-b50e-f312a3c9bc1b",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -256,
        -336
      ],
      "parameters": {
        "width": 560,
        "height": 528,
        "content": "1. 在 \"OpenAI 聊天模型\" 节点中添加您的 OpenAI API 密钥"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "pinData": {},
  "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
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

中级 - 工程, AI RAG 检索增强

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
中级
节点数量8
分类2
节点类型6
难度说明

适合有一定经验的用户,包含 6-15 个节点的中等复杂度工作流

外部链接
在 n8n.io 查看

分享此工作流