Master AI & Build your First Coding Portfolio with SkillReactor | Sign Up Now

Quick Start Guide: RAG with Langchain

Get Started with RAG

This guide demonstrates how to use Langchain with Retrieval-Augmented Generation (RAG) for question answering tasks. We'll cover installation, code examples in Python, and walk you through each step.

Setup

Installation:

pip install langchain langchain-community langchain-core langchain-openai unstructured sentence-transformers chromadb

Create LLM Client:

This code initialises the OpenAI client class for interacting with the OpenAI API.

Python

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    openai_api_key=openai_api_key, # For production this should be ideally set in Env Variables
    openai_api_base="https://your_custom_url.com"
)

Document Loading & Splitting

Process Documents

This code defines the document directory and uses DirectoryLoader to load documents from that location into memory.

Python

from langchain_community.document_loaders import DirectoryLoader

directory = './documents'
loader = DirectoryLoader(directory)
documents = loader.load()

Text Splitting

Large documents need to be split for efficient retrieval. This code splits long documents into smaller chunks for better processing. You can adjust chunk_size and chunk_overlap based on your needs.

Python

from langchain.text_splitter import RecursiveCharacterTextSplitter

text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
split_docs = text_splitter.split_documents(documents)

Create Embeddings

Text needs to be converted into numerical representations (embeddings) that capture their semantic meaning, so semantic retrieval can be performed later. Sentences with similar meanings will have similar embeddings.

Python

from langchain.embeddings import SentenceTransformerEmbeddings
embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")

Loading Documents into Chroma DB

Chroma is a vector store and embeddings database. This code will load our documents into the Chrome store.

Python

from langchain.vectorstores.chroma import Chroma
db = Chroma.from_documents(documents, embeddings)

Retrieving Relevant Content

Once documents are loaded in Chroma, we can search the Chroma store to find matches that are similar to the user's query.

Python

matching_docs = db.similarity_search(query)

Generate Response from LLM

We now have relevant results retrieved from our knowledge base documents, we will pass this along with the user query to our LLM to get the final response.

We can use the load_qa_chain function provided by Langchain to do this.

Python

from langchain.chains.question_answering import load_qa_chain
chain = load_qa_chain(llm, chain_type="stuff", verbose=True)
answer = chain.run(input_documents=matching_docs, question=query)

Summary

In this guide, we've learned how to use Langchain with Retrieval-Augmented Generation (RAG) for question answering tasks. We covered installation, setup, document loading, text splitting, creating embeddings, retrieving relevant content, and generating responses from the Langchain Language Model (LLM). By following these steps, you're equipped to efficiently retrieve and generate accurate responses from your document corpus, empowering you to build intelligent language processing applications with ease.