本文档介绍了 n8n 的 AI 工具集成系统,该系统可以将普通的 n8n 节点转换为可供 LangChain 代理和其他 AI 工作流使用的 AI 工具。该系统允许标记有 usableAsTool 的节点自动转换为结构化工具,其动态模式基于参数中的 $fromAI 函数调用。
有关更广泛的节点系统架构信息,请参阅 节点系统。有关特定服务集成节点的信息,请参阅 服务集成节点。
AI 工具集成系统将 n8n 的工作流自动化能力与 AI 代理框架(主要是 LangChain)连接起来。它由三个主要组件组成:
$fromAI 函数调用来定义动态工具参数。来源: packages/cli/src/load-nodes-and-credentials.ts303-328 packages/core/src/execution-engine/node-execution-context/utils/create-node-as-tool.ts104-127
$fromAI 函数支持在节点配置中动态定义参数。当节点参数包含 $fromAI 调用时,AI 工具系统会提取它来为 AI 代理创建结构化模式。
$fromAI(key, description, type, defaultValue)
string、number、boolean、json)来源: packages/core/src/execution-engine/node-execution-context/utils/create-node-as-tool.ts29-89 packages/core/src/execution-engine/node-execution-context/utils/create-node-as-tool.ts37-72
节点使用其类型描述中的 usableAsTool 属性标记为与工具兼容。
来源: packages/cli/src/load-nodes-and-credentials.ts303-328 packages/cli/src/load-nodes-and-credentials.ts438-515
转换过程添加了用于 AI 工具功能的特定属性:
| 属性 | 目的 | 默认值 |
|---|---|---|
descriptionType | 自动或手动描述 | 'auto' |
toolDescription | AI 工具描述 | 节点描述 |
inputs | 输入连接 | [] (无输入) |
outputs | 输出连接 | [NodeConnectionTypes.AiTool] |
codex.categories | 工具分类 | ['AI'] |
codex.subcategories | 工具子分类 | {AI: ['Tools'], Tools: ['Other Tools']} |
来源: packages/cli/src/load-nodes-and-credentials.ts456-501
createNodeAsTool 函数生成一个 DynamicStructuredTool 实例。
来源: packages/core/src/execution-engine/node-execution-context/utils/create-node-as-tool.ts104-127
工具名称使用 nodeNameToToolName() 进行规范化,以确保兼容性。
来源: packages/core/src/execution-engine/node-execution-context/utils/create-node-as-tool.ts96-98 packages/@n8n/nodes-langchain/utils/helpers.ts258-260
logWrapper 函数为 AI 工具操作提供全面监控。
来源: packages/@n8n/nodes-langchain/utils/logWrapper.ts404-436 packages/@n8n/nodes-langchain/utils/logWrapper.ts33-73
getConnectedTools 函数处理工具发现和验证。
来源: packages/@n8n/nodes-langchain/utils/helpers.ts188-233
前端通过 useActionsGenerator 组合式函数为 AI 工具生成 UI 动作。
来源: packages/frontend/editor-ui/src/components/Node/NodeCreator/composables/useActionsGeneration.ts274-285 packages/frontend/editor-ui/src/components/Node/NodeCreator/composables/useActionsGeneration.ts111-139
AI 工具在节点创建器中自动分类。
| 类别 | 子类别 | 目的 |
|---|---|---|
AI | 工具 | 主要 AI 工具类别 |
工具 | 其他工具 | 默认子类别 |
工具 | 推荐要求 | 精选工具(如果指定) |