The CustomComponent class allows us to create components that interact with Langflow itself. In this example, we will make a component that runs other flows available in "My Collection".
We will cover how to:
List Collection flows using the
Load a flow using the
Configure a dropdown input field using the
The typical structure of a Custom Component is composed of
Let's start by defining our component's
Second, we will import
Document from the langchain.schema module. This will be the return type of the
Now, let's add the parameters and the return type to the
build method. The parameters added are:
flow_nameis the name of the flow we want to run.
documentis the input document to be passed to that flow.
Documentis a Langchain type, it will add an input handle to the component (see more).
We can now start writing the
build method. Let's list available flows in "My Collection" using the
And retrieve a flow that matches the selected name (we'll make a dropdown input field for the user to choose among flow names).
You can load this flow using
get_flow and set a
tweaks dictionary to customize it. Find more about tweaks in our features guidelines.
We are using a
Document as input because it is a straightforward way to pass text data in Langflow (specifically because you can connect it to many loaders). Generally, a flow will take a string or a dictionary as input because that's what LangChain components expect. In case you are passing a dictionary, you need to build it according to the needs of the flow you are using.
The content of a document can be extracted using the
page_content attribute, which is a string, and passed as an argument to the selected flow.
Finally, we can add field customizations through the
build_config method. Here we added the
options key to make the
flow_name field a dropdown menu. Check out the custom component reference for a list of available keys.
Done! This is what our script and custom component looks like: