
LANGCHAIN — What Is Langchain Expression Language?
In the software world, the moment you start using someone else’s software, you are living in their world, under their philosophy. — Richard Stallman
LangChain Expression Language (LCEL) is a new syntax that allows the creation of chains with composition. LCEL supports batch, async, and streaming out of the box and is designed to make it easier to construct complex chains. The LCEL syntax is aimed at improving the construction of language model applications by making it easier to perform tasks such as chaining multiple LLM calls, constructing the input to LLMs, and using the output of LLMs.
To illustrate the usage of LangChain Expression Language, let’s take a look at a common way of using it:
from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
model = ChatOpenAI()
prompt = ChatPromptTemplate.from_template("tell me a joke about {foo}")
chain = prompt | model
chain.invoke({"foo": "bears"})In this example, a standard ChatOpenAI model and a prompt template are chained together using the | operator, and then invoked with chain.invoke. Additionally, LCEL supports batch, stream, and async operations:
Batch
chain.batch([{"foo": "bears"}, {"foo": "cats"}])Stream
for s in chain.stream({"foo": "bears"}):
print(s.content, end="")Async
await chain.ainvoke({"foo": "bears"})By using LangChain Expression Language, complex chains can be created that integrate seamlessly with LangSmith. This allows for easier customization of the chain components and makes the prompts more prominent and easily swappable. LangChain has also introduced a “LangChain Teacher” application to help users learn the basics of getting started with LangChain Expression Language.
In conclusion, LangChain Expression Language provides a declarative and composable way to create chains, making it easier to work with language models and create complex, customizable applications. The new syntax supports batch, async, and streaming operations out of the box, and integrates seamlessly with LangSmith.
It’s important to note that LangChain Expression Language is constantly evolving with more examples and functionality, so users are encouraged to provide feedback and suggestions for further improvements.




