Merge branch 'python_custom_node_component' of github.com:logspace-ai/langflow into python_custom_node_component

This commit is contained in:
Lucas Oliveira 2023-07-31 14:05:41 -03:00
commit ce0a67553a
2 changed files with 104 additions and 57 deletions

View file

@ -20,14 +20,13 @@ For an in-depth explanation of custom components, their rules, and applications,
- **Code:** The Python code to define the component. - **Code:** The Python code to define the component.
## The CustomComponent Class ## The CustomComponent Class
The CustomComponent class serves as the foundation for creating custom components. By inheriting this class, users can create new, configurable components, tailored to their specific requirements. The CustomComponent class serves as the foundation for creating custom components. By inheriting this class, users can create new, configurable components, tailored to their specific requirements.
**Methods** **Methods**
- **build**: This method is required within a Custom Component class. It defines the component's functionality and specifies how it processes input data to produce output data. This method is called when the component is built (i.e., when you click the *Build* ⚡ button in the canvas). - **build**: This method is required within a Custom Component class. It defines the component's functionality and specifies how it processes input data to produce output data. This method is called when the component is built (i.e., when you click the _Build_ ⚡ button in the canvas).
The type annotations of the _`build`_ instance method are used to create the fields of the component. The type annotations of the _`build`_ instance method are used to create the fields of the component.
@ -46,7 +45,9 @@ The CustomComponent class serves as the foundation for creating custom component
| _`langchain.schema.BaseRetriever`_ | | _`langchain.schema.BaseRetriever`_ |
<Admonition type="info"> <Admonition type="info">
Unlike Langchain types, base Python types do not add a [handle](../guidelines/components) to the field by default. To add handles, use the _`input_types`_ key in the _`build_config`_ method. Unlike Langchain types, base Python types do not add a
[handle](../guidelines/components) to the field by default. To add handles,
use the _`input_types`_ key in the _`build_config`_ method.
</Admonition> </Admonition>
- **build_config**: Used to define the configuration fields of the component (if applicable). It should always return a dictionary with specific keys representing the field names and corresponding configurations. This method is called when the code is processed (i.e., when you click _Check and Save_ in the code editor). It must follow the format described below: - **build_config**: Used to define the configuration fields of the component (if applicable). It should always return a dictionary with specific keys representing the field names and corresponding configurations. This method is called when the code is processed (i.e., when you click _Check and Save_ in the code editor). It must follow the format described below:
@ -57,7 +58,7 @@ The CustomComponent class serves as the foundation for creating custom component
Below are the available keys used to configure component fields: Below are the available keys used to configure component fields:
| Key | Description | | Key | Description |
| -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | -------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| _`field_type: str`_ | The type of the field (can be any of the types supported by the _`build`_ method). | | _`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 can be a list of values, meaning that the user can manually add more inputs to the same field. | | _`is_list: bool`_ | If the field can be a list of values, meaning that the user can manually add more inputs to the same field. |
| _`options: List[str]`_ | When defined, the field becomes a dropdown menu where a list of strings defines the options to be displayed. If the _`value`_ attribute is set to one of the options, that option becomes default. For this parameter to work, _`field_type`_ should invariably be _`str`_. | | _`options: List[str]`_ | When defined, the field becomes a dropdown menu where a list of strings defines the options to be displayed. If the _`value`_ attribute is set to one of the options, that option becomes default. For this parameter to work, _`field_type`_ should invariably be _`str`_. |
@ -68,18 +69,16 @@ The CustomComponent class serves as the foundation for creating custom component
| _`password: bool`_ | To mask the input text. Useful to hide sensitive text (e.g. API keys). | | _`password: bool`_ | To mask the input text. Useful to hide sensitive text (e.g. API keys). |
| _`required: bool`_ | Makes the field required. | | _`required: bool`_ | Makes the field required. |
| _`info: str`_ | Adds a tooltip to the field. | | _`info: str`_ | Adds 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*. | | _`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 helpful methods for specific tasks (e.g., to load and use other flows from the Langflow platform): - The CustomComponent class also provides helpful methods for specific tasks (e.g., to load and use other flows from the Langflow platform):
| Method Name | Description | | Method Name | Description |
| -------------- | ------------------------------------------------------------- | | -------------- | ------------------------------------------------------------------- |
| _`list_flows`_ | Returns a list of Flow objects with an _`id`_ and a _`name`_. | | _`list_flows`_ | Returns a list of Flow objects with an _`id`_ and a _`name`_. |
| _`get_flow`_ | Returns a Flow object. Parameters are _`flow_name`_ or _`flow_id`_. |
| _`load_flow`_ | Loads a flow from a given _`id`_. | | _`load_flow`_ | Loads a flow from a given _`id`_. |
<Admonition type="info" label="Tip"> <Admonition type="info" label="Tip">
Check out the [FlowRunner](../examples/flow-runner) example to understand how to call a flow from a custom component. Check out the [FlowRunner](../examples/flow-runner) example to understand how to call a flow from a custom component.

View file

@ -29,11 +29,54 @@ We will cover how to:
- Load a flow using the _`load_flow`_ method. - Load a flow using the _`load_flow`_ method.
- Configure a dropdown input field using the _`options`_ parameter. - Configure a dropdown input field using the _`options`_ parameter.
<details>
<summary>Example Code</summary>
```python
from langflow import CustomComponent
from langchain.schema import Document
class FlowRunner(CustomComponent):
display_name = "Flow Runner"
description = "Run other flows using a document as input."
def build_config(self):
flows = self.list_flows()
flow_names = [f.name for f in flows]
return {"flow_name": {"options": flow_names,
"display_name": "Flow Name",
},
"document": {"display_name": "Document"}
}
def build(self, flow_name: str, document: Document) -> Document:
# List the flows
flows = self.list_flows()
# Get the flow that matches the selected name
# You can also get the flow by id
# using self.get_flow(flow_id=flow_id)
flow = self.get_flow(flow_name=flow_name)
# Load the flow
tweaks = {}
flow = self.load_flow(flow.id, tweaks)
# Get the page_content from the document
page_content = document.page_content
# Use it in the flow
result = flow(page_content)
return Document(page_content=str(result))
```
</details>
<CH.Scrollycoding rows={20} className={""}> <CH.Scrollycoding rows={20} className={""}>
```python ```python
from langflow import CustomComponent from langflow import CustomComponent
class MyComponent(CustomComponent): class MyComponent(CustomComponent):
display_name = "Custom Component" display_name = "Custom Component"
description = "This is a custom component" description = "This is a custom component"
@ -53,6 +96,7 @@ The typical structure of a Custom Component is composed of _`display_name`_ and
```python ```python
from langflow import CustomComponent from langflow import CustomComponent
# focus # focus
class FlowRunner(CustomComponent): class FlowRunner(CustomComponent):
# focus # focus
@ -90,7 +134,7 @@ class FlowRunner(CustomComponent):
``` ```
Second, we will import _`Document`_ from the [*langchain.schema*](https://docs.langchain.com/docs/components/schema/) module. This will be the return type of the _`build`_ method. Second, we will import _`Document`_ from the [_langchain.schema_](https://docs.langchain.com/docs/components/schema/) module. This will be the return type of the _`build`_ method.
--- ---
@ -99,6 +143,7 @@ from langflow import CustomComponent
# focus # focus
from langchain.schema import Document from langchain.schema import Document
class FlowRunner(CustomComponent): class FlowRunner(CustomComponent):
display_name = "Flow Runner" display_name = "Flow Runner"
description = "Run other flows using a document as input." description = "Run other flows using a document as input."
@ -120,7 +165,7 @@ Now, let's add the [parameters](focus://11[20:55]) and the [return type](focus:/
--- ---
```python ```python focus=13:14
from langflow import CustomComponent from langflow import CustomComponent
from langchain.schema import Document from langchain.schema import Document
@ -133,9 +178,7 @@ class FlowRunner(CustomComponent):
... ...
def build(self, flow_name: str, document: Document) -> Document: def build(self, flow_name: str, document: Document) -> Document:
# focus
# List the flows # List the flows
# focus
flows = self.list_flows() flows = self.list_flows()
``` ```
@ -144,7 +187,7 @@ We can now start writing the _`build`_ method. Let's list available flows in "My
--- ---
```python ```python focus=15:18
from langflow import CustomComponent from langflow import CustomComponent
from langchain.schema import Document from langchain.schema import Document
@ -159,17 +202,19 @@ class FlowRunner(CustomComponent):
def build(self, flow_name: str, document: Document) -> Document: def build(self, flow_name: str, document: Document) -> Document:
# List the flows # List the flows
flows = self.list_flows() flows = self.list_flows()
# focus
# Get the flow that matches the selected name # Get the flow that matches the selected name
# focus # You can also get the flow by id
flow = next(filter(lambda f: f.name == flow_name, flows)) # using self.get_flow(flow_id=flow_id)
flow = self.get_flow(flow_name=flow_name)
``` ```
And retrieve a flow that matches the selected name (we'll make a dropdown input field for the user to choose among flow names). And retrieve a flow that matches the selected name (we'll make a dropdown input field for the user to choose among flow names).
<Admonition type="caution"> <Admonition type="caution">
From version 0.4.0, names are unique, which was not the case in previous versions. This might lead to unexpected results if using flows with the same name. From version 0.4.0, names are unique, which was not the case in previous
versions. This might lead to unexpected results if using flows with the same
name.
</Admonition> </Admonition>
--- ---
@ -190,7 +235,9 @@ class FlowRunner(CustomComponent):
# List the flows # List the flows
flows = self.list_flows() flows = self.list_flows()
# Get the flow that matches the selected name # Get the flow that matches the selected name
flow = next(filter(lambda f: f.name == flow_name, flows)) # You can also get the flow by id
# using self.get_flow(flow_id=flow_id)
flow = self.get_flow(flow_name=flow_name)
# focus # focus
# Load the flow # Load the flow
# focus # focus
@ -220,7 +267,9 @@ class FlowRunner(CustomComponent):
# List the flows # List the flows
flows = self.list_flows() flows = self.list_flows()
# Get the flow that matches the selected name # Get the flow that matches the selected name
flow = next(filter(lambda f: f.name == flow_name, flows)) # You can also get the flow by id
# using self.get_flow(flow_id=flow_id)
flow = self.get_flow(flow_name=flow_name)
# Load the flow # Load the flow
tweaks = {} tweaks = {}
flow = self.load_flow(flow.id, tweaks) flow = self.load_flow(flow.id, tweaks)
@ -255,11 +304,13 @@ class FlowRunner(CustomComponent):
} }
def build(self, flow_name: str, document: Optional[Document] = None) -> Document: def build(self, flow_name: str, document: Document) -> Document:
# List the flows # List the flows
flows = self.list_flows() flows = self.list_flows()
# Get the flow that matches the selected name # Get the flow that matches the selected name
flow = next(filter(lambda f: f.name == flow_name, flows)) # You can also get the flow by id
# using self.get_flow(flow_id=flow_id)
flow = self.get_flow(flow_name=flow_name)
# Load the flow # Load the flow
tweaks = {} tweaks = {}
flow = self.load_flow(flow.id, tweaks) flow = self.load_flow(flow.id, tweaks)
@ -276,12 +327,9 @@ Finally, we can add field customizations through the _`build_config`_ method. He
Make sure that the field type is _`str`_ and _`options`_ values are strings. Make sure that the field type is _`str`_ and _`options`_ values are strings.
</Admonition> </Admonition>
---
</CH.Scrollycoding> </CH.Scrollycoding>
Done! This is what our script and custom component look like: Done! This is what our script and custom component looks like:
<div style={{ <div style={{
display: "flex", display: "flex",
@ -299,10 +347,9 @@ Done! This is what our script and custom component look like:
display: "flex", display: "flex",
justifyContent: "center", justifyContent: "center",
}} }}
/> />
<ZoomableImage <ZoomableImage
alt="Document Processor Component" alt="Document Processor Component"
sources={{ sources={{
@ -315,4 +362,5 @@ Done! This is what our script and custom component look like:
justifyContent: "center", justifyContent: "center",
}} }}
/> />
</div> </div>