RunnableLambda.
Objectives
- Dynamically append processing steps to an existing LCEL chain.
- Build custom transformers (such as uppercasing and word counting) using
RunnableLambda. - Understand how custom logic execution behaves within a sequence.
LCEL Chains Extension Plan
Goal
Extend a standard prompt-model-parser chain with custom formatting functions to convert the output to uppercase and count the total words in the response.Sample Input
Sample Output
An uppercase text report stating the word count followed by the jokes.Plan
- Initialize prompt, model, and string output parser.
- Define a
RunnableLambdato convert response strings to uppercase. - Define another
RunnableLambdathat splits the output, counts the words, and prepends the count metadata. - Construct the extended LCEL chain:
chain = prompt_template | model | StrOutputParser() | uppercase_output | count_words. - Invoke the chain.
Code Implementation
The following example shows a pipeline where the prompt formats, the model generates jokes, the string parser extracts text, and two customRunnableLambda steps convert the text to uppercase and count the total words:
Practice & Exercises
To reinforce what you’ve learned in this section, practice with the interactive notebook:Practice & Exercises
Practice extending sequences with downstream functions, modifying text formats, and adding analytical steps.💻 VS Code | 🚀 Colab | 📥 Download