
10 Useful LangChain Components for Your Next RAG System
Image by Editor | Midjourney
LangChain is a robust framework conceived to simplify the developing of LLM-powered applications — with LLM, of course, standing for large language model. Its versatile components allow for the integration of LLMs into several workflows, including retrieval augmented generation (RAG) systems, which combine LLMs with external document bases to provide more accurate, contextually relevant, and factually correct responses by retrieving up-to-date relevant information from the document base.
This article overviews 10 of the most popular building blocks in LangChain you may want to consider if you are keen on building RAG systems using this powerful framework. From data ingestion to retrieval approaches, and orchestration with LLMs, we will uncover some key component to look at.
1. Document Loaders
Document loaders are LangChain components utilized for data ingestion from various sources like TXT or PDF files, web pages, or CSV files. They also support connectors to load files from storage systems or databases through APIs. Specific examples of document loaders include PyPDFLoader, UnstructuredFileLoader, and WebBaseLoader.
2. Text Splitters
Text splitters are responsible for breaking large ones into more manageable text portions, usually called chunks, for efficient processing and faster indexing, once documents are loaded. LangChain’s text splitters support custom chunk sizes and even overlap among chunks to avoid context loss. Popular types of text splitters include RecursiveCharacterTextSplitter and TokenTextSplitter.
3. Embeddings
Embeddings are an effective way to numerically encode text while keeping their semantic properties, namely via dense vector representations. In RAG systems, embeddings are a cornerstone to performing similarity-based search: embedding vectors that are close to each other should indicate they represent similar texts. LangChain supports integration with widely used embedding models like OpenAI models through its API, and embedding approaches for sentence transformers through Hugging Face. The classes for these are OpenAIEmbeddings and HuggingFaceEmbeddings.
4. Vector Stores
Vector stores are components are used for storing and retrieving embeddings in similarity-based search processes. Some specific components are available in LangChain to manage vector databases like Pinecone, Chroma, and FAISS.
5. Retrievers
Retrievers are a centerpiece component of RAG systems, the retriever is responsible for searching in vector stores and retrieving the most relevant chunk embeddings for an input query. In LangChain, they can be tuned for hybrid retrieval methods, e.g. combining sparse and dense search. Example components to implement a retriever with LangChain include SimilarityRetriever and HybridRetriever.
6.LLM Wrappers
LLM wrappers are the interfaces with LLMs for RAG systems focused on text generation. Through APIs, it allows calling models as widespread as OpenAI’s GPT models, or Anthropic’s Claude models, as well as local pre-trained models from Hugging Face (class HuggingFaceHub).
7. Chains
Chains are a key element in LangChain as a whole, a chain is a workflow that combines multiple NLP components. One example we will see later is question-answering chains, which can link retrievers with LLMs to produce answers based on retrieved knowledge. There are various LangChain classes to support specific types of chains for diverse scenarios, for instance, RetrievalQA and ConversationalRetrievalChain.
8. Memory Usage
Memory usage components help control the state and context of the conversation across interactions, and it is indispensable in chat-based RAG systems. They are controlled with abstractions like ConversationBufferMemory and VectorStoreMemory.
9. Interaction Tools & Agents
Interaction tools and agents are these advanced components enable LLMs in RAG systems to interact with external systems for addressing more challenging tasks based on agents that dynamically select the most appropriate tool(s) for each specific problem. The Tool and ZeroShotAgent classes are used for this end.
10. Evaluation
Evaluation in LangChain means to assess the performance of an RAG pipeline, and LangChain components like QA Eval Chain allow iteratively tuning retrieval and generation mechanisms and their benchmarking.
Example: Putting it (Almost) All Together
For a gentle demonstration of their use in Python code, below is a complete example of a very simplified RAG workflow for question-answering that puts together some of the LangChain components we discussed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
from langchain.document_loaders import TextLoader from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.embeddings import OpenAIEmbeddings from langchain.vectorstores import FAISS from langchain.chains import RetrievalQA from langchain.llms import OpenAI Â # 1. Loading a sample document loader = TextLoader(“sample.txt”) documents = loader.load() Â # 2. Splitting document into chunks text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50) chunks = text_splitter.split_documents(documents) Â # 3. Converting chunks into vector embeddings and storing them embeddings = OpenAIEmbeddings() vector_store = FAISS.from_documents(chunks, embeddings) Â # 4. Initializing a retriever retriever = vector_store.as_retriever(search_type=“similarity”, search_kwargs=“k”: 3) Â # 5. Building a retrieval-augmented QA chain llm = OpenAI(model=“gpt-3.5-turbo”) qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=retriever, return_source_documents=True) Â # 6. Asking questions query = “What are the key points in the document?” result = qa_chain.run(query) Â # 7. Displaying results print(“Answer:”, result[“result”]) print(“Sources:”, [doc.metadata[“source”] for doc in result[“source_documents”]]) |
A few important aspects to take into account for a smooth code execution:
- If running it on a notebook, you’ll need to first install the community version of LangChain, by running:
!pip install -U langchain-community
- You’ll need a sample .txt document of your own
- You’ll also necessitate an OpenAI API key to use certain classes like OpenAIEmbeddings
Wrapping Up
This article introduced 10 essential types of components in the extensive and robust LangChain framework to consider when building effective RAG systems, spanning elements and processes like knowledge retrieval, text embeddings, interaction with LLMs and external systems, and so on. Through an example, we illustrated how to put some of these components together to define a simplified question-answering RAG-based workflow.