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

RunnableMap and RunnableParallel in LangChain JS

In this article we will explore some JavaScript examples of using LangChain's RunnableMap class. We will create chains that run parallel processes with different inputs, perform data manipulations with custom functions, and translate strings into multiple languages. Let's learn!

By using the RunnableMap in LCEL (LangChain Expression Language), we can run the same process with different inputs.

Let's take a very simple example:

import { 
    RunnableMap, 
    RunnablePassthrough, 
} from "@langchain/core/runnables"

const chain = RunnableMap.from({
    "first": new RunnablePassthrough(), 
    "second": new RunnablePassthrough()
})

const result = await chain.invoke(20)
console.log(result)
// {first: 20, second: 20}

According to the official LangChain docs, the RunnableMap runs a mapping of runnables in parallel and returns a mapping of their outputs.

Using RunnableMap with RunnableLambda

Given that the RunnablePassthrough just forwards the data, the above example will map the unchanged input to the output keys.

While passing unchanged data can be useful in some situations, most likely we will need to perform some manipulations on the data that flows through the chain.

To perform these manipulations, we can mix the RunnableMap with some custom functions created with RunnableLambda. Check out the example below:

import { 
    RunnableMap, 
    RunnablePassthrough, 
    RunnableLambda
} from "@langchain/core/runnables"

const double = input => input * 2

const chain = RunnableMap.from({
    "original": new RunnablePassthrough(), 
    "double": new RunnableLambda({ func: double })
})

const result = await chain.invoke(20)
console.log(result)
// {first: 20, second: 40}

Making a Language Translation Chain with RunnableMap

The above examples were quite simple and isolated, but we can use RunnableMap for more practical use cases.

A useful application for RunnableMap is translating a string into multiple languages.

To build this example, we will use a ChatOpenAI model and a JavaScript StringOutputParser to parse the response object.

Below is the full code of the example:

import { RunnableMap } from "@langchain/core/runnables"
import { ChatOpenAI } from "@langchain/openai"
import { StringOutputParser } from "@langchain/core/output_parsers"
import { ChatPromptTemplate } from "@langchain/core/prompts"
import * as dotenv from "dotenv"

dotenv.config()

const model = new ChatOpenAI({})

const frChain = ChatPromptTemplate.fromTemplate(
    `Translate this to French {input}`
).pipe(model).pipe(new StringOutputParser())

const itChain = ChatPromptTemplate.fromTemplate(
    `Translate this to Italian {input}`
).pipe(model).pipe(new StringOutputParser())

const chain = RunnableMap.from({
    "FR": frChain, 
    "IT": itChain
})

const result = await chain.invoke({ input: 'Good morning, friend!' })
console.log(result)

Running the code will output the following result:

{ 
  IT: 'Buongiorno, amico!', 
  FR: 'Bonjour, mon ami!' 
}

Well, that's it, folks! Hope you have enjoyed this brief intro to using RunnableMap with LangChain JS. You can see the full code 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 *