...

/

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 transition from prototype to production without requiring any code changes. This flexibility is essential for deploying everything from simple “prompt + LLM” chains to the most complex sequences required for advanced applications.

Chain types

Below is a table with some of the LCEL chains:

Chain

Usage

create_stuff_documents_chain

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 we are using.

create_openai_fn_runnable

If we want to use OpenAI function calling to optionally structure an output response, we may pass in multiple functions for it to call, but it does not have to.

create_structured_output_runnable

If we want to use OpenAI function calling to force the LLM to respond with a certain function. We may only pass in one function; the chain will always return this response.

load_query_constructor_runnable

This chain can be used to generate queries. We must specify a list of allowed operations, and then will return a runnable that converts a natural language query into those allowed operations.

create_sql_query_chain

If we want to construct a query for a SQL database from natural language.

create_history_aware_retriever

This chain takes in conversation history and then uses that to generate a search query which is passed to the underlying retriever.

create_retrieval_chain

This chain takes in a user inquiry, which is then passed to the retriever to fetch relevant documents. Those documents (and original inputs) are then passed to an LLM to generate a response

Key benefits

LCEL is the preferred way of utilizing ...