N8N中文教程
代码开发/Built_in_methods_and_variables

LangChain 代码节点方法#

n8n 提供了以下方法,以简化在 LangChain 代码节点 中执行常见任务的操作。

仅限 LangChain 代码节点使用 这些变量只能在 LangChain 代码节点的表达式中使用,不能在其他节点中使用。

方法说明
this.addInputData(inputName, data)填充指定非主输入(non-main input)的数据。可用于模拟(mocking)数据。
  • 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) | 填充指定非主输出(non-main output)的数据。可用于模拟(mocking)数据。

  • 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 会自动处理,但如果你正在构建自己的链(chains)或智能体(agents),可能需要手动使用它。该方法替代了在常规 LangChain 应用中使用的 取消运行中的 LLMChain 代码。