跳到主要内容

贡献组件

新组件作为 Component 类的对象添加。

依赖项被添加到 pyproject.toml 文件中。

向 Langflow 贡献示例组件

任何人都可以贡献示例组件。例如,要创建一个名为 DataFrame processor 的新数据组件,请按照以下步骤将其贡献给 Langflow。

  1. Create a Python file for your component, such as dataframe_processor.py.

  2. Write your component as an object of the Component class. Create a new class that inherits from Component and override the base class's methods.

    Backwards compatibility

    The lfx import path replaced the import from langflow.custom import Component in Langflow 1.7, but the original input is still compatible and works the same way.


    _10
    from typing import Any, Dict, Optional
    _10
    import pandas as pd
    _10
    from lfx.custom.custom_component.component import Component
    _10
    _10
    class DataFrameProcessor(Component):
    _10
    """A component that processes pandas DataFrames with various operations."""

  3. Define class attributes to provide information about your custom component:


    _13
    from typing import Any, Dict, Optional
    _13
    import pandas as pd
    _13
    from lfx.custom.custom_component.component import Component
    _13
    _13
    class DataFrameProcessor(Component):
    _13
    """A component that processes pandas DataFrames with various operations."""
    _13
    _13
    display_name: str = "DataFrame Processor"
    _13
    description: str = "Process and transform pandas DataFrames with various operations like filtering, sorting, and aggregation."
    _13
    documentation: str = "https://docs.langflow.org/components-dataframe-processor"
    _13
    icon: str = "DataframeIcon"
    _13
    priority: int = 100
    _13
    name: str = "dataframe_processor"

    • display_name: A user-friendly name shown in the visual editor.
    • description: A brief description of what your component does.
    • documentation: A link to detailed documentation.
    • icon: An emoji or icon identifier for visual representation. Langflow uses Lucide for icons. To assign an icon to your component, set the icon attribute to the name of a Lucide icon as a string, such as icon = "file-text". Langflow renders icons from the Lucide library automatically. For more information, see Contributing bundles.
    • priority: An optional integer to control display order. Lower numbers appear first.
    • name: An optional internal identifier that defaults to class name.
  4. Define the component's interface by specifying its inputs, outputs, and the method that will process them. The method name must match the method field in your outputs list, as this is how Langflow knows which method to call to generate each output.

    This example creates a minimal custom component skeleton.


    _21
    from typing import Any, Dict, Optional
    _21
    import pandas as pd
    _21
    from lfx.custom.custom_component.component import Component
    _21
    _21
    class DataFrameProcessor(Component):
    _21
    """A component that processes pandas DataFrames with various operations."""
    _21
    _21
    display_name: str = "DataFrame Processor"
    _21
    description: str = "Process and transform pandas DataFrames with various operations like filtering, sorting, and aggregation."
    _21
    documentation: str = "https://docs.langflow.org/components-dataframe-processor"
    _21
    icon: str = "DataframeIcon"
    _21
    priority: int = 100
    _21
    name: str = "dataframe_processor"
    _21
    _21
    # input and output lists
    _21
    inputs = []
    _21
    outputs = []
    _21
    _21
    # method
    _21
    def some_output_method(self):
    _21
    return ...

  1. dataframe_processor.py 保存到 src/lfx/src/lfx/components 目录。 本示例添加了一个数据组件,因此将其添加到 /data 目录中。

  2. src/lfx/src/lfx/components/data/__init__.py 中添加组件依赖项,格式为 from .DataFrameProcessor import DataFrameProcessor。 您可以在 Langflow 仓库中查看 /data/init.py

  3. 将任何新的依赖项添加到 pyproject.toml 文件中。

  4. 为您的组件提交文档。对于此示例组件,您需要将文档提交到 数据组件页面

  5. 将您的更改作为拉取请求 (pull request) 提交。Langflow 团队将进行审查、提出修改建议,并将您的组件添加到 Langflow 中。

修改组件的最佳实践

在创建或更新组件时,请遵循以下最佳实践,以维护向后兼容性并确保用户的顺畅体验。

不要重命名类或 name 属性

更改类名或 name 属性会破坏所有现有用户的组件。发生这种情况是因为前端测试 type 属性,该属性被设置为类的名称或 name 属性。如果这些名称发生更改,该组件实际上就变成了一个新组件,而旧组件就会消失。

相反,请执行以下操作:

  • 如果旧名称不清楚,仅更改显示名称 (display name)。
  • 如果功能发生更改但仍相关,仅更改显示名称。
  • 如果必须使用新的内部名称,请将旧组件标记为 legacy=true 并创建一个新组件。

例如:


_10
class MyCustomComponent(BaseComponent):
_10
name = "my_custom_component_internal"
_10
legacy = True

不要删除字段和输出

删除字段或输出可能会导致连接断开并改变组件的行为。

相反,应将字段标记为 deprecated(弃用)并保留在原位置。如果绝对有必要删除,您必须定义并记录迁移计划。务必清楚地向用户传达字段信息中的任何更改。

将过时的组件维护为 legacy(遗留)

更新组件时,将它们创建为完全独立的实体,同时将旧组件维护为遗留版本。始终确保向后兼容性,并且永远不要从基类(如 LCModelComponent)中删除方法和属性。

优先使用异步方法

在组件中始终优先使用异步方法和函数。在与文件交互时,使用 aiofileanyio.Path 以获得更好的性能和兼容性。

在组件中包含测试

使用 ComponentTestBase 类为您的更改包含测试。有关更多信息,请参阅 测试组件

文档

在拉取请求中记录更改时,请清楚地解释 改了什么(例如显示名称更新或新字段)、为什么改(例如改进或错误修复)以及对现有用户的 影响

例如:

PR 示例

_22
# 更改 Notify 组件的拉取请求
_22
_22
此拉取请求更新了 Notify 组件。
_22
_22
## 改了什么
_22
- 添加了新的 `timeout` 字段,以控制组件等待响应的时间。
_22
- 为了清晰起见,将 `message` 字段重命名为 `notification_text`。
_22
- 增加了对异步操作的支持。
_22
- 弃用了 `retry_count` 字段,改用 `max_retries`。
_22
_22
## 为什么改
_22
- `timeout` 字段解决了用户对更好控制等待时间的需求。
_22
- 将 `message` 更改为 `notification_text` 使该字段的用途更明确。
_22
- 异步支持提高了复杂流程中的性能。
_22
- 将 `retry_count` 更改为 `max_retries` 符合常见的重试模式术语。
_22
_22
## 对用户的影响
_22
- 新的 `timeout` 字段是可选的(默认为 30 秒)。
_22
- 用户将看到 `retry_count` 的弃用警告。
_22
- 迁移:在现有流程中将 `retry_count` 替换为 `max_retries`。
_22
- 这两个字段都将保留到 2.0 版本。
_22
- 异步支持无需任何操作 - 它是向后兼容的。

示例拉取请求流程

  1. 创建或更新组件。 如果用途保持不变,请保留类名和 name 属性。 否则,创建一个新组件并将旧组件移动到 legacy
  2. 添加测试。 使用 ComponentTestBase 类之一创建测试。 有关更多信息,请参阅 测试组件
  3. 将过时的字段和输出标记为 deprecated(弃用)并保留在原位置,以确保向后兼容性。
  4. 记录您的更改。 如果发生破坏性更改,请包含迁移说明。
Search