💻 Practice Notebook
Master the concepts from this page with hands-on practice: 💻 VS Code | 🚀 Colab | 📥 Download Notebook LangChain Expression Language (LCEL) is a declarative way to build LLM applications, allowing you to compose components using the pipe operator (|).
1. What is LCEL & Why Use It?
Instead of writing imperative code to link prompts, models, and parsers, you connect them together like a Unix pipeline:Why Use LCEL?
- Simple: Chain complex components in just a few lines of code.
- Readable: Easy to inspect the flow of inputs and outputs.
- Composable: Swap prompts, LLMs, retrievers, or parsers effortlessly.
- Built-in Support: Handles streaming and parallel operations out of the box.
2. LCEL Code Examples
Example 1: Basic QA Chain
A simple chain that takes a topic, formats a prompt, invokes the model, and extracts the response content.Example 2: Subject Line Generator
Generate a professional email subject line using dynamic topic and tone variables.3. Exercises for LCEL
Exercise 1: Marketing Pitch Generator
Create a chain that takes a product name and a target audience and generates a catchy marketing slogan. Instructions:- Import
PromptTemplateandinit_chat_model. - Define a string prompt template containing two variables:
{product_name}and{target_audience}. - Initialize the Groq model
llama-3.3-70b-versatile. - Compose an LCEL chain linking the prompt template and the chat model.
- Invoke the chain passing a dictionary with values for
"product_name"(e.g.,"EcoWater Bottle") and"target_audience"(e.g.,"fitness enthusiasts"). - Print the model’s text response using
.content.
Solution
Solution
Exercise 2: Tech Tag Extractor
Create a chain that takes an article excerpt and lists the top 3 technology keywords mentioned. Instructions:- Import
PromptTemplateandinit_chat_model. - Define a string prompt template containing a variable
{text}that asks the model to list the top 3 technology keywords mentioned in the text. - Initialize the Groq model
llama-3.3-70b-versatile. - Compose an LCEL chain linking the prompt template and the chat model.
- Invoke the chain passing a dictionary containing a sample text paragraph.
- Print the model’s text response using
.content.
Solution
Solution
4. Invoking vs. Streaming
4.1 invoke()
Waits for the entire model execution to complete and returns the full response at once.
4.2 stream()
Yields the response progressively, token-by-token. This is crucial for interactive chat interfaces to improve perceived user latency.
Invocation Input Cheat Sheet
5. LangChain Runnables
A Runnable is the fundamental building block in LangChain. Any component that implementsinvoke(), batch(), or stream() is a Runnable.
5.1 RunnableSequence
Chains multiple runnables sequentially so the output of one component becomes the input of the next. The pipe operator (|) automatically creates a RunnableSequence.
5.2 RunnablePassthrough
Forwards the input value as-is. This is useful for passing unchanged variables down a chain or creating multi-keyed inputs.5.3 RunnableParallel
Executes multiple runnables concurrently on the same input, returning their outputs as a unified dictionary.6. Practice Exercises
Practice 1: Basic LCEL Translation Pipeline
Create a simple LCEL chain combining a prompt template ("Translate the word '{word}' into German.") and a chat model. Invoke it with the word "apple" and print the response content.
Solution
Solution