Runnable
Author: hyeyeoon
Peer Review : hong-seongmin, Wooseok Jeong
Proofread : Q0211
This is a part of LangChain Open Tutorial
Overview
LangChain's Runnable objects provide a modular and flexible approach to designing workflows by enabling the chaining, parallel execution, and transformation of data. These utilities allow for efficient handling of structured inputs and outputs, with minimal code overhead.
Key Components is:
RunnableLambda: A lightweight utility that enables the application of custom logic through lambda functions, ideal for dynamic and quick data transformations.RunnablePassthrough: Designed to pass input data unchanged or augment it with additional attributes when paired with the.assign()method.itemgetter: A Pythonoperatormodule utility for efficiently extracting specific keys or indices from structured data such as dictionaries or tuples.
These tools can be combined to build powerful workflows, such as:
Extracting and processing specific data elements using
itemgetter.Performing custom transformations with
RunnableLambda.Creating end-to-end data pipelines with
Runnablechains.
By leveraging these components, users can design scalable and reusable pipelines for machine learning and data processing workflows.
Table of Contents
References
Environment Setup
Set up the environment. You may refer to Environment Setup for more details.
[Note]
langchain-opentutorialis a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.You can checkout the
langchain-opentutorialfor more details.
You can also load the OPEN_API_KEY from the .env file.
Efficient Data Handling with RunnablePassthrough
RunnablePassthrough is a utility designed to streamline data processing workflows by either passing input data unchanged or enhancing it with additional attributes. Its flexibility makes it a valuable tool for handling data in pipelines where minimal transformation or selective augmentation is required.
Simple Data Forwarding
Suitable for scenarios where no transformation is required, such as logging raw data or passing it to downstream systems.
Dynamic Data Augmentation
Enables the addition of metadata or context to input data for use in machine learning pipelines or analytics systems.
RunnablePassthroughcan either pass the input unchanged or append additional keys to it.When
RunnablePassthrough()is called on its own, it simply takes the input and passes it as is.When called using
RunnablePassthrough.assign(...), it takes the input and adds additional arguments provided to the assign function.
RunnablePassthrough
When invoking the chain with invoke(), the input data must be of type dictionary.
However, with the update to the LangChain library, if the template includes only one variable, it is also possible to pass just the value directly.
Here is an example using RunnablePassthrough.RunnablePassthrough is a runnable object with the following characteristics:
Basic Operation
Performs a simple pass-through function that forwards input values directly to output
Can be executed independently using the
invoke()method
Use Cases
Useful when you need to pass data through chain steps without modification
Can be combined with other components to build complex data pipelines
Particularly helpful when you need to preserve original input while adding new fields
Input Handling
Accepts dictionary-type inputs
Can handle single values as well
Maintains data structure throughout the chain
Here is an example of creating a chain using RunnablePassthrough.
Here is a comparison of the results when using RunnablePassthrough.assign().
RunnablePassthrough.assign()
Combines the key/value pairs from the input with the newly assigned key/value pairs.
Efficient Parallel Execution with RunnableParallel
RunnableParallel is a utility designed to execute multiple Runnable objects concurrently, streamlining workflows that require parallel processing. It distributes input data across different components, collects their results, and combines them into a unified output. This functionality makes it a powerful tool for optimizing workflows where tasks can be performed independently and simultaneously.
Concurrent Execution
Executes multiple
Runnableobjects simultaneously, reducing the time required for tasks that can be parallelized.
Unified Output Management
Combines the results from all parallel executions into a single, cohesive output, simplifying downstream processing.
Flexibility
Can handle diverse input types and support complex workflows by distributing the workload efficiently.
Chains can also be applied to RunnableParallel.
Dynamic Processing with RunnableLambda
RunnableLambda is a flexible utility that allows developers to define custom data transformation logic using lambda functions. By enabling quick and easy implementation of custom processing workflows, RunnableLambda simplifies the creation of tailored data pipelines while ensuring minimal setup overhead.
Customizable Data Transformation
Allows users to define custom logic for transforming input data using lambda functions, offering unparalleled flexibility.
Lightweight and Simple
Provides a straightforward way to implement ad-hoc processing without the need for extensive class or function definitions.
Extracting Specific Keys Using itemgetter
itemgetteritemgetter is a utility function from Python's operator module with the following features and benefits:
Core Functionality
Efficiently extracts values from specific keys or indices in dictionaries, tuples, and lists
Capable of extracting multiple keys or indices simultaneously
Supports functional programming style
Performance Optimization
More efficient than regular indexing for repetitive key access operations
Optimized memory usage
Performance advantages when processing large datasets
Usage in LangChain
Data filtering in chain compositions
Selective extraction from complex input structures
Combines with other Runnable objects for data preprocessing
Last updated