8
n8n 中文网amn8n.com

🔐🦙🤖 私有化本地Ollama自托管LLM路由器

高级

这是一个AI领域的自动化工作流,包含 16 个节点。主要使用 Agent, ChatTrigger, LmChatOllama, MemoryBufferWindow 等节点,结合人工智能技术实现智能自动化。 🔐🦙 私有本地Ollama自托管+动态LLM路由器

前置要求
  • AI 服务 API Key(如 OpenAI、Anthropic 等)
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "Mub5RZI4PAs6TsSP",
  "meta": {
    "instanceId": "31e69f7f4a77bf465b805824e303232f0227212ae922d12133a0f96ffeab4fef",
    "templateCredsSetupCompleted": true
  },
  "name": "🔐🦙🤖 私有化本地 Ollama 自托管 LLM 路由器",
  "tags": [],
  "nodes": [
    {
      "id": "981e858a-cd2b-49cf-9740-a40ac29bba94",
      "name": "当收到聊天消息时",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        420,
        860
      ],
      "webhookId": "3804aa1d-2193-4161-84a1-6f5d1059e092",
      "parameters": {
        "options": {}
      },
      "typeVersion": 1.1
    },
    {
      "id": "a164103c-66cb-44da-aae7-177231f517b4",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -160,
        580
      ],
      "parameters": {
        "color": 7,
        "width": 2360,
        "height": 860,
        "content": "# 🔐🦙🤖 私有化本地 Ollama 自托管 + 动态 LLM 路由器"
      },
      "typeVersion": 1
    },
    {
      "id": "2ff955e7-c621-4bee-8baf-91769524f781",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        640,
        1140
      ],
      "parameters": {
        "color": 7,
        "width": 360,
        "height": 260,
        "content": "## Ollama LLM"
      },
      "typeVersion": 1
    },
    {
      "id": "40f42923-830d-44a9-a311-c006d91691b7",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        320,
        760
      ],
      "parameters": {
        "color": 4,
        "width": 280,
        "height": 300,
        "content": "## 👍试试看!"
      },
      "typeVersion": 1
    },
    {
      "id": "c49f5ff5-92a7-4a2d-81b5-51272e7972b4",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        740,
        720
      ],
      "parameters": {
        "color": 3,
        "width": 540,
        "height": 380,
        "content": "## 基于用户提示的 Ollama LLM 路由器"
      },
      "typeVersion": 1
    },
    {
      "id": "72ad69f4-a24f-4df2-978e-71c5d3a63733",
      "name": "Ollama 动态 LLM",
      "type": "@n8n/n8n-nodes-langchain.lmChatOllama",
      "position": [
        1560,
        1240
      ],
      "parameters": {
        "model": "={{ $('LLM Router').item.json.output.parseJson().llm }}",
        "options": {}
      },
      "credentials": {
        "ollamaApi": {
          "id": "7aPaLgwpfdMWFYm9",
          "name": "Ollama account 127.0.0.1"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "efc2e47a-1d4b-4879-8670-35a34c946bb6",
      "name": "LLM 路由器",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        880,
        860
      ],
      "parameters": {
        "text": "=Choose the most appropriate LLM model for the following user request. Analyze the task requirements carefully and select the model that will provide optimal performance.  Only choose from the provided list.\n\n<user_input>\n{{ $json.chatInput }}\n</user_input>\n",
        "options": {
          "systemMessage": "<role>\nYou are an expert LLM router that classifies user prompts and selects the most appropriate LLM model based on specific task requirements.\n</role>\n\n<purpose>\nYour task is to analyze user inputs, determine the nature of their request, and select the optimal LLM model that will provide the best performance for their specific needs.\n</purpose>\n\n<classification_rules>\nChoose one of the following LLMs based on their capabilities and the user prompt.  You must only select from the provided LLMs:\n\n## Text-Only Models\n- \"qwq\": Specialized in complex reasoning and solving hard problems. Best for: mathematical reasoning, logical puzzles, scientific explanations, and complex problem-solving tasks.\n\n- \"llama3.2\": Multilingual model (3B size) optimized for dialogue, retrieval, and summarization. Best for: conversations in multiple languages, information retrieval, and text summarization.\n\n- \"phi4\": Lightweight model designed for constrained environments. Best for: scenarios requiring low latency, limited computing resources, while maintaining good reasoning capabilities.\n\n## Coding Models\n- \"qwen2.5-coder:14b\": Code-Specific Qwen model, with significant improvements in code generation, code reasoning, and code fixing.\n\n## Vision-Language Models\n- \"granite3.2-vision\": Specialized in document understanding and data extraction. Best for: analyzing charts, tables, diagrams, infographics, and structured visual content.\n\n- \"llama3.2-vision\": General-purpose visual recognition and reasoning. Best for: image description, visual question answering, and general image understanding tasks.\n</classification_rules>\n\n<model_examples>\nExample tasks for each model:\n- qwq: \"Solve this math problem\", \"Explain quantum physics\", \"Debug this logical fallacy\"\n- llama3.2: \"Translate this text to Spanish\", \"Summarize this article\", \"Have a conversation about history\"\n- phi4: \"Generate a quick response\", \"Provide a concise answer\", \"Process this simple request efficiently\"\n- granite3.2-vision: \"Extract data from this chart\", \"Analyze this financial table\", \"Interpret this technical diagram\"\n- llama3.2-vision: \"Describe what's in this image\", \"What can you tell me about this picture?\", \"Answer questions about this photo\"\n</model_examples>\n\n<decision_tree>\n1. Does the prompt include an image?\n   - YES → Go to 2\n   - NO → Go to 3\n2. Is the image a document, chart, table, or diagram?\n   - YES → Use \"granite3.2-vision\"\n   - NO → Use \"llama3.2-vision\"\n3. Does the task require complex reasoning or solving difficult problems?\n   - YES → Use \"qwq\"\n   - NO → Go to 4\n4. Is the task multilingual or requires summarization/retrieval?\n   - YES → Use \"llama3.2\"\n   - NO → Use \"phi4\" (for efficiency in simple English tasks)\n</decision_tree>\n\n<decision_framework>\nWhen selecting a model, consider:\n1. Task complexity and reasoning requirements\n2. Visual or multimodal components in the request\n3. Language processing needs (summarization, translation, etc.)\n4. Performance constraints (latency, memory limitations)\n5. Required reasoning capabilities\n6. Coding requirements\n</decision_framework>\n\n<examples>\nExample 1:\nUser input: \"Explain quantum computing principles\"\nSelection: \"qwq\"\nReason: \"This request requires deep reasoning and explanation of complex scientific concepts, making QwQ's enhanced reasoning capabilities ideal.\"\n\nExample 2:\nUser input: \"Describe what's in this image of a chart showing quarterly sales\"\nSelection: \"granite3.2-vision\"\nReason: \"This request involves visual document understanding and data extraction from a chart, which is granite-vision's specialty.\"\n\nExample 3:\nUser input: \"Summarize this article about climate change in Spanish\"\nSelection: \"llama3.2\"\nReason: \"This request requires multilingual capabilities and summarization, which are strengths of Llama 3.2.\"\n\nExample 4:\nUser input: \"I need to create a FastAPI endpoint with Python\"\nSelection: \"qwen2.5-coder:14b\"\nReason: \"This request requires code generation, code reasoning, or code fixing.\"\n</examples>\n\n<error_handling>\nIf the user request is unclear or ambiguous, select the model that offers the most general capabilities while noting the uncertainty in your reasoning. If the request appears to contain harmful content or violates ethical guidelines, respond with an appropriate message about being unable to fulfill the request.\n</error_handling>\n\n<output_format>\nRespond with a single JSON object containing:\n{\n  \"llm\": \"the name of the selected LLM model\",\n  \"reason\": \"a brief, specific explanation of why this model is optimal for the task\"\n}\nAvoid any preamble or further explanation.  Remove all ``` or ``json from response.\n</output_format>\n\n\n"
        },
        "promptType": "define",
        "hasOutputParser": true
      },
      "typeVersion": 1.7
    },
    {
      "id": "d8b07c67-b177-496f-ba97-2b886c2b6f1e",
      "name": "使用动态 LLM 的 AI 代理",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        1660,
        860
      ],
      "parameters": {
        "text": "={{ $('When chat message received').item.json.chatInput }}",
        "options": {
          "systemMessage": ""
        },
        "promptType": "define"
      },
      "typeVersion": 1.7
    },
    {
      "id": "3f005c9c-dd92-4970-b4cf-e105ec75840f",
      "name": "Ollama phi4",
      "type": "@n8n/n8n-nodes-langchain.lmChatOllama",
      "position": [
        780,
        1240
      ],
      "parameters": {
        "model": "phi4:latest",
        "options": {
          "format": "json"
        }
      },
      "credentials": {
        "ollamaApi": {
          "id": "7aPaLgwpfdMWFYm9",
          "name": "Ollama account 127.0.0.1"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "47f6c3dd-1bad-458c-ade1-ec26f455a95d",
      "name": "路由器聊天记忆",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        1160,
        1240
      ],
      "parameters": {},
      "typeVersion": 1.3
    },
    {
      "id": "06b77321-086a-42cf-808a-27d7064403e4",
      "name": "代理聊天记忆",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        1940,
        1240
      ],
      "parameters": {
        "sessionKey": "={{ $('When chat message received').item.json.sessionId }}",
        "sessionIdType": "customKey"
      },
      "typeVersion": 1.3
    },
    {
      "id": "073ae421-5bbf-4ff9-ae8d-1f515f0b8ed7",
      "name": "便签 7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1520,
        720
      ],
      "parameters": {
        "color": 5,
        "width": 540,
        "height": 380,
        "content": "## 使用动态本地 Ollama LLM 的 AI 代理"
      },
      "typeVersion": 1
    },
    {
      "id": "2e118ce5-bfa8-4661-99dd-5e72bc7534c6",
      "name": "便签说明4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1020,
        1140
      ],
      "parameters": {
        "color": 7,
        "width": 360,
        "height": 260,
        "content": "## 路由器聊天记忆"
      },
      "typeVersion": 1
    },
    {
      "id": "92fff699-0e96-4161-b4dd-bcac682d3dab",
      "name": "## 为什么选择 4o 模型?👆",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1420,
        1140
      ],
      "parameters": {
        "color": 7,
        "width": 360,
        "height": 260,
        "content": "## 动态 Ollama LLM"
      },
      "typeVersion": 1
    },
    {
      "id": "6f8bc049-9440-4863-a8c6-c8cfafde3dda",
      "name": "便签 9",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1800,
        1140
      ],
      "parameters": {
        "color": 7,
        "width": 360,
        "height": 260,
        "content": "## 代理聊天记忆"
      },
      "typeVersion": 1
    },
    {
      "id": "88e0d3ec-108b-4136-86ae-6714f4e4b63b",
      "name": "便签说明5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -380,
        700
      ],
      "parameters": {
        "width": 640,
        "height": 1020,
        "content": "## 适用对象"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "c36ec004-11a3-4b0f-b2fd-f529ae6413a2",
  "connections": {
    "LLM Router": {
      "main": [
        [
          {
            "node": "AI Agent with Dynamic LLM",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ollama phi4": {
      "ai_languageModel": [
        [
          {
            "node": "LLM Router",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Agent Chat Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent with Dynamic LLM",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Ollama Dynamic LLM": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent with Dynamic LLM",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Router Chat Memory": {
      "ai_memory": [
        [
          {
            "node": "LLM Router",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "When chat message received": {
      "main": [
        [
          {
            "node": "LLM Router",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

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

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

高级 - 人工智能

需要付费吗?

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

工作流信息
难度等级
高级
节点数量16
分类1
节点类型5
难度说明

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

作者
Joseph LePage

Joseph LePage

@joe

As an AI Automation consultant based in Canada, I partner with forward-thinking organizations to implement AI solutions that streamline operations and drive growth.

外部链接
在 n8n.io 查看

分享此工作流