跳至内容

LangChain 代码节点#

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

本页面将介绍节点参数、配置节点的指南以及更多资源的链接。

云端不可用

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

节点参数#

添加代码#

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

Code节点不同,LangChain Code节点不支持Python。

  • 执行: 像n8n自带的代码节点一样使用LangChain代码节点。该节点从工作流中获取输入数据,处理后将其作为节点输出返回。此模式需要一个主输入和输出。您必须在输入输出中创建这些连接。
  • 提供数据: 使用LangChain代码节点作为子节点,将数据发送到根节点。这里使用了除main以外的其他输出。

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

输入#

选择输入类型。

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

输出#

选择输出类型。

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

节点输入输出配置#

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

Screenshot of a workflow with four LangChain nodes, configured as different node types

节点类型 输入 输出 代码模式
应用节点。类似于代码节点 执行
根节点 主节点;至少包含另一种类型 主节点 执行
子节点 - 除主节点外的其他类型。必须与您想要连接的输入类型匹配。 提供数据
包含子节点的子节点 非主类型 非主类型。必须与您想要连接的输入类型匹配。 提供数据

内置方法#

n8n提供了这些方法,以便更轻松地在LangChain代码节点中执行常见任务。

方法 描述
this.addInputData(inputName, data) Populate the data of a specified non-main input. Useful for mocking 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) Populate the data of a specified non-main output. Useful for mocking 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?) Get data from a specified non-main input.
  • 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?) Get data from the main input.
this.getNode() Get the current node.
this.getNodeOutputs() Get the outputs of the current node.
this.getExecutionCancelSignal() Use this to stop the execution of a function when the workflow stops. In most cases n8n handles this, but you may need to use it if building your own chains or agents. It replaces the Cancelling a running LLMChain code that you'd use if building a LangChain application normally.

模板和示例#

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

作者:Joseph LePage

查看模板详情
Custom LangChain agent written in JavaScript

由n8n团队

查看模板详情
Use any LangChain module in n8n (with the LangChain code node)

作者:David Roberts

查看模板详情
浏览LangChain代码集成模板, or 搜索所有模板

查看n8n的高级AI文档。

AI术语表#

  • completion: 补全(completion)是指由GPT等模型生成的响应内容。
  • 幻觉: AI中的幻觉是指大型语言模型(LLM)错误地感知到不存在的模式或对象。
  • 向量数据库: 向量数据库存储信息的数学表示。与嵌入和检索器配合使用,可创建AI在回答问题时能够访问的数据库。
  • 向量存储: 向量存储(或称向量数据库)用于存储信息的数学表示。结合嵌入模型和检索器使用,可创建供AI在回答问题时访问的数据库。
优云智算