šŸ“• Build AI Agents using LangGraph.js is now out!

LangChain LCEL Introduction (JavaScript Version)

The LangChain Expression Language, or LCEL for short, allows us to write minimalist code to build chains.

Let's explore a basic example of how LCEL code looks. We will create an LCEL chain to ask a basic query to a ChatOpenAI model.

First, we need to create the instance variables that will become the links in the chain:

const model = new ChatOpenAI()
const prompt = ChatPromptTemplate.fromTemplate(
    `What country is {celebrity} from? Tell me the {fact} of this country?`
)
const parser = new StringOutputParser()

The next step is to compose our chain using the pipe() function:

const chain = prompt.pipe(model).pipe(parser)

Here, we pass the prompt to the model, and the result is then passed to the output parser.

Finally, we can invoke the chain and provide the values for the parameters (if any):

const response = await chain.invoke({
    celebrity: `Shakira`, 
    fact: `population`
})
// response value šŸ‘‡šŸ‘‡šŸ‘‡
// Shakira is from Colombia. The population of Colombia is approximately 50 million.

The more complex the chain, the more evident the advantages of using LCEL become.

Runnables are the core concept of the LangChain Expression Language. Each chain element is a runnable.

To be chainable, a runnable must expose two essential methods:

  • a method that is called when the execution of the chain reaches that runnable. In most cases, this is the invoke() method, but there are alternatives like stream() or batch().
  • the pipe() method, which passes the output of one runnable to the next.

In the example above, all elements in the chain — ChatOpenAI , StringOutputParser, and ChatPromptTemplate — expose these methods.

As a side note, in the Python version of LangChain, the pipe() method is replaced by the pipe | operator:

// in Python
chain = prompt | model | parser

// in JavaScript
chain = prompt.pipe(model).pipe(parser)

The most commonly used runnables are:

  • RunnablePassthrough: a utility runnable used for constructing more complex chains. It allows inputs to pass through unchanged. You will often see RunnablePassthrough used with RunnableMap or RunnableParallel.
  • RunnableParallel and RunnableMap: used to run the same process with different inputs. For example, they can be used to translate a sentence into multiple languages, as explained in this post.
  • RunnableLambda: adds custom functions to LCEL chains, allowing manipulation of the data that flows between the chain's links. I've written a separate post about the basics of RunnableLambda here.

Additionally, there are many other runnables, such as RunnableBranch (example 1 and example 2) and RunnableSequence. Each has specific use cases, but the ones mentioned above are fundamental building blocks for using LCEL in LangChain with JavaScript.

As always, you can find the code used in this example on my GitHub. Happy coding!

šŸ“– Build a full trivia game app with LangChain

Learn by doing with this FREE ebook! This 35-page guide walks you through every step of building your first fully functional AI-powered app using JavaScript and LangChain.js

šŸ“– Build a full trivia game app with LangChain

Learn by doing with this FREE ebook! This 35-page guide walks you through every step of building your first fully functional AI-powered app using JavaScript and LangChain.js


Leave a Reply

Your email address will not be published. Required fields are marked *