掘金 人工智能 16小时前
2、n8n 构建你的第一个 AI Agent
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文介绍了如何构建你的第一个AI Agent,它不仅能进行对话,还能调用工具执行实际操作。通过将AI Agent与天气查询、新闻获取等工具连接,并利用记忆功能增强对话连贯性,可以创建一个强大的自动化助手。文章强调了AI Agent在用户交互方面的优势,以及在复杂流程中结合结构化工作流的重要性,为理解和应用AI Agent提供了清晰的入门指南。

✨ **AI Agent的工作原理**:AI Agent是驱动的聊天机器人,能够使用工具执行操作,如同手机上的应用程序。通过与其他节点连接,它可以与真实世界的数据和服务进行交互,例如获取天气信息或发送电子邮件。

🔧 **核心组件与功能**:一个典型的AI Agent工作流程包含一个聊天触发器(Chat Trigger)提供交互界面,一个AI Agent节点作为核心处理消息并调用工具,以及一个语言模型(如Google Gemini)驱动其推理能力。记忆节点(Conversation Memory)则能让Agent记住对话历史,保持连贯性。

💡 **工具的集成与应用**:AI Agent能够调用预设的工具来完成任务,例如“获取天气”和“获取新闻”。当用户提出相关请求时,Agent会智能地选择并调用相应的工具,然后以清晰易懂的方式将工具执行结果呈现给用户。

🚀 **AI Agent在n8n中的优势与最佳实践**:AI Agent非常适合面向用户的交互,能够通过简单的对话连接各种功能。但对于复杂和关键的流程,建议在n8n中构建结构化的工作流以确保可靠性。保持Agent的工具集聚焦(10-15个以内)是确保其可靠性的最佳实践。

📚 **上手指南与示例**:文章提供了构建第一个AI Agent的详细步骤,包括配置节点、设置系统消息以及如何通过聊天界面与Agent互动。示例问题如“巴黎的天气怎么样?”或“给我最新的科技新闻。”,帮助用户快速体验AI Agent的能力。

工作原理

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

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

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

节点说明

AI Agent

    选择人工智能 -> AI Agent

    Agent设置:Add Option -> System Message

    System Message

<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

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

    Ollama Chat Model配置如下

Memory

选择Simple Memory,配置如下图

Tool

Get Weather

    添加HTTP Request Tool节点

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

    下图2,工具请求的URL

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

Get News

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

开始聊天!

其他

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

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

{  "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": []}

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

AI Agent n8n 自动化 工具集成 语言模型
相关文章