Skip to main content

Explain Runnables and LCEL in LangChain

 

Understanding Runnables and LCEL in LangChain: Building Modular AI Workflows

LangChain is a powerful framework for building applications powered by large language models (LLMs). It provides tools to create modular, reusable, and scalable AI workflows. Two key concepts in LangChain are Runnables and LangChain Expression Language (LCEL), which enable developers to build complex chains of operations with ease. In this blog post, we’ll explore what Runnables and LCEL are, how they work, and how you can use them to create sophisticated AI pipelines.


1. What are Runnables?

Definition

In LangChain, a Runnable is a basic building block that represents a unit of work or operation. It can be a model, a function, or a chain of operations. Runnables are designed to be modular and composable, allowing you to combine them into more complex workflows.

Types of Runnables

  1. Models: Pre-trained LLMs like GPT, BERT, or custom models.

  2. Functions: Custom Python functions that process input and produce output.

  3. Chains: Sequences of Runnables that execute in a specific order.

Key Features:

  • Modularity: Runnables can be combined to create complex workflows.

  • Reusability: Once defined, Runnables can be reused across different chains.

  • Interoperability: Runnables can work with other LangChain components like prompts, memory, and tools.

Example: Creating a Runnable

python
Copy
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI

# Define a prompt template
prompt = PromptTemplate(
    input_variables=["topic"],
    template="Write a short paragraph about {topic}."
)

# Create a Runnable (LLMChain)
llm = OpenAI(model="gpt-3.5-turbo")
runnable = LLMChain(llm=llm, prompt=prompt)

# Run the Runnable
output = runnable.run(topic="artificial intelligence")
print(output)

2. What is LangChain Expression Language (LCEL)?

Definition

LangChain Expression Language (LCEL) is a declarative way to define and compose Runnables into chains. It provides a simple and intuitive syntax for building complex workflows by chaining together multiple Runnables.

Key Features:

  • Declarative Syntax: Define chains using a clean and readable syntax.

  • Composability: Easily combine multiple Runnables into a single chain.

  • Flexibility: Supports conditional logic, loops, and parallel execution.

Example: Using LCEL to Create a Chain

python
Copy
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI
from langchain.expression_language import chain

# Define prompt templates
prompt1 = PromptTemplate(
    input_variables=["topic"],
    template="Write a short paragraph about {topic}."
)
prompt2 = PromptTemplate(
    input_variables=["text"],
    template="Summarize the following text: {text}"
)

# Define Runnables
llm = OpenAI(model="gpt-3.5-turbo")
runnable1 = LLMChain(llm=llm, prompt=prompt1)
runnable2 = LLMChain(llm=llm, prompt=prompt2)

# Create a chain using LCEL
@chain
def my_chain(topic):
    text = runnable1.run(topic=topic)
    summary = runnable2.run(text=text)
    return summary

# Run the chain
output = my_chain("artificial intelligence")
print(output)

3. Combining Runnables and LCEL

Runnables and LCEL work together seamlessly to create modular and reusable workflows. You can define individual Runnables for specific tasks and then use LCEL to compose them into a larger chain.

Example: Combining Runnables with LCEL

python
Copy
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI
from langchain.expression_language import chain

# Define Runnables
llm = OpenAI(model="gpt-3.5-turbo")

# Runnable 1: Generate a story
prompt1 = PromptTemplate(
    input_variables=["character"],
    template="Write a short story about {character}."
)
runnable1 = LLMChain(llm=llm, prompt=prompt1)

# Runnable 2: Analyze the mood of the story
prompt2 = PromptTemplate(
    input_variables=["story"],
    template="What is the mood of the following story? {story}"
)
runnable2 = LLMChain(llm=llm, prompt=prompt2)

# Runnable 3: Suggest a title
prompt3 = PromptTemplate(
    input_variables=["story"],
    template="Suggest a title for the following story: {story}"
)
runnable3 = LLMChain(llm=llm, prompt=prompt3)

# Create a chain using LCEL
@chain
def story_chain(character):
    story = runnable1.run(character=character)
    mood = runnable2.run(story=story)
    title = runnable3.run(story=story)
    return {"story": story, "mood": mood, "title": title}

# Run the chain
output = story_chain("a brave knight")
print(output)

4. Benefits of Using Runnables and LCEL

Modularity

  • Break down complex workflows into smaller, reusable components.

  • Easily swap out or update individual Runnables.

Readability

  • LCEL provides a clean and intuitive syntax for defining chains.

  • Makes it easier to understand and maintain complex workflows.

Scalability

  • Compose Runnables into larger chains to handle more complex tasks.

  • Supports parallel execution and conditional logic for advanced use cases.


5. Practical Applications

Chatbots

  • Use Runnables to handle different intents (e.g., greeting, answering questions).

  • Compose them into a chatbot chain using LCEL.

Content Generation

  • Create chains for generating and refining content (e.g., blog posts, summaries).

  • Use Runnables for specific tasks like topic generation, writing, and editing.

Data Processing

  • Build chains for processing and analyzing data (e.g., sentiment analysis, summarization).

  • Combine Runnables for data cleaning, transformation, and analysis.


Conclusion

Runnables and LCEL are powerful tools in LangChain that enable developers to build modular, reusable, and scalable AI workflows. By breaking down complex tasks into smaller Runnables and composing them using LCEL, you can create sophisticated pipelines for a wide range of applications.

Whether you’re building chatbots, generating content, or processing data, Runnables and LCEL provide the flexibility and readability you need to succeed.


References:

Comments

Popular posts from this blog

Simple Linear Regression - and Related Regression Loss Functions

Today's Topics: a. Regression Algorithms  b. Outliers - Explained in Simple Terms c. Common Regression Metrics Explained d. Overfitting and Underfitting e. How are Linear and Non Linear Regression Algorithms used in Neural Networks [Future study topics] Regression Algorithms Regression algorithms are a category of machine learning methods used to predict a continuous numerical value. Linear regression is a simple, powerful, and interpretable algorithm for this type of problem. Quick Example: These are the scores of students vs. the hours they spent studying. Looking at this dataset of student scores and their corresponding study hours, can we determine what score someone might achieve after studying for a random number of hours? Example: From the graph, we can estimate that 4 hours of daily study would result in a score near 80. It is a simple example, but for more complex tasks the underlying concept will be similar. If you understand this graph, you will understand this blog. Sim...

What problems can AI Neural Networks solve

How does AI Neural Networks solve Problems? What problems can AI Neural Networks solve? Based on effectiveness and common usage, here's the ranking from best to least suitable for neural networks (Classification Problems, Regression Problems and Optimization Problems.) But first some Math, background and related topics as how the Neural Network Learn by training (Supervised Learning and Unsupervised Learning.)  Background Note - Mathematical Precision vs. Practical AI Solutions. Math can solve all these problems with very accurate results. While Math can theoretically solve classification, regression, and optimization problems with perfect accuracy, such calculations often require impractical amounts of time—hours, days, or even years for complex real-world scenarios. In practice, we rarely need absolute precision; instead, we need actionable results quickly enough to make timely decisions. Neural networks excel at this trade-off, providing "good enough" solutions in seco...

Activation Functions in Neural Networks

  A Guide to Activation Functions in Neural Networks 🧠 Question: Without activation function can a neural network with many layers be non-linear? Answer: Provided at the end of this document. Activation functions are a crucial component of neural networks. Their primary purpose is to introduce non-linearity , which allows the network to learn the complex, winding patterns found in real-world data. Without them, a neural network, no matter how deep, would just be a simple linear model. In the diagram below the f is the activation function that receives input and send output to next layers. Commonly used activation functions. 1. Sigmoid Function 2. Tanh (Hyperbolic Tangent) 3. ReLU (Rectified Linear Unit - Like an Electronic Diode) 4. Leaky ReLU & PReLU 5. ELU (Exponential Linear Unit) 6. Softmax 7. GELU, Swish, and SiLU 1. Sigmoid Function                       The classic "S-curve," Sigmoid squashes any input value t...