8
n8n 中文网amn8n.com

模板 2

高级

这是一个Engineering, AI领域的自动化工作流,包含 20 个节点。主要使用 Set, Merge, Switch, Postgres, Telegram 等节点,结合人工智能技术实现智能自动化。 使用双代理AI和PostgreSQL集成的自然语言数据库查询

前置要求
  • PostgreSQL 数据库连接信息
  • Telegram Bot Token
  • OpenAI API Key
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "XIcKeYYhzG9DdDPN",
  "meta": {
    "instanceId": "e7ccf4281d5afb175c79c02db95b45f15d5b53862cb6bc357c5e5bc26567f35c",
    "templateCredsSetupCompleted": true
  },
  "name": "模板 2",
  "tags": [],
  "nodes": [
    {
      "id": "c4a7b5c1-f74e-4de0-9469-146a197c42f5",
      "name": "当被其他工作流执行时",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "position": [
        -1560,
        360
      ],
      "parameters": {
        "inputSource": "passthrough"
      },
      "typeVersion": 1.1
    },
    {
      "id": "1f37374e-3c5c-4dad-bfa0-5e948cfbd6e5",
      "name": "查询代理",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -1320,
        360
      ],
      "parameters": {
        "text": "={{ $json.query }}",
        "options": {
          "systemMessage": "# SQL Query Generator - Sub Agent Template\n\nYou are a PostgreSQL query generator. Convert user requests into SELECT queries for the database table.\n\n## CRITICAL: OUTPUT FORMAT\n- Output ONLY the SQL query\n- NO explanations, descriptions, or additional text\n- NO code blocks or formatting\n- Just the raw SQL query string\n\n## DATABASE SCHEMA:\n**[REPLACE WITH YOUR DATABASE SCHEMA]**\n```\n[TABLE_NAME] Table:\nid (bigint, Primary Key)\n[field_name] (data_type)\n[field_name] (data_type)\n...\n```\n\n## STRICT RULES:\n1. ONLY SELECT queries - NO INSERT/UPDATE/DELETE/DROP/ALTER\n2. Table name: \"[YOUR_TABLE_NAME]\" (use double quotes if needed)\n3. Column names with spaces: use double quotes (\"[column name]\")\n4. Boolean columns: use true/false (not 1/0)\n5. Text searches: use ILIKE for case-insensitive matching\n6. Always end query with semicolon\n\n## EXAMPLES:\n**[CUSTOMIZE THESE EXAMPLES FOR YOUR USE CASE]**\nUser: \"Show verified entries\" \nResponse: SELECT * FROM \"[YOUR_TABLE]\" WHERE [verified_field] = true;\n\nUser: \"Top 10 most popular\"  \nResponse: SELECT * FROM \"[YOUR_TABLE]\" ORDER BY [popularity_field] DESC LIMIT 10;\n\nUser: \"Items containing [keyword]\"\nResponse: SELECT * FROM \"[YOUR_TABLE]\" WHERE [description_field] ILIKE '%[keyword]%';\n\nREMEMBER: Output ONLY the SQL query, nothing else."
        },
        "promptType": "define"
      },
      "typeVersion": 2
    },
    {
      "id": "1019eeda-425f-4450-934e-0224c689a801",
      "name": "思考",
      "type": "@n8n/n8n-nodes-langchain.toolThink",
      "position": [
        -1140,
        580
      ],
      "parameters": {
        "description": "思考输出的查询是否真的匹配输入消息。"
      },
      "typeVersion": 1
    },
    {
      "id": "2219bc4a-cdf2-4297-9bd6-423d0f138319",
      "name": "OpenRouter 聊天模型",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenRouter",
      "position": [
        -1340,
        560
      ],
      "parameters": {
        "model": "anthropic/claude-3.5-sonnet",
        "options": {}
      },
      "credentials": {
        "openRouterApi": {
          "id": "1t22HwKbMlWsWxcJ",
          "name": "OpenRouter account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "21a65536-abfd-4d19-94c9-9aa8d839db16",
      "name": "Postgres 聊天记忆",
      "type": "@n8n/n8n-nodes-langchain.memoryPostgresChat",
      "position": [
        -280,
        80
      ],
      "parameters": {
        "sessionKey": "C08SU8T0FJ8",
        "sessionIdType": "customKey"
      },
      "credentials": {
        "postgres": {
          "id": "6uE1gPELXSqR1Wgn",
          "name": "Postgres accou"
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "32cf9699-649c-4781-b27b-c68e7ff306a4",
      "name": "OpenRouter 聊天模型1",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenRouter",
      "position": [
        -440,
        80
      ],
      "parameters": {
        "model": "anthropic/claude-3.5-sonnet",
        "options": {}
      },
      "credentials": {
        "openRouterApi": {
          "id": "1t22HwKbMlWsWxcJ",
          "name": "OpenRouter account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "cacfb298-2ee6-4292-ade7-bb18436df301",
      "name": "思考 1",
      "type": "@n8n/n8n-nodes-langchain.toolThink",
      "position": [
        -140,
        80
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "6c979f9c-38df-4b8d-a7c2-be499bd37cd7",
      "name": "下载文件1",
      "type": "n8n-nodes-base.telegram",
      "position": [
        -1040,
        -280
      ],
      "webhookId": "00c7755c-cd84-4aa4-89fe-c56f1f0cb50d",
      "parameters": {
        "fileId": "={{ $json.message.voice.file_id }}",
        "resource": "file"
      },
      "credentials": {
        "telegramApi": {
          "id": "GX0f93u8bE9meFhE",
          "name": "customer service"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "578c6f2d-d738-49d3-95f4-6de6a48c5754",
      "name": "转录1",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        -820,
        -280
      ],
      "parameters": {
        "options": {},
        "resource": "audio",
        "operation": "transcribe"
      },
      "credentials": {
        "openAiApi": {
          "id": "OQJASLp1qn1StvpI",
          "name": "OpenAi account"
        }
      },
      "typeVersion": 1.6
    },
    {
      "id": "68b40dcf-46a0-47e8-9f63-ce020f08c4c6",
      "name": "Telegram 触发器1",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        -1480,
        -180
      ],
      "webhookId": "75096a78-a466-4f4e-bc0d-f68e9e952231",
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "id": "1vaRNATU5x2lDpH2",
          "name": "supachat"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "e28131b3-4c13-4c5a-b6d1-e9be94970c9f",
      "name": "语音或文本",
      "type": "n8n-nodes-base.switch",
      "position": [
        -1260,
        -180
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "Voice",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "612fa3b1-0e08-47f3-9455-746568546ab6",
                    "operator": {
                      "type": "string",
                      "operation": "exists",
                      "singleValue": true
                    },
                    "leftValue": "={{ $json.message.voice.file_id }}",
                    "rightValue": ""
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Text",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "8c844924-b2ed-48b0-935c-c66a8fd0c778",
                    "operator": {
                      "type": "string",
                      "operation": "exists",
                      "singleValue": true
                    },
                    "leftValue": "={{ $json.message.text }}",
                    "rightValue": ""
                  }
                ]
              },
              "renameOutput": true
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "b8d80841-beeb-43f9-a51b-3ea3683c4ba1",
      "name": "文本",
      "type": "n8n-nodes-base.set",
      "position": [
        -940,
        -80
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "fe7ecc99-e1e8-4a5e-bdd6-6fce9757b234",
              "name": "text",
              "type": "string",
              "value": "={{ $json.message.text }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "e5e1508a-ceb2-49fd-b3d2-ef87338a230b",
      "name": "合并",
      "type": "n8n-nodes-base.merge",
      "position": [
        -600,
        -180
      ],
      "parameters": {},
      "typeVersion": 3.1
    },
    {
      "id": "be9cde2d-e131-406b-a061-49193941e159",
      "name": "主代理",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -320,
        -180
      ],
      "parameters": {
        "text": "={{ $json.text }}",
        "options": {
          "systemMessage": "# Database Assistant - Main Agent\n\nYou are an intelligent database assistant that helps users find and explore information from a structured database. You work with a specialized SQL sub-agent to retrieve data efficiently.\n\n## DATABASE SCHEMA:\n**[REPLACE WITH YOUR DATABASE SCHEMA]**\n```\nExample format:\nid (bigint, Primary Key)\nname (text)\ndescription (text)\ncategory (text)\n...\n```\n\n## CRITICAL OPERATIONAL RULES:\n- **ALWAYS use LIMIT in queries** - Default: 10, Maximum: 50\n- **NEVER request unlimited data** - this breaks the system\n- Use ORDER BY with LIMIT for meaningful results\n- Apply specific filters and conditions to narrow results\n\n## COMMUNICATION WITH SQL SUB-AGENT:\nSend clear, natural language requests:\n- \"Show me the 10 most popular [YOUR_ITEMS] in [CATEGORY_FIELD]\"\n- \"Find 5 newest verified entries containing '[KEYWORD]'\"\n- \"Get top 15 [YOUR_ITEMS] by [ACTIVITY_METRIC]\"\n- \"Search for entries with [PRICE_FIELD] less than [AMOUNT], limit 20\"\n\n## RESPONSE GUIDELINES:\nPresent results in a user-friendly format:\n- Highlight key information ([NAME_FIELD], [DESCRIPTION_FIELD], metrics)\n- Include relevant metadata ([VERIFICATION_FIELD], [POPULARITY_FIELD], [CATEGORIES_FIELD])\n- Provide actionable insights\n- Suggest related searches or refinements\n\n## DATA INTERPRETATION:\n**[CUSTOMIZE THESE FIELD DESCRIPTIONS]**\n- **[POPULARITY_FIELD]**: [Description of what this metric means]\n- **[ACTIVITY_FIELD]**: [Description of recent activity indicator]\n- **[VERIFICATION_FIELD]**: [Description of quality/trust indicator]\n- **[CATEGORY_FIELD]**: [Description of classification system]\n- **[PRICE_FIELD]**: [Description of pricing (0 = free, etc.)]\n\nAlways respect system limits and focus on delivering relevant, targeted results."
        },
        "promptType": "define"
      },
      "typeVersion": 2
    },
    {
      "id": "ec7a16f3-fd62-4952-a074-3c044de6e86f",
      "name": "发送消息",
      "type": "n8n-nodes-base.telegram",
      "position": [
        120,
        -180
      ],
      "webhookId": "1aa59dcd-d299-439f-9fdc-96eae710da9d",
      "parameters": {
        "text": "={{ $json.output }}",
        "chatId": "(YOUR CHAT ID)",
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "id": "1vaRNATU5x2lDpH2",
          "name": "supachat"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "4dfb46ad-d110-40e6-9a87-3c247fc2d8d3",
      "name": "调用查询代理",
      "type": "@n8n/n8n-nodes-langchain.toolWorkflow",
      "position": [
        -20,
        80
      ],
      "parameters": {
        "workflowId": {
          "__rl": true,
          "mode": "list",
          "value": "Iw7dOCXYbpfRJrlW",
          "cachedResultName": "query agent"
        },
        "description": "为自然语言的 SQL 查询调用此工具",
        "workflowInputs": {
          "value": {},
          "schema": [],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "dd79a9fb-cf29-4266-a51d-114713e030c0",
      "name": "使用动态查询访问数据库",
      "type": "n8n-nodes-base.postgres",
      "position": [
        -980,
        360
      ],
      "parameters": {
        "query": "{{ $json.output }}",
        "options": {
          "largeNumbersOutput": "numbers",
          "replaceEmptyStrings": true
        },
        "operation": "executeQuery"
      },
      "credentials": {
        "postgres": {
          "id": "6uE1gPELXSqR1Wgn",
          "name": "Postgres accou"
        }
      },
      "typeVersion": 2.6
    },
    {
      "id": "a302abcd-e6a0-4150-bcd7-84c549c1dc94",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1600,
        -540
      ],
      "parameters": {
        "color": 6,
        "width": 1960,
        "height": 800,
        "content": "# 主代理"
      },
      "typeVersion": 1
    },
    {
      "id": "cfcf980f-e037-4c6a-9dd3-7ba4359f6b44",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1600,
        260
      ],
      "parameters": {
        "color": 4,
        "width": 1220,
        "height": 440,
        "content": "# 子代理"
      },
      "typeVersion": 1
    },
    {
      "id": "4aaa94db-f919-44d4-9c0f-c703d3838e94",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2320,
        -540
      ],
      "parameters": {
        "color": 2,
        "width": 1780,
        "height": 1760,
        "content": "# 设置与配置指南"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "36903df7-c2e2-4b12-b58d-9c9fbeebea24",
  "connections": {
    "Text": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Main agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Think": {
      "ai_tool": [
        [
          {
            "node": "Query agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Think1": {
      "ai_tool": [
        [
          {
            "node": "Main agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Main agent": {
      "main": [
        [
          {
            "node": "SEND MESSAGE",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Query agent": {
      "main": [
        [
          {
            "node": "ACCES DATABASE WITH DYNAMIC QUERYS",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Transcribe1": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Voice or Text": {
      "main": [
        [
          {
            "node": "Download File1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download File1": {
      "main": [
        [
          {
            "node": "Transcribe1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "CALL QUERY AGENT": {
      "ai_tool": [
        [
          {
            "node": "Main agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Telegram Trigger1": {
      "main": [
        [
          {
            "node": "Voice or Text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Postgres Chat Memory": {
      "ai_memory": [
        [
          {
            "node": "Main agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "OpenRouter Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Query agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "OpenRouter Chat Model1": {
      "ai_languageModel": [
        [
          {
            "node": "Main agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "When Executed by Another Workflow": {
      "main": [
        [
          {
            "node": "Query agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

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

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

高级 - 工程, 人工智能

需要付费吗?

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

工作流信息
难度等级
高级
节点数量20
分类2
节点类型14
难度说明

适合高级用户,包含 16+ 个节点的复杂工作流

作者

Automation expert & n8n power user. I build advanced workflows combining AI, outbound, and business logic. Grab my templates or reach out for custom builds.

外部链接
在 n8n.io 查看

分享此工作流