Orchestrating Workflows with LangChain

LangChain RAG 

Learning Outcome

4

Compare traditional LLMs with LangChain RAG applications.

3

Understand the indexing and retrieval phases.

2

Identify the core components of a LangChain RAG system.

1

Understand LangChain RAG and its benefits.

Recall

Memory helps AI remember previous interactions.

Memory maintains conversation history and context.

RAG combines conversation history with retrieved knowledge.

LangChain components work together to process and structure responses.

LangChain RAG combines these components into a complete application.

Documents

Books

Text Splitter

Divides books into sections

Digital catalogue entries 

Embeddings

Vector Database

The Library catalogue

Imagine asking a librarian about Neural Networks. They search the catalogue and give you the relevant page, so you read only what you need.

In LangChain RAG, the same process happens digitally:

Retriever

The librarian

The reader

LLM

Final Response

The answer

Think About It

Like a librarian finding the right page, should AI read an entire 500-page PDF each time you ask, “What is Transfer Learning?”

No—split, embed, and search the document to retrieve only relevant chunks each time. The librarian analogy illustrates this idea.

 What is LangChain RAG ?

LangChain RAG is a framework-based implementation of Retrieval-Augmented Generation that combines document loading, text splitting, embedding generation, vector databases, retrievers, prompt templates, and Large Language Models to generate accurate, context-aware responses from external knowledge sources.

Why Do We Need LangChain RAG?

LangChain simplifies RAG development with ready-made modules for document reading, splitting, embeddings, vector databases, similarity search, prompts, and LLM integration in one workflow.

Manual RAG Devlopment

With LangChain

Manage each Component separately

Ready-made modules.One unified workflow

 Core Components of a LangChain RAG System

1.Document Loader

2.Text Splitter

3.Embedding Model

Imports PDFs, Word documents, CSVs, web pages, text files, and database data to build the application’s knowledge base.

Splits large documents into smaller chunks for better retrieval accuracy, embeddings, and search speed.

Converts each chunk into a meaning-based vector, placing similar ideas close together in vector space.

Stores document embeddings and associated text for fast similarity search (e.g., Chroma, FAISS, Pinecone, Milvus, Weaviate, Qdrant).

Embeds the user’s query, searches the vector database, and retrieves relevant chunks—the RAG system’s search engine.

Combines retrieved chunks and the user’s question into a structured prompt for a context-based answer.

4.Vector Database

 5.Retriever

6.Prompt Template

7.Large Language Model

8.Output Parser

Uses retrieved documents as evidence to understand the user’s question and generate a grounded response beyond pre-trained knowledge.

Formats responses as plain text, JSON, lists, or tables for software integration.

How LangChain RAG Works: Phase 1 – Indexing

This phase happens once, ahead of time, to prepare the knowledge base for fast retrieval later.

How LangChain RAG Works: Phase 2 – Retrieval

Conceptual Code Flow

Load Documents

 Split Documents

Generate Embeddings

Store in Vector Database

Create Retriever

Create Prompt Template

Connect LLM

Create RAG Chain

Ask Questions

Generate Responses.

Code

loader = PyPDFLoader("guidelines.pdf") 
docs = loader.load() 
chunks = RecursiveCharacterTextSplitter(chunk_size=500).split_documents(docs) 
vectorstore = Chroma.from_documents(chunks, OpenAIEmbeddings()) 
retriever = vectorstore.as_retriever() 
prompt = PromptTemplate.from_template("Context: {context}\n\nQuestion: {question}") 
rag_chain = {"context": retriever, "question": RunnablePassthrough()} | prompt | llm 
rag_chain.invoke("What is Transfer Learning?") 

LangChain’s modular design lets you swap, update, or customise each step without rebuilding the entire pipeline.

Applying LangChain RAG (Core Mechanism)

A hospital updates its treatment guidelines every month. Which system would provide more accurate and current answers — a standalone LLM trained last year, or a LangChain RAG application connected to the latest hospital documents?

Key Point

A LangChain RAG application connected to the latest documents will consistently give more accurate and current answers than a standalone LLM relying only on frozen training knowledge.

Real-Life Examples

University Chatbot

LangChain retrieves relevant admission rules, exam guidelines, and syllabus sections so the LLM can answer students’ questions accurately.

Research Assistant

For “Explain Transformer Architecture,” the Retriever finds relevant chunks from uploaded papers, and the LLM summarises them clearly.

Customer Support

LangChain retrieves router reset instructions from uploaded manuals, and the LLM provides step-by-step guidance.

Healthcare Assistant

The Retriever finds relevant clinical recommendations, and the LLM provides doctors with an explanation grounded in that evidence.

Daily Life Applications

      Uploading a textbook PDF and asking an AI tool specific questions about its content instead of searching manually.  

       Asking a company's AI assistant a question and getting an answer grounded in the actual, current employee handbook.

A research tool answering questions using your own uploaded papers rather than general knowledge alone.

    A product support assistant giving step-by-step troubleshooting drawn directly from the actual product manual.  

 Traditional LLM vs LangChain RAG (Comparison)

An Easy Way to Remember

Think of preparing for an open-book examination:

Books = Documents  

Bookmarks = Embeddings

Library = Vector Database  

Student = LLM

Answer Sheet = Final Response

Question Paper = Prompt Template

Librarian = Retriever

Summary

5

RAG uses external knowledge, reduces hallucinations, and stays current.

4

Modular components can be swapped or customized independently.

3

Retrieval queries, retrieves, augments, and generates responses.

2

Indexing loads, splits, embeds, and stores documents.

1

LangChain RAG combines multiple components into one pipeline.

Quiz

A hospital needs its AI assistant to use monthly updated treatment guidelines. Which approach avoids retraining the LLM?

A. Fine-tune the LLM monthly

B. Use LangChain RAG & update the vector database

C. Rely on pre-trained knowledge

D. Manually rewrite responses

Quiz-Answer

A hospital needs its AI assistant to use monthly updated treatment guidelines. Which approach avoids retraining the LLM?

A. Fine-tune the LLM monthly

B. Use LangChain RAG & update the vector database

C. Rely on pre-trained knowledge

D. Manually rewrite responses