Skip to content

LangChain 代码节点#

使用 LangChain 代码节点可以导入 LangChain。这意味着即使 n8n 尚未为某些功能创建专用节点,您仍然可以使用这些功能。通过配置 LangChain 代码节点的连接器,您可以将其用作普通节点、根节点或子节点。

本页将介绍节点参数、配置指南以及相关资源链接。

云版本不可用

此节点仅在自托管的 n8n 中可用。

节点参数#

添加代码#

添加您的自定义代码。选择 执行(Execute)提供数据(Supply Data) 模式。您只能使用其中一种模式。

代码节点 不同,LangChain 代码节点不支持 Python。

  • 执行(Execute):将 LangChain 代码节点当作 n8n 自带的代码节点使用。该模式接收来自工作流的输入数据,处理后将其作为节点输出返回。此模式需要主输入和主输出连接。您必须在 输入(Inputs)输出(Outputs) 中创建这些连接。
  • 提供数据(Supply Data):将 LangChain 代码节点作为子节点使用,向根节点发送数据。此模式使用非主输出。

默认情况下,此节点无法加载内置或外部模块。自托管用户可以 启用内置和外部模块

输入#

选择输入类型。

主输入是所有 n8n 工作流中常见的标准连接器。如果节点中设置了主输入和主输出,则必须使用 执行(Execute) 代码。

输出#

选择输出类型。

主输出是所有 n8n 工作流中常见的标准连接器。如果节点中设置了主输入和主输出,则必须使用 执行(Execute) 代码。

节点输入输出配置#

通过配置 LangChain Code 节点的连接器(输入和输出),您可以将其用作应用节点、根节点或子节点。

包含四个 LangChain 节点的工作流截图,配置为不同的节点类型

节点类型 输入 输出 代码模式
应用节点。类似于 Code 节点 主输入 主输出 执行模式
根节点 主输入;至少一个其他类型 主输出 执行模式
子节点 - 非主类型。必须与您要连接的输入类型匹配。 数据供应模式
带子节点的子节点 非主类型 非主类型。必须与您要连接的输入类型匹配。 数据供应模式

内置方法#

n8n 提供以下方法,以便在 LangChain Code 节点中更轻松地执行常见任务。

方法 描述
this.addInputData(inputName, data) 向指定的非主输入填充数据。适用于模拟数据场景。
  • inputName 表示输入连接类型,必须是以下之一:ai_agent, ai_chain, ai_document, ai_embedding, ai_languageModel, ai_memory, ai_outputParser, ai_retriever, ai_textSplitter, ai_tool, ai_vectorRetriever, ai_vectorStore
  • data 包含要添加的数据。关于 n8n 预期的数据结构,请参考数据结构
this.addOutputData(outputName, data) 向指定的非主输出填充数据。适用于模拟数据场景。
  • outputName 表示输入连接类型,必须是以下之一:ai_agent, ai_chain, ai_document, ai_embedding, ai_languageModel, ai_memory, ai_outputParser, ai_retriever, ai_textSplitter, ai_tool, ai_vectorRetriever, ai_vectorStore
  • data 包含要添加的数据。关于 n8n 预期的数据结构,请参考数据结构
this.getInputConnectionData(inputName, itemIndex, inputIndex?) 从指定的非主输入获取数据。
  • inputName 表示输入连接类型,必须是以下之一:ai_agent, ai_chain, ai_document, ai_embedding, ai_languageModel, ai_memory, ai_outputParser, ai_retriever, ai_textSplitter, ai_tool, ai_vectorRetriever, ai_vectorStore
  • itemIndex 应始终设为 0(该参数将在未来功能中使用)
  • 当有多个节点连接到指定输入时,使用 inputIndex 参数。
this.getInputData(inputIndex?, inputName?) 从主输入获取数据。
this.getNode() 获取当前节点。
this.getNodeOutputs() 获取当前节点的输出。
this.getExecutionCancelSignal() 用于在工作流停止时终止函数执行。大多数情况下 n8n 会自动处理,但在构建自定义链或代理时可能需要使用。该方法替代了常规构建 LangChain 应用时会使用的取消运行中的 LLMChain 代码。

模板与示例#

🤖 AI Powered RAG Chatbot for Your Docs + Google Drive + Gemini + Qdrant

by Joseph LePage

View template details
Telegram AI bot with LangChain nodes

by n8n Team

View template details
Agentic Telegram AI bot with with LangChain nodes and new tools

by Yulia

View template details
Browse LangChain 代码 integration templates, or search all templates

相关资源#

查看 n8n 的高级 AI 文档。

AI glossary#

  • completion: Completions are the responses generated by a model like GPT.
  • hallucinations: Hallucination in AI is when an LLM (large language model) mistakenly perceives patterns or objects that don't exist.
  • vector database: A vector database stores mathematical representations of information. Use with embeddings and retrievers to create a database that your AI can access when answering questions.
  • vector store: A vector store, or vector database, stores mathematical representations of information. Use with embeddings and retrievers to create a database that your AI can access when answering questions.