langflow/docs/docs/components/custom.mdx
2024-06-24 05:05:35 -07:00

227 lines
7.2 KiB
Text
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import ThemedImage from "@theme/ThemedImage";
import useBaseUrl from "@docusaurus/useBaseUrl";
import ZoomableImage from "/src/theme/ZoomableImage.js";
import ReactPlayer from "react-player";
import Admonition from "@theme/Admonition";
# Custom Components
<Admonition type="warning" title="warning">
This page may contain outdated information. It will be updated as soon as possible.
</Admonition>
Build custom components in Langflow for various data processing and transformation tasks.
This guide provides a comprehensive overview of how to create custom components using Langflow.
## Basic Structure of a Custom Component
A custom component in Langflow typically includes the following parts:
1. **Class Definition**: Inherits from the `Component` class.
2. **Component Metadata**: Defines display name, description, and icon.
3. **Inputs and Outputs**: Specifies the inputs and outputs for the component.
4. **Processing Logic**: Implements the logic for processing data within the component.
A custom component in Python looks like this:
```python
from langflow.custom import Component
from langflow.inputs import MessageTextInput, IntInput, BoolInput, DropdownInput, HandleInput
from langflow.template import Output
from langflow.schema import Data, Message
from typing import List, Optional
class ExampleComponent(Component):
display_name = "Example Component"
description = "A template for creating custom components."
icon = "icon-name"
inputs = [
MessageTextInput(
name="input_text",
display_name="Input Text",
info="Text input for the component.",
),
IntInput(
name="input_number",
display_name="Input Number",
info="Numeric input for the component.",
),
BoolInput(
name="input_boolean",
display_name="Input Boolean",
info="Boolean input for the component.",
),
DropdownInput(
name="input_choice",
display_name="Input Choice",
options=["Option1", "Option2", "Option3"],
info="Dropdown input for the component.",
),
]
outputs = [
Output(display_name="Output Data", name="output_data", method="process_data"),
]
def process_data(self) -> Data:
input_text = self.input_text
input_number = self.input_number
input_boolean = self.input_boolean
input_choice = self.input_choice
# Implement your processing logic here
result = f"Processed: {input_text}, {input_number}, {input_boolean}, {input_choice}"
self.status = result
return Data(data={"result": result})
```
## Create a Custom Component Step-by-Step
1. Create a class that inherits from the `Component` class.
```python
class ExampleComponent(Component):
# Class content
```
2. Define metadata such as `display_name`, `description`, and `icon`.
```python
display_name = "Example Component"
description = "A template for creating custom components."
icon = "icon-name"
```
3. Define the inputs and outputs for the component using the `inputs` and `outputs` lists.
**Inputs** can be of various types such as `TextInput`, `IntInput`, `BoolInput`, `DropdownInput`, etc.
```python
inputs = [
MessageTextInput(
name="input_text",
display_name="Input Text",
info="Text input for the component.",
),
IntInput(
name="input_number",
display_name="Input Number",
info="Numeric input for the component.",
),
BoolInput(
name="input_boolean",
display_name="Input Boolean",
info="Boolean input for the component.",
),
DropdownInput(
name="input_choice",
display_name="Input Choice",
options=["Option1", "Option2", "Option3"],
info="Dropdown input for the component.",
),
]
```
**Outputs** define the output methods for the component.
```python
outputs = [
Output(display_name="Output Data", name="output_data", method="process_data"),
]
```
4. Implement the logic for processing data within the component. Define methods for processing data and returning results.
```python
def process_data(self) -> Data:
input_text = self.input_text
input_number = self.input_number
input_boolean = self.input_boolean
input_choice = self.input_choice
# Implement your processing logic here
result = f"Processed: {input_text}, {input_number}, {input_boolean}, {input_choice}"
self.status = result
return Data(data={"result": result})
```
## Advanced Example: Create a Conditional Router Component
This example demonstrates a more complex component that routes data based on a condition.
Notice that this component has two outputs associated with the methods `true_response` and `false_response`.
These methods trigger `self.stop` to block the transmission for the selected output, allowing for logic operations to be implemented visually.
```python
from langflow.custom import Component
from langflow.inputs import MessageTextInput, DropdownInput, BoolInput
from langflow.template import Output
from langflow.field_typing import Text
class ConditionalRouterComponent(Component):
display_name = "Conditional Router"
description = "Routes input based on a specified condition."
icon = "router"
inputs = [
MessageTextInput(
name="input_value",
display_name="Input Value",
info="Value to be evaluated.",
),
MessageTextInput(
name="comparison_value",
display_name="Comparison Value",
info="Value to compare against.",
),
DropdownInput(
name="operator",
display_name="Operator",
options=["equals", "not equals", "contains"],
info="Comparison operator.",
),
]
outputs = [
Output(display_name="True Output", name="true_output", method="true_response"),
Output(display_name="False Output", name="false_response", method="false_response"),
]
def evaluate_condition(self, input_value: str, comparison_value: str, operator: str) -> bool:
if operator == "equals":
return input_value == comparison_value
elif operator == "not equals":
return input_value != comparison_value
elif operator == "contains":
return comparison_value in input_value
return False
def true_response(self) -> Text:
if self.evaluate_condition(self.input_value, self.comparison_value, self.operator):
self.stop("false_response")
return self.input_value
else:
self.stop("true_response")
return ""
def false_response(self) -> Text:
if not self.evaluate_condition(self.input_value, self.comparison_value, self.operator):
self.stop("true_response")
return self.input_value
else:
self.stop("false_response")
return ""
```
By following these steps and examples, you can create custom components in Langflow tailored to your specific needs. The modular structure of Custom Components allows for flexible and reusable components that can be easily integrated into your workflows.
---