📝 docs(custom.mdx): update documentation for Custom Component

The changes in this commit update the documentation for the Custom Component.

- Clarify that the code must be a class that inherits from the `langflow.CustomComponent` class.
- Explain that the type annotations of the `build` instance method are used to create the fields of the component.
- Provide a table of supported types for the `build` method.
- Introduce the `build_config` instance method for defining configuration fields.
- Describe the format and available keys for the `build_config` method.
- Add usage example for the `build` method.

These changes improve the clarity and completeness of the documentation for the Custom Component, making it easier for users to understand and utilize this feature.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-07-28 11:23:26 -03:00
commit 457453656a

View file

@ -2,48 +2,79 @@ import Admonition from "@theme/Admonition";
# Custom Component
---
Used to create a custom component. The code is the class that will be converted to a Custom Component with the fields and formatting you define.
**Params**
- **Code:** The Python code to define the component.
The code must be a class that inherits from the _`langflow.CustomComponent`_ class.
The type annotations of the _`build`_ method will be used to create the fields of the component. Supported types are:
The type annotations of the _`build`_ instance method will be used to create the fields of the component.
- _`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`_
| 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`_ |
The class can have a [_`build_config`_](focus://8) instance method, which is used to define configuration fields for the component.
The [_`build_config`_](focus://8) method should always return a dictionary with specific keys representing the field names and their corresponding configurations.
It must follow the format described below:
The top level keys are the field names.
Their values are of type _`dict`_ with any of the following keys (all of them are **optional**):
- The top level keys are the field names.
- Their values are of type _`dict`_ with the following keys:
| 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 is a list |
| _`options: List[str]`_ | If, and only if, the _`is_list`_ is _`True`_, this defines the options to be displayed. If you set the _`value`_ attribute to one of the options, it will be selected by default. Having _`is_list`_ and _`options`_ will display a dropdown menu. |
| _`multiline: bool`_ | When the field is a string, if it should be multiline |
| _`input_types: List[str]`_ | To be used when you want a _`str`_ field to have connectable handles |
| _`display_name: str`_ | To define the name of the field |
| _`advanced: bool`_ | To hide the field in default view |
| _`password: bool`_ | To mask the input text |
| _`required: bool`_ | To make the field required |
| _`info: str`_ | To add a tooltip 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 |
- _`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 is a list.
- _`options: List[str]`_: When the field is a list, the options to be displayed. If you set the _`value`_ attribute to one of the options, it will be selected by default.
- _`multiline: bool`_: When the field is a string, if it should be multiline.
- _`input_types: List[str]`_: To be used when you want a _`str`_ field to have connectable handles.
- _`display_name: str`_: To define the name of the field.
- _`advanced: bool`_: To hide the field in default view.
- _`password: bool`_: To mask the input text.
- _`required: bool`_: To make the field required.
- _`info: str`_: To add a tooltip 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*.
Now we can load the flow using the _`load_flow`_ method.
The _`tweaks`_ parameter is a dictionary that allows you to customize the flow.
The CustomComponent class provides the following methods:
| Method name | Description |
| -------------- | ------------------------------------------------------------- |
| _`list_flows`_ | Returns a list of Flow objects with an _`id`_ and a _`name`_. |
| _`load_flow`_ | Loads a flow from the given _`id`_. |
### Usage
```python
# In the CustomComponent class
def build(self, flow_name: str)
# This is a list of Flow objects with an id and a name
flows = self.list_flows()
found_flow = next((f for f in flows if f.name == flow_name), None)
if not found_flow:
raise ValueError(f"Flow {flow_name} not found")
flow = self.load_flow(found_flow.id)
result = flow()
return result
```
<Admonition type="info" label="Tip">