跳到主要内容

LangChain 代码节点方法#

n8n 提供以下方法,以便更便捷地在 LangChain 代码节点 中执行常见任务。

仅限 LangChain 代码节点使用

这些变量专用于 LangChain 代码节点中的表达式,不可在其他节点中使用。

方法描述
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 代码