skip to Main Content
Join Us for Comet's Annual Convergence Conference on May 8-9:

LCEL: A Guide to LangChain Expression Language

Streamlining Text Processing and Interaction with Advanced Language Models

Photo by Nik on Unsplash

Introduction to LCEL

The LangChain Expression Language (LCEL) is a pivotal addition to the LangChain toolkit, designed to enhance the efficiency and flexibility of text processing tasks.

This guide provides an in-depth overview of LCEL’s capabilities, from its initial setup to its advanced functionalities. With LCEL, users can adopt a declarative approach to chain composition, facilitating operations like streaming, batch processing, and asynchronous tasks. Its modular architecture also allows for easy customization and modification of chain components.

Dive in to understand how LCEL can optimize your interactions with language models and streamline your text-processing workflows.

%%capture
!pip install langchain openai
import os
import getpass
os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter Your OpenAI API Key:")

LangChain Expression Language

This is a declarative way to truly compose chains — and get streaming, batch, and async support out of the box.

You can use all the same existing LangChain constructs to create them.

In addition to standardizing interfaces, another advantage of LangChain is that it makes it simpler for users to personalize different parts of the chain. The chain’s declarative and modular nature allows for easy components swapping. Furthermore, the prompts are now more visible and can be easily modified to suit specific use cases.

The prompts in LangChain are only defaults and should be changed for production applications. Previously, modifying the prompts was challenging due to them being hidden.

Still, with LCEL, they are more prominent and easier to adjust.

This interface defines custom chains and enables standardized invocation.

The standard interface exposed includes:

stream: stream back chunks of the response

invoke: call the chain on an input

batch: call the chain on a list of inputs

These also have corresponding async methods:

astream: stream back chunks of the response async

ainvoke: call the chain on an input async

  • abatch: call the chain on a list of inputs async

Want to learn how to build modern software with LLMs using the newest tools and techniques in the field? Check out this free LLMOps course from industry expert Elvis Saravia of DAIR.AI.


PromptTemplate + LLM and LCEL

A PromptTemplate -> LLM is a core chain that is used in most other larger chains/systems.

from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate

model = ChatOpenAI()

prompt = ChatPromptTemplate.from_template("write a rap about {thing}")

chain = prompt | model

chain.invoke({"thing":"bears"})
AIMessage(content="Verse 1:\nYo, listen up, I got a story to tell,\n'Bout some creatures that you know so well,\nThey're big, they're fierce, they roam the land,\nThe kings of the wilderness, the bears command.\n\nChorus:\nBears, bears, they're everywhere,\nFrom the polar ice to the forest lair,\nThey're strong, they're wild, they're never scared,\nSo let me tell you 'bout these beasts, beware!\n\nVerse 2:\nFirst up, we got the grizzly bear,\nWith its powerful paws, it's a true nightmare,\nIn the mountains and the woods, it roams free,\nA symbol of strength, a sight to see.\n\nNext, we got the polar bear, so rare,\nIn the Arctic, it's the ultimate player,\nSurviving in the cold, icy terrain,\nCatching seals, they're the kings of the domain.\n\nChorus:\nBears, bears, they're everywhere,\nFrom the polar ice to the forest lair,\nThey're strong, they're wild, they're never scared,\nSo let me tell you 'bout these beasts, beware!\n\nVerse 3:\nBut let's not forget the panda bear,\nCuteness overload, with black and white hair,\nMunching on bamboo, living in peace,\nAn emblem of harmony, let's give them some space.\n\nLastly, we got the Kodiak bear,\nThe largest of all, a true heavyweight,\nRoaming Alaska, where the wild things are,\nWith a roar that shakes the earth, it's a superstar.\n\nChorus:\nBears, bears, they're everywhere,\nFrom the polar ice to the forest lair,\nThey're strong, they're wild, they're never scared,\nSo let me tell you 'bout these beasts, beware!\n\nVerse 4:\nRespect these creatures, don't mess around,\nThey're a vital part of nature, never let them down,\nLet's protect their habitat, keep them alive,\nFor future generations, let's strive.\n\nSo when you see a bear, give it some space,\nAdmire from a distance, embrace its grace,\nThese majestic beasts, they deserve our care,\nLet's learn from them, and become aware.\n\nChorus:\nBears, bears, they're everywhere,\nFrom the polar ice to the forest lair,\nThey're strong, they're wild, they're never scared,\nSo let me tell you 'bout these beasts, beware!", additional_kwargs={}, example=False)

Batch

Batch takes a list of inputs and performs internal optimizations, such as batching calls to LLM providers.

chain.batch([{"thing":"Jattis"}, {"thing":"morning coffee"}])
[AIMessage(content="(Verse 1)\nYo, listen up, I got a story to tell\n'Bout these fly ladies that I know so well\nThey're fierce, they're strong, they're full of pride\nJattis, the queens, they can't be denied\n\nRocking those suits, representing their roots\nFrom Punjab to the world, breaking all the rules\nWith their colorful turbans, shining like gold\nJattis stand tall, they never fold\n\n(Chorus)\nJattis in the house, breaking it down\nThey're the queens of the town, they wear the crown\nJattis in the house, making moves so smooth\nThey're the definition of power and groove\n\n(Verse 2)\nFrom the fields to the city, they're making their mark\nJattis ain't afraid to chase their dreams in the dark\nBalancing traditions with modern styles\nJattis keep shining with their infectious smiles\n\nWith their bhangra moves, they own the dance floor\nJattis got the rhythm, they never bore\nThey're the life of the party, the center of attention\nJattis bring the heat, no need for intervention\n\n(Chorus)\nJattis in the house, breaking it down\nThey're the queens of the town, they wear the crown\nJattis in the house, making moves so smooth\nThey're the definition of power and groove\n\n(Verse 3)\nJattis, they're the epitome of grace\nWith their strong spirit, they win every race\nThey're educated, ambitious, reaching new heights\nJattis breaking barriers, shining bright lights\n\nThey stand for equality, justice, and pride\nJattis fight for their rights, side by side\nThey're the role models, inspiring the youth\nJattis breaking stereotypes, speaking the truth\n\n(Chorus)\nJattis in the house, breaking it down\nThey're the queens of the town, they wear the crown\nJattis in the house, making moves so smooth\nThey're the definition of power and groove\n\n(Outro)\nSo raise your hands, let's show some love\nTo the Jattis out there, rising above\nThey're the queens of Punjab, the pride of the nation\nJattis, keep shining with your determination.", additional_kwargs={}, example=False),
 AIMessage(content="(Verse 1)\nYo, wake up, it's a brand new day\nGotta kickstart the morning in a caffeinated way\nI grab my mug, fill it up, you know the deal\nCoffee's the fuel that keeps me on the real\n\n(Chorus)\nMorning coffee, you're my energy booster\nAin't no other drink that can make me move faster\nSippin' on java, feeling so alive\nI can conquer anything, I'm ready to thrive\n\n(Verse 2)\nThe aroma hits my nose, it's so divine\nThat rich, bold flavor, it's like a wake-up sign\nSteamin' hot, it warms my soul\nCoffee in my hand, I'm in control\n\n(Chorus)\nMorning coffee, you're my energy booster\nAin't no other drink that can make me move faster\nSippin' on java, feeling so alive\nI can conquer anything, I'm ready to thrive\n\n(Verse 3)\nWhether it's a latte, cappuccino, or just black\nCoffee's got my back, never cut me no slack\nI take a sip, and my mind starts to race\nIdeas flow like a river, at a rapid pace\n\n(Chorus)\nMorning coffee, you're my energy booster\nAin't no other drink that can make me move faster\nSippin' on java, feeling so alive\nI can conquer anything, I'm ready to thrive\n\n(Bridge)\nNo need for sugar or cream, keep it pure and strong\nCoffee in my veins, it's where I belong\nFrom sunrise to sunset, you're my loyal friend\nMorning coffee, you'll be with me till the end\n\n(Chorus)\nMorning coffee, you're my energy booster\nAin't no other drink that can make me move faster\nSippin' on java, feeling so alive\nI can conquer anything, I'm ready to thrive\n\n(Outro)\nSo raise your cup, let's toast to the start\nMorning coffee, you're a work of art\nYou keep me focused, alert, and awake\nThank you for being the fuel that I take.", additional_kwargs={}, example=False)]

Stream

stream returns an iterable that you can consume.

for s in chain.stream({"thing":"The Golden Temple"}):
  print(s.content, end="")
(Verse 1)
Yo, listen up, I got a tale so bold
'Bout a place that shines brighter than gold
In Amritsar, India, let me introduce
The Golden Temple, a spiritual seduce

(Chorus)
Golden Temple, oh, a sight to behold
Sacred and divine, it's a story untold
A haven of peace, where souls unite
Come and feel the blessings, day or night

(Verse 2)
Dome of gold, glistening in the sun
Reflecting the faith of Sikhs, second to none
Built with love and devotion, a symbol so grand
Four doors, welcoming all, you'll understand

(Chorus)
Golden Temple, oh, a sight to behold
Sacred and divine, it's a story untold
A haven of peace, where souls unite
Come and feel the blessings, day or night

(Verse 3)
It's not just about the shimmer and shine
But the Langar, a miracle, oh so fine
A free kitchen, serving meals for all
Breaking barriers, no matter how small

(Chorus)
Golden Temple, oh, a sight to behold
Sacred and divine, it's a story untold
A haven of peace, where souls unite
Come and feel the blessings, day or night

(Verse 4)
Enter the temple, leave your worries behind
Feel the serenity, let your soul unwind
Wash your feet in the holy Amrit Sarovar
Find solace, seek answers, no matter who you are

(Chorus)
Golden Temple, oh, a sight to behold
Sacred and divine, it's a story untold
A haven of peace, where souls unite
Come and feel the blessings, day or night

(Bridge)
The Golden Temple, a beacon so bright
A testament to love, compassion, and light
May it inspire us all, to spread the love
In this world, like the temple above

(Chorus)
Golden Temple, oh, a sight to behold
Sacred and divine, it's a story untold
A haven of peace, where souls unite
Come and feel the blessings, day or night

(Outro)
So let's celebrate, this treasure so rare
The Golden Temple, a place beyond compare
Embrace the harmony, let your spirit soar
In the Golden Temple, forevermore.

PromptTemplate + LLM + OutputParser

We can also add an output parser to quickly transform the raw LLM/ChatModel output into a more workable format.

from langchain.schema.output_parser import StrOutputParser

chain = prompt | model | StrOutputParser()

chain.invoke({"thing":"language models"})
Yeah, language models, they're here to stay
Revolutionizing the game in a major way
AI-powered, they're the future of text
Generating content that leaves you perplexed

GPT-3, the king of the models
Learning from data, breaking all the bottlenecks
From chatbots to translations, it does it all
With its vast knowledge, it stands tall

Training on massive amounts of text
Understanding context, no need to second-guess
Generating paragraphs, rhymes, and prose
Innovating storytelling, striking a pose

From Shakespearean sonnets to modern-day slang
Language models adapt, they can do anything
Creating poetry that resonates with hearts
Or crafting lyrics that make you hit restart

But with great power comes great responsibility
Language models must be used with integrity
Guarding against biases, promoting inclusivity
Ensuring human values, respecting diversity

They're not here to replace, but to enhance
Assisting humans to take a chance
Empowering writers, thinkers, and dreamers
Unleashing creativity, breaking through the barriers

Language models, the future is bright
Unleashing their potential, reaching new heights
With every word they generate, they inspire
A revolution in language, setting the world on fire

In summary, the LangChain Expression Language (LCEL) represents a significant advancement in the realm of text processing, offering a seamless and efficient way to compose and manage chains.

Its modular and declarative nature ensures adaptability to various use cases while simplifying integration and customization processes. As the demands for efficient language model interactions grow, tools like LCEL will undoubtedly become indispensable for developers and researchers alike.

Embracing such innovations will ensure that we remain at the forefront of the rapidly evolving landscape of AI and natural language processing.

Harpreet Sahota

Back To Top