LCEL Interface
Author: JeongGi Park
Peer Review: YooKyung Jeon, Wooseok Jeong
Proofread : Q0211
This is a part of LangChain Open Tutorial
Overview
The LangChain Expression Language (LCEL) is a powerful interface designed to simplify the creation and management of custom chains in LangChain. It implements the Runnable protocol, providing a standardized way to build and execute language model chains.
Table of Contents
References
Environment Setup
Set up the environment. You may refer to Environment Setup for more details.
[Note]
langchain-opentutorialis a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.You can checkout the
langchain-opentutorialfor more details.
set environment variables is in .env.
Copy the contents of .env_sample and load it into your .env with the key you set.
LCEL Runnable Protocol
To make it as easy as possible to create custom chains, we've implemented the Runnable protocol.
The Runnable protocol is implemented in most components.
It is a standard interface that makes it easy to define custom chains and call them in a standard way. The standard interface includes
stream: Streams a chunk of the response.invoke: Invoke a chain on an input.batch: Invoke a chain against a list of inputs.
There are also asynchronous methods
astream: Stream chunks of the response asynchronously.ainvoke: Invoke a chain asynchronously on an input.abatch: Asynchronously invoke a chain against a list of inputs.astream_log: Streams the final response as well as intermediate steps as they occur.
Log your trace
We provide multiple ways to log traces to LangSmith. Below, we'll highlight how to use traceable().
Use the code below to record a trace in LangSmith
Create a chain using LCEL syntax.
stream: real-time output
This function uses the chain.stream method to create a stream of data for a given topic, iterating over it and immediately outputting the content of each piece of data.
The end="" argument disables newlines after output, and the flush=True argument causes the output buffer to be emptied immediately.
Invoke
The invoke method of a chain object takes a topic as an argument and performs processing on that topic.
batch: unit execution
The function chain.batch takes a list containing multiple dictionaries as arguments and performs batch processing using the values of the topic key in each dictionary.
You can use the max_concurrency parameter to set the number of concurrent requests.
The config dictionary uses the max_concurrency key to set the maximum number of operations that can be processed concurrently. Here, it is set to process up to three jobs concurrently.
async stream
The function chain.astream creates an asynchronous stream and processes messages for a given topic asynchronously.
It uses an asynchronous for loop (async for) to sequentially receive messages from the stream, and the print function to immediately print the contents of the messages (s.content). end="" disables line wrapping after printing, and flush=True forces the output buffer to be emptied to ensure immediate printing.
async invoke
The ainvoke method of a chain object performs an operation asynchronously with the given arguments. Here, we are passing a dictionary with a key named topic and a value named NVDA (NVIDIA's ticker) as arguments. This method can be used to asynchronously request processing for a specific topic.
async batch
The function abatch batches a series of actions asynchronously.
In this example, we are using the abatch method of the chain object to asynchronously process actions on topic .
The await keyword is used to wait for those asynchronous tasks to complete.
Parallel
Let's take a look at how the LangChain Expression Language supports parallel requests. For example, when you use RunnableParallel (often written in dictionary form), you execute each element in parallel.
Here's an example of running two tasks in parallel using the RunnableParallel class in the langchain_core.runnables module.
Create two chains (chain1, chain2) that use the ChatPromptTemplate.from_template method to get the capital and area for a given country.
These chains are connected via the model and pipe (|) operators, respectively. Finally, we use the RunnableParallel class to combine these two chains with the keys capital and area to create a combined object that can be run in parallel.
The chain1.invoke() function calls the invoke method of the chain1 object.
As an argument, it passes a dictionary with the value Canada in the key named country.
Call chain2.invoke(), this time passing a different country, the United States, for the country key.
The invoke method of the combined object performs the processing for the given country.
In this example, the topic USA is passed to the invoke method to run.
Parallelism in batches
Parallelism can be combined with other executable code. Let's try using parallelism with batch.
The chain1.batch function takes a list containing multiple dictionaries as an argument, and processes the values corresponding to the "topic" key in each dictionary. In this example, we're batch processing two topics, "Canada" and "United States".
The chain2.batch function takes in multiple dictionaries as a list and performs batch processing.
In this example, we request processing for two countries, Canada and the United States.
The combined.batch function is used to process the given data in batches.
In this example, it takes a list containing two dictionary objects as arguments and batches data for two countries, Canada and the United States, respectively.
Last updated