This class requires a
build method used to run the component and define its fields.
The Return Type Annotation of the
build method defines the component type (e.g., Chain, BaseLLM, or basic Python types).
The class can have a
build_config method, which defines configuration fields for the component. The
build_config method 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.
Let's create a custom component that processes a document (
langchain.schema.Document) using a simple function.
Pick a display name
To start, let's choose a name for our component by adding a
display_name attribute. This name will appear on the canvas. The name of the class is not relevant, but let's call it
Write a description
We can also write a description for it using a
Add the build method
Here, the build method takes two input parameters:
document, representing the input document to be processed, and
function, a string representing the selected text transformation to be applied (either "Uppercase," "Lowercase," or "Titlecase"). The method processes the text content of the input Document based on the selected function.
repr_value is used to display the result of the component on the canvas. It is optional and can be used to display any string value.
The return type is
Customize the component fields
build_config method is here defined to customize the component fields.
optionsdetermines that the field will be a dropdown menu. The list values and field type must be
valueis the default option of the dropdown menu.
display_nameis the name of the field to be displayed.
All done! This is what our script and brand-new custom component look like:
Loading Custom Components
For advanced customization, Langflow offers the option to create and load custom components outside of the standard interface. This process involves creating the desired components using a text editor and loading them using the Langflow CLI.
Create a folder that follows the same structural conventions as the config.yaml file. Inside this main directory, use a
custom_components subdirectory for your custom components.
custom_components, you can create a Python file for each component. Similarly, any custom agents should be housed in an
If you use a subdirectory name that is not in our config.yaml file, your component will appear in an
Other category in the sidebar.
Your structure should look something like this:
Loading Custom Components
The recommended way to load custom components is to set the
LANGFLOW_COMPONENTS_PATH environment variable to the path of your custom components directory. Then, run the Langflow CLI as usual.
Alternatively, you can specify the path to your custom components using the
--components-path argument when running the Langflow CLI, as shown below:
Langflow will attempt to load all of the components found in the specified directory. If a component fails to load due to errors in the component's code, Langflow will print an error message to the console but will continue loading the rest of the components.
Interacting with Custom Components
Once your custom components have been loaded successfully, they will appear in Langflow's sidebar. From there, you can add them to your Langflow canvas for use. However, please note that components with errors will not be available for addition to the canvas. Always ensure your code is error-free before attempting to load components.
Remember, creating custom components allows you to extend the functionality of Langflow to better suit your unique needs. Happy coding!