81 lines
No EOL
6.7 KiB
Text
81 lines
No EOL
6.7 KiB
Text
import Admonition from "@theme/Admonition";
|
|
|
|
# Custom Component
|
|
|
|
Used to create a custom component, a special type of Langflow component that allows users to extend the functionality of the platform by creating their own reusable and configurable components from a user-defined Python script.
|
|
|
|
To use a custom component, follow these steps:
|
|
|
|
- Create a class that inherits from _`langflow.CustomComponent`_ and contains a _`build`_ method.
|
|
- Use arguments with [Type Annotations (or Type Hints)](https://docs.python.org/3/library/typing.html) of the _`build`_ method to create component fields.
|
|
- If applicable, use the _`build_config`_ method to customize how these fields look and behave.
|
|
|
|
<Admonition type="info" label="Tip">
|
|
|
|
For an in depth explanation of custom components, their rules and applications, make sure to read [Custom Component guidelines](../guidelines/custom-component).
|
|
|
|
</Admonition>
|
|
|
|
**Params**
|
|
|
|
- **Code:** The Python code to define the component.
|
|
|
|
|
|
## The CustomComponent Class
|
|
|
|
|
|
**Methods**
|
|
|
|
- **build**: This is a required method within a Custom Component class. It defines the functionality of the component and specifies how it processes input data to produce output data. The type annotations of the _`build`_ instance method are used to create the fields of the component.
|
|
|
|
| Supported types |
|
|
| --------------------------------------------------------- |
|
|
| _`str`_, _`int`_, _`float`_, _`bool`_, _`list`_, _`dict`_ |
|
|
| _`langchain.chains.base.Chain`_ |
|
|
| _`langchain.PromptTemplate`_ |
|
|
| _`langchain.llms.base.BaseLLM`_ |
|
|
| _`langchain.Tool`_ |
|
|
| _`langchain.document_loaders.base.BaseLoader`_ |
|
|
| _`langchain.schema.Document`_ |
|
|
| _`langchain.text_splitters.TextSplitter`_ |
|
|
| _`langchain.vectorstores.base.VectorStore`_ |
|
|
| _`langchain.embeddings.base.Embeddings`_ |
|
|
| _`langchain.schema.BaseRetriever`_ |
|
|
|
|
|
|
- **build_config**: Used to define the configuration fields of the component. Should always return a dictionary with specific keys representing the field names and their corresponding configurations. It must follow the format described below:
|
|
|
|
- Top-level keys are field names.
|
|
- Their values are also of type _`dict`_. They specify the behavior of the generated fields.
|
|
|
|
Following are the available keys used to customize component fields:
|
|
|
|
| Key | Description |
|
|
| -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
| _`field_type: str`_ | The type of the field (can be any of the types supported by the _`build`_ method). |
|
|
| _`is_list: bool`_ | If the field contains a list of values. When paired with _`options`_, becomes a dropdown menu. |
|
|
| _`options: List[str]`_ | Defines the options to be displayed in a dropdown menu. If the _`value`_ attribute is set to one of the options, that option becomes the default. For this parameter to work, _`field_type`_ should invariably be _`str`_. |
|
|
| _`multiline: bool`_ | When the field is a string, if it should be multiline. Useful for longer texts when a text modal is helpful. |
|
|
| _`input_types: List[str]`_ | To be used when you want a _`str`_ field to have connectable handles. |
|
|
| _`display_name: str`_ | Defines the field name. |
|
|
| _`advanced: bool`_ | To hide the field in canvas view (shown only in the component's settings). |
|
|
| _`password: bool`_ | To mask the input text. |
|
|
| _`required: bool`_ | To make the field required. |
|
|
| _`info: str`_ | To add a tooltip with information to the field. |
|
|
| _`file_types: List[str]`_ | This is a requirement if the _`field_type`_ is *file*. Defines which file types will be accepted. For example, *json*, *yaml* or *yml*. |
|
|
|
|
|
|
|
|
- The CustomComponent class also provides methods to load and use other flows from the Langflow platform:
|
|
|
|
| Method name | Description |
|
|
| -------------- | ------------------------------------------------------------- |
|
|
| _`list_flows`_ | Returns a list of Flow objects with an _`id`_ and a _`name`_. |
|
|
| _`load_flow`_ | Loads a flow from a given _`id`_. |
|
|
|
|
|
|
<Admonition type="info" label="Tip">
|
|
|
|
Check out the [FlowRunner](../guidelines/custom-component) example to understand how to call a flow from a custom component.
|
|
|
|
</Admonition> |