8
n8n 中文网amn8n.com

使用Supabase + TogetherAI + Openrouter的RAG聊天机器人

中级

这是一个Internal Wiki, AI RAG领域的自动化工作流,包含 13 个节点。主要使用 Code, Supabase, Aggregate, GoogleDocs, HttpRequest 等节点。 使用Supabase + TogetherAI + Openrouter的RAG聊天机器人

前置要求
  • Supabase URL 和 API Key
  • 可能需要目标 API 的认证凭证
  • Telegram Bot Token
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "b6731f6e46273cffeac6b5188a5b3bcb0840c00108e73b48ae2b799ac6e8e081",
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "7ac1a47f-3373-418c-84ff-30106c911340",
      "name": "训练内容",
      "type": "n8n-nodes-base.googleDocs",
      "position": [
        -1880,
        -100
      ],
      "parameters": {
        "operation": "get",
        "documentURL": "1shUBSb2aEFZrOWOROcCeG9glxi7u205p4lEopNt1e7E",
        "authentication": "serviceAccount"
      },
      "credentials": {
        "googleApi": {
          "id": "bK1tErzcCo9nt89s",
          "name": "Google Service Account account"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "2aabc964-23e8-4113-8280-9eb70588c858",
      "name": "分割成文本块",
      "type": "n8n-nodes-base.code",
      "position": [
        -1660,
        -100
      ],
      "parameters": {
        "jsCode": "const text = $input.first().json.content;\nconst chunkSize = 1000;\n\nlet chunks = [];\nfor (let i = 0; i < text.length; i += chunkSize) {\n  chunks.push({\n    json: { chunk: text.slice(i, i + chunkSize) }\n  });\n}\n\nreturn chunks;\n\n"
      },
      "typeVersion": 2
    },
    {
      "id": "561cff53-365d-4d09-baf6-f1a864ec6c46",
      "name": "嵌入上传的文档",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -1440,
        -100
      ],
      "parameters": {
        "url": "https://api.together.xyz/v1/embeddings",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "sendHeaders": true,
        "authentication": "genericCredentialType",
        "bodyParameters": {
          "parameters": [
            {
              "name": "model",
              "value": "BAAI/bge-large-en-v1.5"
            },
            {
              "name": "input",
              "value": "={{ $json.chunk }}"
            }
          ]
        },
        "genericAuthType": "httpBearerAuth",
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        }
      },
      "credentials": {
        "httpBearerAuth": {
          "id": "2D3rpbElf45WbFsO",
          "name": "Together API"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "57488d21-d59b-4e1a-916c-e35409d796c4",
      "name": "将嵌入向量保存到数据库",
      "type": "n8n-nodes-base.supabase",
      "position": [
        -1220,
        -100
      ],
      "parameters": {
        "tableId": "embed",
        "fieldsUi": {
          "fieldValues": [
            {
              "fieldId": "chunk",
              "fieldValue": "={{ $('Splitting into Chunks').item.json.chunk }}"
            },
            {
              "fieldId": "embedding",
              "fieldValue": "={{ JSON.stringify($json.data[0].embedding) }}"
            }
          ]
        }
      },
      "credentials": {
        "supabaseApi": {
          "id": "YrlSoXTufsJxWo88",
          "name": "Supabase account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "a91cd4dd-eab7-468b-932e-3fd7122bd5f5",
      "name": "Telegram 触发器",
      "type": "n8n-nodes-base.telegramTrigger",
      "disabled": true,
      "position": [
        -2160,
        -100
      ],
      "webhookId": "d5337a19-baec-4329-8a33-b5f5406cb155",
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "id": "TF7r8Ffn9dp8LYyc",
          "name": "Telegram account"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "f9bd510a-1894-4f03-abb1-b1aa9fa007e1",
      "name": "当收到聊天消息时",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        -300,
        -140
      ],
      "webhookId": "705cc3e9-1972-47cf-832f-951868b0ec99",
      "parameters": {
        "public": true,
        "options": {},
        "initialMessages": "Hi there! 👋\nTest the basic RAG chat with Supabase "
      },
      "typeVersion": 1.1
    },
    {
      "id": "d4be0adb-e8ad-4ce8-aaaa-2bb957da47ce",
      "name": "聚合",
      "type": "n8n-nodes-base.aggregate",
      "position": [
        360,
        -140
      ],
      "parameters": {
        "options": {},
        "fieldsToAggregate": {
          "fieldToAggregate": [
            {
              "fieldToAggregate": "chunk"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "2db8d4ed-e32a-449e-ae2b-68399c552ea4",
      "name": "基础 LLM 链",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "position": [
        680,
        -140
      ],
      "parameters": {
        "text": "=You are an AI assistant. Use the following context to answer the user's question.\n\nContext:\n{{ $json.chunk }}\n\nUser's message:\n{{ $('When chat message received').item.json.chatInput }}\n\nProvide a helpful and detailed answer based *only* on the context. If the answer is not in the context, say \"I don't know based on the provided information.\"\n",
        "batching": {},
        "promptType": "define"
      },
      "typeVersion": 1.7
    },
    {
      "id": "db2ba386-1a3b-4d46-abc3-c53f98323ca4",
      "name": "OpenRouter 聊天模型",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenRouter",
      "position": [
        680,
        60
      ],
      "parameters": {
        "model": "qwen/qwen3-8b:free",
        "options": {}
      },
      "credentials": {
        "openRouterApi": {
          "id": "FFuwU8AOeIl1dxRW",
          "name": "OpenRouter account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "ed1849e9-fb30-4763-a6bf-2ba8c6c243f7",
      "name": "搜索嵌入向量",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        140,
        -140
      ],
      "parameters": {
        "url": "https://fqdttboovkcezrxizkpw.supabase.co/rest/v1/rpc/matchembeddings1",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "authentication": "predefinedCredentialType",
        "bodyParameters": {
          "parameters": [
            {
              "name": "=query_embedding",
              "value": "={{ $json.data[0].embedding }}"
            },
            {
              "name": "match_count",
              "value": "5"
            }
          ]
        },
        "nodeCredentialType": "supabaseApi"
      },
      "credentials": {
        "supabaseApi": {
          "id": "YrlSoXTufsJxWo88",
          "name": "Supabase account"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "e5966ced-618c-4fbf-85c9-d8fd8bf0576b",
      "name": "嵌入用户消息",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -80,
        -140
      ],
      "parameters": {
        "url": "https://api.together.xyz/v1/embeddings",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "authentication": "genericCredentialType",
        "bodyParameters": {
          "parameters": [
            {
              "name": "model",
              "value": "BAAI/bge-large-en-v1.5"
            },
            {
              "name": "input",
              "value": "={{ $json.chatInput }}"
            }
          ]
        },
        "genericAuthType": "httpBearerAuth"
      },
      "credentials": {
        "httpBearerAuth": {
          "id": "2D3rpbElf45WbFsO",
          "name": "Together API"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "3aed0aa2-25ab-4f5e-b6d1-fe536bce5ef3",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2900,
        -300
      ],
      "parameters": {
        "width": 1900,
        "height": 1080,
        "content": "## ⚠️ 首先运行此步骤且仅运行一次"
      },
      "typeVersion": 1
    },
    {
      "id": "6afc7ab6-18d2-4781-bfc5-a0ae03a9ef68",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -900,
        -300
      ],
      "parameters": {
        "width": 1900,
        "height": 1080,
        "content": "## 💬 当收到聊天消息时"
      },
      "typeVersion": 1
    }
  ],
  "pinData": {},
  "connections": {
    "Aggregate": {
      "main": [
        [
          {
            "node": "Basic LLM Chain",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Content for the Training",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search Embeddings": {
      "main": [
        [
          {
            "node": "Aggregate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Embend User Message": {
      "main": [
        [
          {
            "node": "Search Embeddings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenRouter Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Basic LLM Chain",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Splitting into Chunks": {
      "main": [
        [
          {
            "node": "Embedding Uploaded document",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Content for the Training": {
      "main": [
        [
          {
            "node": "Splitting into Chunks",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When chat message received": {
      "main": [
        [
          {
            "node": "Embend User Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Embedding Uploaded document": {
      "main": [
        [
          {
            "node": "Save the embedding in DB",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

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

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

中级 - 内部知识库, AI RAG 检索增强

需要付费吗?

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

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

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

外部链接
在 n8n.io 查看

分享此工作流