Langflow Micro Tutorials — Transform Chain

Rodrigo Nader
Langflow
Published in
3 min readOct 22, 2023

Welcome back to our Langflow micro tutorials series! In this article, we’ll continue exploring simple Langflow examples and the design of custom components.

You can download the flow mentioned in this article to modify and understand the components used.

Today, we’ll focus on reproducing a Langchain Transform Chain in Langflow. Hope you enjoy it!

Main Features

Transform Chain: A custom component with a special method called perform_action. This method allows for customization of its logic to meet specific requirements before being converted into a chain.

Objective

In Langchain, a Transform Chain is designed to modify input data before sending it to another chain. It uses input and output variables that are transformed by a function (hereby called perform_action).

This function takes a dictionary of input variables and returns a dictionary of output variables, allowing for arbitrary Python code to change the inputs. Transform Chains are useful for tasks like segmenting long text, translating language, or extracting metadata. More information is available here.

The goal is to bring this Transform Chain feature into Langflow, converting any Python function into a chain type. This allows us to utilize all the chain-related functionalities Langflow offers. For example, they can be used as input to the CombineDocs Chain or Sequential Chains and can work with prompt templates or even open up the chat interface.

To use it, just open the code of the custom component and play around with the perform_action method.

In this case, we're simplifying it and passing a single input variable to the perform_action, but the logic applied in lines 16-19 makes it so that any argument supplied to the build function automatically becomes a part of the class with self — meaning you can add new parameters to the build function and access them within perform_action. The foo argument serves as an example of this.

Once the chat interface is activated, just interact with your function as if it’s a chain.

Notice that this opens up possibilities for including diverse fields in the component. For example, foo could be set as a Document instead of a string, allowing it to accept outputs from other components.

Download Flow (gist)

--

--