使用自然语言查询您的数据库
中级
这是一个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)可能需要您自行付费。
相关工作流推荐
具有Graph RAG知识本体的推理专家
使用GraphRAG和知识本体为AI代理创建自定义推理模式
Agent
Http Request Tool
Chat Trigger
+3
8 节点InfraNodus
工程
LLM模板
使用GPT-4o-mini和Qdrant向量数据库构建持久聊天记忆
Set
Agent
Chat Trigger
+8
25 节点Einar César Santos
工程
数据分析师 Agent v3
用于电子表格的AI数据分析助手,基于NocoDB平台
Set
Noco Db Tool
Http Request
+5
10 节点Derek Cheung
工程
AI智能助手:与Supabase存储和Google Drive文件对话
AI智能助手:与Supabase存储和Google Drive文件对话
If
Set
Wait
+20
62 节点Mark Shcherbakov
工程
AI智能助手与Airtable对话及数据分析
AI智能助手与Airtable对话及数据分析
If
Set
Merge
+12
41 节点Mark Shcherbakov
工程
集成MySQL数据库的AI聊天机器人工作流
集成MySQL数据库的AI驱动聊天机器人工作流
My Sql Tool
Agent
Lm Chat Groq
+3
11 节点Gegenfeld
工程