...

/

Composition Strategies: Chain Constructor Explanation

Composition Strategies: Chain Constructor Explanation

Learn everything you need to know about chains with LangChain

LangChain chains

Chains are integral components in the architecture of language understanding systems, acting as sequences of calls that interact with large language models (LLMs), tools, or data preprocessing steps. LangChain excels in constructing these chains as It offers a robust environment known as LCEL (LangChain Chain Execution Language) that facilitates the custom creation of chains tailored to specific workflows or ready-to-use off-the-shelf chain constructors.

Press + to interact
RAG workflow: Chain constructor
RAG workflow: Chain constructor

These pre-built chains are designed to streamline the integration and deployment of complex sequences for chatbots’ output or response, making it easier for developers to leverage advanced language processing features without the need for extensive customization.

There are two types of off-the-shelf chains that LangChain supports.

Legacy chains

Legacy chains in LangChain represent a foundational approach to structuring sequences of operations in language processing tasks. These chains are created by sub-classing from a dedicated legacy chain class, making them distinct and standalone classes within the framework. This method encapsulates specific functionalities and workflows into manageable, reusable components. Legacy chains provide a structured way to handle complex sequences of calls to large language models (LLMs), tools, or data preprocessing steps, each tailored to specific tasks or requirements. By using these predefined classes, developers can leverage established patterns and practices within LangChain, facilitating easier implementation and maintenance of language-based applications. The availability of various legacy chains allows for a broad range of applications, from simple data handling to intricate model interactions, supporting a versatile deployment of language technologies in diverse environments.

Chain types

Below is a table with some of the legacy chains:

Chain

Usage

ConversationalRetrievalChain

This chain can be used to have conversations with a document. It takes in a question and (optional) previous conversation history. If there is a previous conversation history, it uses an LLM to rewrite the conversation into a query to send to a retriever (otherwise, it just uses the newest user input). It then fetches those documents and passes them (along with the conversation) to an LLM to respond.

StuffDocumentsChain

This chain takes a list of documents and formats them all into a prompt, then passes that prompt to an LLM. It passes ALL documents, so we should make sure it fits within the context window of the LLM you are using.

ReduceDocumentsChain

This chain combines documents by iteratively reducing them. It groups documents into chunks (less than some context length) and passes them into an LLM. It then takes the responses and continues to do this until it can fit everything into one final LLM call. This is useful when we have a lot of documents and want to have the LLM run over all of them, and we can do this in parallel.

MapRerankDocumentsChain

This calls on LLM on each document, asking it to not only answer but also produce a score of how confident it is. The answer with the highest confidence is then returned. This is useful when we have a lot of documents, but only want to answer based on a single document, rather than trying to combine answers.

LLMMath

This chain converts a user question to a math problem and then executes it.

QAGenerationChain

This chain creates both questions and answers from documents. It can be used to generate question/answer pairs for the evaluation of retrieval projects.

RetrievalQA

This chain first does a retrieval step to fetch relevant documents, then passes those documents into an LLM to generate a response.

LLMRouterChain

This chain uses an LLM to route between potential options.

It is worth noting that LangChain is gradually moving all the chain frameworks towards the LangChain expression language (LCEL), which we will discuss next. At some point, the legacy chains will eventually be deprecated.

LCEL Chains

LangChain’s LCEL (LangChain Chain Execution Language) represents a sophisticated method for constructing and managing chains of operations in language processing tasks. LCEL provides a higher-level constructor method and a declarative approach to seamlessly compose various chains, simplifying the ...

Access this course and 1400+ top-rated courses and projects.