菜单

AI 工具集成

相关源文件

目的与范围

本文档介绍了 n8n 的 AI 工具集成系统,该系统可以将普通的 n8n 节点转换为可供 LangChain 代理和其他 AI 工作流使用的 AI 工具。该系统允许标记有 usableAsTool 的节点自动转换为结构化工具,其动态模式基于参数中的 $fromAI 函数调用。

有关更广泛的节点系统架构信息,请参阅 节点系统。有关特定服务集成节点的信息,请参阅 服务集成节点

AI 工具集成概述

AI 工具集成系统将 n8n 的工作流自动化能力与 AI 代理框架(主要是 LangChain)连接起来。它由三个主要组件组成:

  1. 参数模式生成:使用 $fromAI 函数调用来定义动态工具参数。
  2. 节点到工具的转换:将普通节点转换为具有适当描述和模式的 AI 工具。
  3. 工具执行环境:为 AI 工具调用提供日志记录、错误处理和执行上下文。

来源: packages/cli/src/load-nodes-and-credentials.ts303-328 packages/core/src/execution-engine/node-execution-context/utils/create-node-as-tool.ts104-127

The $fromAI System

$fromAI 函数支持在节点配置中动态定义参数。当节点参数包含 $fromAI 调用时,AI 工具系统会提取它来为 AI 代理创建结构化模式。

$fromAI 函数签名

$fromAI(key, description, type, defaultValue)
  • key: 参数标识符(1-64 个字符,字母数字,可包含下划线/连字符)
  • description: AI 代理的可读描述
  • type: 数据类型(stringnumberbooleanjson
  • defaultValue: 可选的默认值

参数提取过程

来源: 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'
toolDescriptionAI 工具描述节点描述
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

前端集成

AI 工具的动作生成

前端通过 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工具主要 AI 工具类别
工具其他工具默认子类别
工具推荐要求精选工具(如果指定)

来源: packages/cli/src/load-nodes-and-credentials.ts506-513