Bug描述
你好,我想知道是否有可能导入其他Python库,如spaCy,来构建使用它们的自定义组件,例如获取文本的nlp分析。
到目前为止,我已经尝试了以下方法:
# from langflow.field_typing import Data
from langflow.custom import Component
from langflow.io import MessageTextInput, Output
from langflow.schema import Data
import spacy
print(spacy.__version__)
nlp = spacy.load("en_core_web_sm")
class CustomComponent(Component):
display_name = "Custom Component"
description = "Use as a template to create your own component."
documentation: str = "http://docs.langflow.org/components/custom"
icon = "custom_components"
name = "CustomComponent"
inputs = [
MessageTextInput(name="input_value", display_name="Input Value", value="Hello, World!"),
]
outputs = [
Output(display_name="Output", name="output", method="build_output"),
]
def build_output(self) -> Data:
doc = nlp(self.input_value)
tokens = []
for token in doc:
tokens.append(f"({token.text}, {token.pos_})")
data = Data(value="; ".join(tokens))
self.status = data
return data
但在执行过程中,我得到了
Error Building Component
Error building Component Custom Component: name 'nlp' is not defined
重现问题
创建自定义组件
复制我尝试过的代码
预期行为
Spacy加载并在输出中提供nlp分析
谁可以帮忙?
- 无响应*
操作系统
Windows 11
Langflow版本
1.0.12
Python版本
3.11
截图
- 无响应*
流程文件
- 无响应*
5条答案
按热度按时间cyej8jka1#
要解决在尝试导入和使用Spacy时出现的错误 "name 'nlp' is not defined",你需要确保在类方法中正确定义和初始化
nlp
对象。以下是更新后的代码:在这个更新的代码中,Spacy模型在
CustomComponent
类的__init__
方法中加载,确保在调用build_output
方法时,nlp
对象可用。这应该解决了"name 'nlp' is not defined"的错误。qmb5sa222#
这段代码不起作用,它甚至不让我检查和保存组件。我只是点击按钮保存,但它什么都不做。
pengsaosao3#
要解决使用Spacy库的自定义Langflow组件的问题,请确保您的组件遵循文档中概述的规则和结构。以下是如何创建一个使用Spacy进行命名实体识别(NER)的自定义组件的示例:
确保已安装Spacy库和所需的模型:
将此脚本保存在您的
custom_components
目录中,并按照文档中的描述使用Langflow CLI加载它:这将允许您在Langflow中保存和使用自定义的Spacy组件。有关创建和加载自定义组件的更详细信息,请参阅 Langflow documentation1(https://github.com/langflow-ai/langflow/blob/dev/docs/docs/tutorials/custom_components.mdx)2(https://github.com/langflow-ai/langflow/blob/dev/docs/docs/administration/custom-component.mdx)3(https://github.com/logspace-ai/langflow/blob/dev/src/frontend/tests/onlyFront/assets/flowtest.json)4(https://github.com/logspace-ai/langflow/blob/dev/src/frontend/tests/end-to-end/assets/flowtest.json)。
lqfhib0f4#
在GUI中,我应该从你的脚本中找到自定义组件的位置吗?我只看到普通的自定义组件。
jv2fixgn5#
你应该在Langflow的侧边栏中找到自定义组件,一旦它们成功加载。如果你的自定义组件没有出现,请确保:
LANGFLOW_COMPONENTS_PATH
环境变量已正确设置为自定义组件目录的路径。以下是快速检查清单:
完成这些步骤后,你的自定义组件应该会出现在Langflow GUI的侧边栏中。如果组件代码中有任何错误,它们将无法添加到画布1(https://github.com/langflow-ai/langflow/blob/dev/docs/docs/tutorials/custom_components.mdx)2(https://github.com/langflow-ai/langflow/blob/dev/docs/docs/administration/custom-component.mdx)中。