2、n8n 构建你的第一个 AI Agent

工作原理

此模板启动您的第一个AI Agent------一个不仅会聊天,还能使用工具执行操作的 AI 驱动的聊天机器人。

将 AI Agent 视为一个智能助手,而工具则是它手机上的应用程序。通过将其与其他节点连接,你赋予你的助手与真实世界数据和服务的交互能力,例如查看天气、获取新闻,甚至代表你发送电子邮件。

这个工作流程旨在成为完美的起点:

  • 聊天界面Chat Trigger 节点为你提供了一个简单、干净的界面,让你与你的助手交谈。

  • 核心AI Agent 节点接收你的消息,智能地决定使用哪个工具(如果有的话),并制定有帮助的回复。它的个性和指示可以在"系统消息"中完全自定义。

  • 语言模型 :它使用 Google Gemini 来驱动其推理和对话能力。

  • 工具:它预装了两个工具来展示其功能:

    1. 获取天气:获取实时天气预报。

    2. 获取新闻:读取任何 RSS 源以获取最新头条。

  • 记忆 :一个 Conversation Memory 节点允许代理记住最后几条消息,从而实现自然、连贯的对话。

节点说明

AI Agent

  1. 选择人工智能 -> AI Agent

  2. Agent设置:Add Option -> System Message

  3. System Message

js 复制代码
<role>  
你是 n8n Demo AI Agent,一个友好且乐于助人的助手,旨在展示 n8n 自动化平台中 AI 代理的强大功能。你的个性鼓励性强,带有一定的教育性质,并对自动化充满热情。你的主要功能是通过使用可用工具回答用户问题和满足请求,来演示你的能力。你是一个善于对话的助手。  
</role>  

<instructions>  
<goal>  
你的主要目标是作为基于 n8n 构建的 AI 代理的实时演示。你将与用户互动,智能地利用可用工具回答问题,并解释 AI 代理背后的概念,帮助用户理解其潜力。  
</goal>  

<context>  
### How I Work  
我是一款运行在简单 n8n 工作流中的 AI 模型。该工作流为我提供两个关键能力:  
1. **一组工具:** 这些是我可以调用以获取信息或执行操作的功能。  
2. **简单记忆:** 我可以记住当前对话的近期内容,以理解上下文。  

### My Purpose

我的主要目的是作为一个展示案例。我演示如何通过聊天界面连接各种功能(即我的工具),无需复杂的用户界面。这是让强大自动化通过简单对话触手可及的好方法。

### My Tools Instructions

如果用户请求符合某个工具的功能,你必须选择并调用该工具。你不能自行完成这些操作,必须通过调用工具实现。

### About AI Agents in n8n

* **可靠性:** 虽然我能有效地一次使用一个工具,但更高级的代理可以执行多步骤任务。然而,对于"复杂且关键的流程",通常更可靠的做法是在 n8n 中构建结构化、分步的工作流,而不是单纯依赖代理的推理能力。代理非常适合面向用户的交互,但结构化工作流才是后台可靠性的王道。
* **最佳实践:** 好的实践是保持代理的工具集聚焦,通常控制在10到15个工具以内,以确保可靠性并防止混淆。

### Current Date & Time

{{ $now }} 
</context>

<output_format>

* 以友好、对话式且乐于助人的语气回应。
* 当用户请求需要工具时,先选择合适的工具,然后将工具执行结果以清晰易懂的方式呈现给用户。
* 要主动,如果用户不确定该做什么,可以建议一些基于你工具能力的示例问题(例如,介绍你的工具和你自己的能力)。
</output_format>

</instructions>

Chat Model

  1. 给Chat Model添加节点,可选择范围如下,这里我们使用本地的Ollama Chat Model

  2. Ollama Chat Model配置如下

  • 连接凭证:下拉选择或添加即可

  • Model选择,建议使用红框Modle(xxx:xb可不同),deepseek不支持tools

Memory

选择Simple Memory ,配置如下图

Tool

Get Weather

  1. 添加HTTP Request Tool节点

  2. 如下图1,设置工具描述,告诉大模型这是什么工具,应该怎么使用

  3. 下图2,工具请求的URL

  4. 下图3,参数,Value使用y由模型自动定义 ,描述里告诉大模型应该怎么赋值

Get News

  1. 添加RSS Read节点
  2. 如下图1,设置工具描述,告诉大模型这是什么工具,应该怎么使用
  3. 下图2,工具应该请求的URL

开始聊天!

  • 前往Example Chat节点。
  • 点击其参数面板中的打开聊天按钮。
  • 试着问它一个示例问题,比如:"巴黎的天气怎么样?"或者"给我最新的科技新闻。"

其他

引用地址:n8n.io/workflows/6...

代码如下,可保存到文件里,然后导入到画布。

json 复制代码
{
  "name": "2、Build Your First AI Agent by Anthony",
  "nodes": [
    {
      "parameters": {
        "content": "## 试试看吧!\n\n**启动您的第一个AI智能体------一个能使用工具获取实时信息、发送邮件并自动执行任务的聊天机器人。**\n\n### 开始步骤:\n1. **连接Gemini**(参见下方红色便签提示)\n2. 点击 **🗨 开启聊天** 按钮,尝试提问:\n* \"巴黎天气如何?\"\n* \"获取最新科技新闻\"\n* \"给我一些n8n AI智能体的创意建议\"",
        "height": 300,
        "width": 392
      },
      "id": "677566cc-b4be-492c-bde1-163f50382e90",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Introduction Note",
      "type": "n8n-nodes-base.stickyNote",
      "creator": "Lucas Peyrin",
      "position": [
        -944,
        -352
      ],
      "typeVersion": 1,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "content": "💡 稍后激活此工作流并分享公开聊天链接,让其他人也能与您的AI智能体对话!",
        "height": 252,
        "width": 300,
        "color": 7
      },
      "id": "a79a8134-a175-4de2-9b6d-3cf8c661516e",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Sticky Note12",
      "type": "n8n-nodes-base.stickyNote",
      "creator": "Lucas Peyrin",
      "position": [
        -464,
        -256
      ],
      "typeVersion": 1,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "content": "您的AI智能体能够:  \n1. **接收** 来自聊天的消息  \n2. **选择** 合适工具(例如天气查询、新闻获取、邮件发送)  \n3. **响应** 实时有效的答案  \n\n**打开AI智能体节点**,编辑 **系统提示语** 以调整智能体的思维模式、行为逻辑与回复方式。  ",
        "height": 348,
        "width": 396,
        "color": 7
      },
      "id": "2cf20608-4b2e-4a9c-b21a-74bd2909b1c7",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Sticky Note13",
      "type": "n8n-nodes-base.stickyNote",
      "creator": "Lucas Peyrin",
      "position": [
        -80,
        -352
      ],
      "typeVersion": 1,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "content": "\n\n\n\n\n\n\n\n\n\n\n\n\n\n该节点可帮助您的智能体记录最近几条消息,以维持对话主题的连贯性。",
        "height": 244,
        "width": 308,
        "color": 7
      },
      "id": "20336c82-3171-4e2e-b65b-40249dde09dd",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Sticky Note15",
      "type": "n8n-nodes-base.stickyNote",
      "creator": "Lucas Peyrin",
      "position": [
        -80,
        80
      ],
      "typeVersion": 1,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "content": "\n\n\n\n\n\n\n\n\n\n\n\n\n这些工具让您的智能体能够访问真实世界数据或执行操作。添加更多工具以扩展其能力!  \n\n点击智能体 **工具输入框下方 ➕ 添加**:  \n- Google Calendar(获取即将到来的日程)  \n- Gmail(发送邮件)(Gmail API)  ",
        "height": 308,
        "width": 372,
        "color": 7
      },
      "id": "7293d8ea-4338-4569-986f-4b8f5ee9109d",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Sticky Note16",
      "type": "n8n-nodes-base.stickyNote",
      "creator": "Lucas Peyrin",
      "position": [
        320,
        80
      ],
      "typeVersion": 1,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "toolDescription": "Gets the latest blog posts about any rss feed.",
        "url": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('URL', `Use one of:\n- https://feeds.bbci.co.uk/news/world/rss.xml (BBC World -- global headlines)\n- https://www.aljazeera.com/xml/rss/all.xml (Al Jazeera English -- in‑depth global coverage)\n- http://rss.cnn.com/rss/edition_world.rss (CNN World -- breaking news worldwide)\n- https://techcrunch.com/feed/ (TechCrunch -- global tech & startup news)\n- http://news.ycombinator.com/rss (Hacker News -- tech community headlines)\n- https://n8n.io/blog/rss (n8n Blog -- updates & tutorials)\n- https://www.bonappetit.com/feed/recipes-rss-feed/rss (Bon Appétit -- recent recipes list)\n- https://www.endsreport.com/rss/news-and-analysis (ENDS Report -- environmental law & policy news)\n- https://medlineplus.gov/groupfeeds/new.xml (MedlinePlus -- health topics & wellness updates)`, 'string') }}",
        "options": {}
      },
      "id": "211a7ffe-db4a-4404-8d72-be2875c17b63",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Get News",
      "type": "n8n-nodes-base.rssFeedReadTool",
      "creator": "Lucas Peyrin",
      "position": [
        464,
        128
      ],
      "typeVersion": 1.2,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "toolDescription": "Get weather forecast anywhere, anytime. You can make requests by assuming most information, the only thing you need is the location (use the city name to infer lat and long automatically) and time period (assume today if not specified)",
        "url": "https://api.open-meteo.com/v1/forecast",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "latitude",
              "value": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('parameters0_Value', `Latitude of the location, e.g. 45.75 for Lyon. Do not ask the user just infer it automatically.`, 'string') }}"
            },
            {
              "name": "longitude",
              "value": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('parameters1_Value', `Longitude of the location, e.g. 4.85 for Lyon. Do not ask the user just infer it automatically.`, 'string') }}"
            },
            {
              "name": "current",
              "value": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('parameters2_Value', `Comma-separated list of current weather variables (no whitespace).\n\nExample: temperature_2m,windspeed_10m,rain.\n\nOptions: temperature_2m,relative_humidity_2m,apparent_temperature,is_day,precipitation,rain,showers,snowfall,weathercode,cloudcover_total,pressure_msl,surface_pressure,windspeed_10m,winddirection_10m,windgusts_10m.`, 'string') }}"
            },
            {
              "name": "hourly",
              "value": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('parameters3_Value', `Comma-separated list of hourly weather variables (no whitespace). Hourly is only useful to get one day's information. For weakly overview please use daily.\n\nExample: temperature_2m,precipitation.\n\nOptions: temperature_2m,relative_humidity_2m,dewpoint_2m,apparent_temperature,precipitation,rain,showers,snowfall,snow_depth,pressure_msl,surface_pressure,cloudcover_total,cloudcover_low,cloudcover_mid,cloudcover_high,windspeed_10m,winddirection_10m,windgusts_10m,visibility,is_day,sunshine_duration,soil_temperature,soil_moisture,PM10,PM2_5,carbon_monoxide,ozone,us_aqi,UV_index.`, 'string') }}"
            },
            {
              "name": "daily",
              "value": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('parameters4_Value', `Comma-separated list of daily weather variables (no whitespace).\n\nExample: temperature_2m_max,precipitation_sum.\n\nOptions: weathercode,temperature_2m_max,temperature_2m_min,apparent_temperature_max,apparent_temperature_min,precipitation_sum,rain_sum,showers_sum,snowfall_sum,precipitation_hours,sunrise,sunset,daylight_duration,sunshine_duration,pressure_msl_max,pressure_msl_min,surface_pressure_max,surface_pressure_min,windgusts_10m_max,windspeed_10m_max,winddirection_10m_dominant,shortwave_radiation_sum.`, 'string') }}"
            },
            {
              "name": "start_date",
              "value": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('parameters5_Value', `Start date in YYYY-MM-DD format. Example: 2025-07-15`, 'string') }}"
            },
            {
              "name": "end_date",
              "value": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('parameters6_Value', `End date in YYYY-MM-DD format. Must be after start_date. Example: 2025-07-18`, 'string') }}"
            },
            {
              "name": "temperature_unit",
              "value": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('parameters7_Value', `Unit for temperature. Options: celsius (default), fahrenheit.`, 'string') }}"
            }
          ]
        },
        "options": {}
      },
      "id": "4a2efe27-85f9-4b97-8537-0600e90a7345",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Get Weather",
      "type": "n8n-nodes-base.httpRequestTool",
      "creator": "Lucas Peyrin",
      "position": [
        352,
        128
      ],
      "notesInFlow": true,
      "typeVersion": 4.2
    },
    {
      "parameters": {
        "options": {
          "systemMessage": "=<role>\nYou are the n8n Demo AI Agent, a friendly and helpful assistant designed to showcase the power of AI agents within the n8n automation platform. Your personality is encouraging, slightly educational, and enthusiastic about automation. Your primary function is to demonstrate your capabilities by using your available tools to answer user questions and fulfill their requests. You are conversational.\n</role>\n\n<instructions>\n<goal>\nYour primary goal is to act as a live demonstration of an AI Agent built with n8n. You will interact with users, answer their questions by intelligently using your available tools, and explain the concepts behind AI agents to help them understand their potential.\n</goal>\n\n<context>\n### How I Work\nI am an AI model operating within a simple n8n workflow. This workflow gives me two key things:\n1.  **A set of tools:** These are functions I can call to get information or perform actions.\n2.  **Simple Memory:** I can remember the immediate past of our current conversation to understand context.\n\n### My Purpose\nMy main purpose is to be a showcase. I demonstrate how you can give a chat interface to various functions (my tools) without needing complex UIs. This is a great way to make powerful automations accessible to anyone through simple conversation.\n\n### My Tools Instructions\nYou must choose one of your available tools if the user's request matches its capability. You cannot perform these actions yourself; you must call the tool.\n\n### About AI Agents in n8n\n- **Reliability:** While I can use one tool at a time effectively, more advanced agents can perform multi-step tasks. However, for `complex, mission-critical processes, it's often more reliable to build structured, step-by-step workflows in n8n rather than relying solely on an agent's reasoning. Agents are fantastic for user-facing interactions, but structured workflows are king for backend reliability.\n- **Best Practices:** A good practice is to keep an agent's toolset focused, typically under 10-15 tools, to ensure reliability and prevent confusion.\n\n### Current Date & Time\n{{ $now }}\n</context>\n\n<output_format>\n- Respond in a friendly, conversational, and helpful tone.\n- When a user's request requires a tool, first select the appropriate tool. Then, present the result of the tool's execution to the user in a clear and understandable way.\n- Be proactive. If the user is unsure what to do, suggest some examples of what they can ask you based on your available tools (e.g., Talk about your tools and what you know about yourself).\n</output_format>\n</instructions>"
        }
      },
      "id": "fcb982e6-2978-46a9-a251-26e01379cf22",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Your First AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "creator": "Lucas Peyrin",
      "position": [
        0,
        -160
      ],
      "typeVersion": 2.2,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "public": true,
        "initialMessages": "Hi there! 👋",
        "options": {
          "inputPlaceholder": "Type your message here...",
          "showWelcomeScreen": false,
          "subtitle": "This is for demo purposes. Try me out !",
          "title": "Your first AI Agent 🚀",
          "responseMode": "lastNode"
        }
      },
      "id": "1b5a445e-1f78-4ec2-9687-68a2ac3ed9a1",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Example Chat",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "creator": "Lucas Peyrin",
      "position": [
        -368,
        -160
      ],
      "webhookId": "e5616171-e3b5-4c39-81d4-67409f9fa60a",
      "typeVersion": 1.1,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "contextWindowLength": 30
      },
      "id": "ed9d660e-7c93-4a8e-8b60-5d2888a74e74",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Conversation Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "creator": "Lucas Peyrin",
      "position": [
        32,
        128
      ],
      "typeVersion": 1.3,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "content": "",
        "height": 188,
        "width": 294,
        "color": 3
      },
      "id": "83dab378-ba5d-4cb0-acec-4a91e4f6821c",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "creator": "Lucas Peyrin",
      "position": [
        -448,
        80
      ],
      "typeVersion": 1,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "model": "gpt-oss:20b",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOllama",
      "typeVersion": 1,
      "position": [
        -336,
        128
      ],
      "id": "cae7440d-041e-4e08-ac46-def5277da7a9",
      "name": "Ollama Chat Model",
      "credentials": {
        "ollamaApi": {
          "id": "m6eGn5Rg96UKQJr2",
          "name": "Ollama account"
        }
      }
    }
  ],
  "pinData": {},
  "connections": {
    "Get News": {
      "ai_tool": [
        [
          {
            "node": "Your First AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Get Weather": {
      "ai_tool": [
        [
          {
            "node": "Your First AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Example Chat": {
      "main": [
        [
          {
            "node": "Your First AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Conversation Memory": {
      "ai_memory": [
        [
          {
            "node": "Your First AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Your First AI Agent": {
      "main": [
        []
      ]
    },
    "Ollama Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Your First AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "354d6e33-a568-4953-8536-2f4f0a9890c1",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "54c9e2e31753f3c874da48cdbf9552e23a88fbc2e067ac67b23af8418a890097"
  },
  "id": "keIhEphiZHfVJME4",
  "tags": []
}
相关推荐
江湖十年10 小时前
Google ADK + DeepSeek 快速构建 Agent
llm·agent·ai编程
机器之心11 小时前
Agent狂欢下的冷思考:为什么说Data&AI数据基础设施,才是AI时代Infra新范式
人工智能·openai
AI大模型13 小时前
超火爆,星标 28.2 k 的一个开源特权账号管理 (PAM) 工具
程序员·llm·agent
AI大模型13 小时前
星标 17.4 k 开源的企业级智能平台,开箱即用,超级便捷
程序员·llm·agent
老顾聊技术13 小时前
老顾深度解析【字节跳动的AI项目DeerFlow】源码(一)
agent
新智元13 小时前
奥特曼公然叫板马斯克!重金杀入脑机接口,硅谷两大巨头彻底决裂
人工智能·openai
新智元15 小时前
蛇吞象!180亿身价,345亿出价:Perplexity硬抢谷歌Chrome浏览器
人工智能·openai
机器之心15 小时前
OpenAI没开源的gpt-oss基础模型,他去掉强化学习逆转出来了
人工智能·openai