<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Dharmendra Singh</title>
    <description>The latest articles on Forem by Dharmendra Singh (@dharmendra_singh_786).</description>
    <link>https://forem.com/dharmendra_singh_786</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3287399%2F5b4e5985-dca6-4fd3-92c5-35dd646092c9.png</url>
      <title>Forem: Dharmendra Singh</title>
      <link>https://forem.com/dharmendra_singh_786</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/dharmendra_singh_786"/>
    <language>en</language>
    <item>
      <title>Building RAG Applications with LangChain(Part-5)</title>
      <dc:creator>Dharmendra Singh</dc:creator>
      <pubDate>Tue, 24 Jun 2025 04:04:03 +0000</pubDate>
      <link>https://forem.com/dharmendra_singh_786/building-rag-applications-with-langchainpart-5-2686</link>
      <guid>https://forem.com/dharmendra_singh_786/building-rag-applications-with-langchainpart-5-2686</guid>
      <description>&lt;h2&gt;
  
  
  Prompting, Chaining &amp;amp; Parsing: Structuring Smart, Reliable LLM Workflows
&lt;/h2&gt;

&lt;p&gt;Welcome back to the &lt;strong&gt;LangChain RAG Series&lt;/strong&gt;. So far, we’ve walked through:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://dev.to/dharmendra_singh_786/building-rag-applications-with-langchain-part-1-4f1p"&gt;Part 1&lt;/a&gt;: Understanding RAG Architecture&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://dev.to/dharmendra_singh_786/building-rag-applications-with-langchainpart-2-4p1o"&gt;Part 2&lt;/a&gt;: Document Loaders&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://dev.to/dharmendra_singh_786/building-rag-applications-with-langchainpart-3-54gb"&gt;Part 3&lt;/a&gt;: Text Splitters&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://dev.to/dharmendra_singh_786/building-rag-applications-with-langchainpart-4-38i0"&gt;Part 4&lt;/a&gt;: Embeddings &amp;amp; Vector Stores&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now it’s time to bring it all together using &lt;strong&gt;LangChain’s Prompt Templates&lt;/strong&gt;, &lt;strong&gt;Chains&lt;/strong&gt;, and &lt;strong&gt;Output Parsers&lt;/strong&gt; — the heart of a well-structured LLM pipeline.&lt;/p&gt;

&lt;h2&gt;
  
  
  What This Part Covers
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Writing reusable prompts using &lt;code&gt;ChatPromptTemplate&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Building logic flows with &lt;code&gt;Runnable&lt;/code&gt;-based Chains
&lt;/li&gt;
&lt;li&gt;Parsing raw LLM output into structured results
&lt;/li&gt;
&lt;li&gt;Examples using Gemini, OpenAI, and LangChain Expressions
&lt;/li&gt;
&lt;li&gt;Best practices for scalable and debuggable apps&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Prompt Engineering with LangChain
&lt;/h2&gt;

&lt;p&gt;LangChain supports a modular approach to prompts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Components:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;SystemMessage&lt;/code&gt; — Set behavior and role of the model&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;HumanMessage&lt;/code&gt; — What the user asks&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;AIMessage&lt;/code&gt; — Model-generated replies (can be optional)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;ChatPromptTemplate&lt;/code&gt; — Compose a full message list with placeholders&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from langchain.prompts.chat import ChatPromptTemplate, SystemMessage, HumanMessage

prompt = ChatPromptTemplate.from_messages([
    SystemMessage(content="You are a helpful {domain} expert."),
    HumanMessage(content="Tell me about {topic}.")
])

formatted_prompt = prompt.invoke({"domain": "Cricket", "topic": "reverse swing"})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes your prompts reusable and structured across different topics or domains.&lt;/p&gt;

&lt;h3&gt;
  
  
  Another example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def  ask_query(query):

#get retriever
retriever = vectorstore.as_retriever(search_type='similarity', search_kwargs={'k': 2})
results = retriever.invoke(query)
context = "\n".join([document.page_content for document in results])

prompt = f"""
You are an FAQ assistant. Use the following content to answer the user's question accurately and concisely:
{context}
Q: {query}
A:
"""
response = model.invoke(prompt)
return response.content

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Chains: Connecting Components Using LangChain Expression Language (LCEL)
&lt;/h2&gt;

&lt;p&gt;LangChain &lt;strong&gt;Chains&lt;/strong&gt; combine multiple steps using &lt;strong&gt;runnables&lt;/strong&gt; to manage logic and flow.&lt;/p&gt;

&lt;h3&gt;
  
  
  Runnable Types:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;RunnableSequence:&lt;/strong&gt; Run in sequence for example use case: Prompt → LLM → Parser&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RunnableParallel:&lt;/strong&gt; Run multiple branches in parallel for example use case: Fetch metadata + summary&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RunnableLambda:&lt;/strong&gt; Custom function as part of chain for example use case: Preprocessing inputs&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RunnableBranch:&lt;/strong&gt; If/else-style branching for example use case: Conditional routing&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RunnablePassthrough:&lt;/strong&gt; Pass input as-is for example use case: Default input/return identity&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example: RunnableSequence Chain
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from langchain.schema.runnable import RunnableSequence
from langchain.output_parsers import StrOutputParser
from langchain.llms import OpenAI

chain = RunnableSequence([
    prompt,             # PromptTemplate
    OpenAI(),           # LLM
    StrOutputParser()   # Clean the output
])

result = chain.invoke({"domain": "Cricket", "topic": "swing bowling"})
print(result)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This runs: &lt;strong&gt;Prompt → LLM → OutputParser&lt;/strong&gt;, all in one clean pipeline.&lt;/p&gt;

&lt;p&gt;You can use &lt;strong&gt;LCEL&lt;/strong&gt; in place of &lt;strong&gt;RunnableSequence&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chain = prompt | OpenAI() | StrOutputParser()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Output Parsers: Making Results Structured
&lt;/h2&gt;

&lt;p&gt;By default, LLMs return plain text. LangChain supports &lt;strong&gt;parsers&lt;/strong&gt; to transform this into structured data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Common Parsers:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;StrOutputParser:&lt;/strong&gt;  Clean text string&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CommaSeparatedListOutputParser:&lt;/strong&gt; List from CSV text&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PydanticOutputParser:&lt;/strong&gt; Typed structured objects&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;JsonOutputParser:&lt;/strong&gt; JSON dictionaries&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example with Pydantic:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from langchain.output_parsers import PydanticOutputParser
from pydantic import BaseModel

class Answer(BaseModel):
    topic: str
    summary: str

parser = PydanticOutputParser(pydantic_object=Answer)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can use this in your chain to &lt;strong&gt;enforce structure&lt;/strong&gt; in the output (e.g., for APIs, UIs, or analytics).&lt;/p&gt;

&lt;h2&gt;
  
  
  Real-World Workflow Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;full_chain = prompt | OpenAI() | StrOutputParser()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This structure works for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Document-based QA&lt;/li&gt;
&lt;li&gt;  Summarizers&lt;/li&gt;
&lt;li&gt;  Entity extractors&lt;/li&gt;
&lt;li&gt;  Internal tools with LLM logic&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Best Practices
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;ChatPromptTemplate&lt;/code&gt; for modularity
&lt;/li&gt;
&lt;li&gt;Parse everything — never trust raw LLM strings
&lt;/li&gt;
&lt;li&gt;Chain small functions using &lt;code&gt;RunnableSequence&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Test components independently
&lt;/li&gt;
&lt;li&gt;Branch with &lt;code&gt;RunnableBranch&lt;/code&gt; to manage complex logic&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Coming Up Next: Part 6
&lt;/h2&gt;

&lt;p&gt;In &lt;strong&gt;Part 6&lt;/strong&gt;, we’ll combine everything we’ve built so far into real-world, production-ready examples.&lt;br&gt;&lt;br&gt;
Think of this as your &lt;strong&gt;RAG Starter Toolkit&lt;/strong&gt; in action.&lt;/p&gt;

&lt;h2&gt;
  
  
  Missed the Previous Parts?
&lt;/h2&gt;

&lt;p&gt;👉 &lt;a href="https://dev.to/dharmendra_singh_786/building-rag-applications-with-langchain-part-1-4f1p"&gt;Part 1 – What is RAG&lt;/a&gt;&lt;br&gt;&lt;br&gt;
👉 &lt;a href="https://dev.to/dharmendra_singh_786/building-rag-applications-with-langchainpart-2-4p1o"&gt;Part 2 – Document Loaders&lt;/a&gt;&lt;br&gt;&lt;br&gt;
👉 &lt;a href="https://dev.to/dharmendra_singh_786/building-rag-applications-with-langchainpart-3-54gb"&gt;Part 3 – Text Splitters&lt;/a&gt;&lt;br&gt;&lt;br&gt;
👉 &lt;a href="https://dev.to/dharmendra_singh_786/building-rag-applications-with-langchainpart-4-38i0"&gt;Part 4 – Embeddings &amp;amp; Vectors&lt;/a&gt;&lt;/p&gt;

</description>
      <category>langchain</category>
      <category>langgraph</category>
      <category>rag</category>
      <category>ai</category>
    </item>
    <item>
      <title>Building RAG Applications with LangChain(Part-4)</title>
      <dc:creator>Dharmendra Singh</dc:creator>
      <pubDate>Mon, 23 Jun 2025 17:04:07 +0000</pubDate>
      <link>https://forem.com/dharmendra_singh_786/building-rag-applications-with-langchainpart-4-38i0</link>
      <guid>https://forem.com/dharmendra_singh_786/building-rag-applications-with-langchainpart-4-38i0</guid>
      <description>&lt;h2&gt;
  
  
  Embeddings &amp;amp; Vector Stores: Turning Text into Searchable Intelligence
&lt;/h2&gt;

&lt;p&gt;Welcome to &lt;strong&gt;Part 4&lt;/strong&gt; of our hands-on RAG series with LangChain.&lt;/p&gt;

&lt;p&gt;So far, we’ve covered:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://dev.to/dharmendra_singh_786/building-rag-applications-with-langchain-part-1-4f1p"&gt;Part 1&lt;/a&gt;: RAG Theory and Architecture&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://dev.to/dharmendra_singh_786/building-rag-applications-with-langchainpart-2-4p1o"&gt;Part 2&lt;/a&gt;: Document Loaders&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://dev.to/dharmendra_singh_786/building-rag-applications-with-langchainpart-3-54gb"&gt;Part 3&lt;/a&gt;: Text Splitters&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this part, we explore how your split documents are &lt;strong&gt;converted into vectors (Embeddings)&lt;/strong&gt; that can be &lt;strong&gt;searched, ranked, and retrieved&lt;/strong&gt; using LLMs.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Are Embeddings?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Embeddings&lt;/strong&gt; are numerical vector representations of text that capture &lt;strong&gt;semantic meaning&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When a model generates an embedding:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It transforms “meaning” into a &lt;strong&gt;dense numerical vector&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Similar meanings = &lt;strong&gt;closer vectors&lt;/strong&gt; in multi-dimensional space&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Example:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;king - man + woman ≈ queen&lt;/code&gt;&lt;br&gt;&lt;br&gt;
This analogy works because embeddings preserve &lt;strong&gt;relational meaning&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Lets understand with some examples&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Let’s say you have a model that creates  embeddings for words.&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;king &amp;lt;-------Embeddings---------&amp;gt;         [0.9, 0.8, 0.7]&lt;/li&gt;
&lt;li&gt;queen &amp;lt;-------Embeddings---------&amp;gt;      [0.88, 0.82, 0.68]&lt;/li&gt;
&lt;li&gt;man &amp;lt;-------Embeddings---------&amp;gt;         [0.5, 0.4, 0.3]&lt;/li&gt;
&lt;li&gt;woman &amp;lt;-------Embeddings---------&amp;gt;    [0.48, 0.42, 0.28]&lt;/li&gt;
&lt;li&gt;apple &amp;lt;-------Embeddings---------&amp;gt;        [0.1, 0.3, 0.4]&lt;/li&gt;
&lt;li&gt;banana &amp;lt;-------Embeddings---------&amp;gt;         [0.09, 0.29, 0.41]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here we can see ,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;King is very close to queen&lt;/li&gt;
&lt;li&gt;man is close to woman&lt;/li&gt;
&lt;li&gt;apple is close to banana but in other semantic cluster&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Embedding Table: Sentences&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;"How to cook pasta?" &amp;lt;--Embeddings--&amp;gt;. [0.65, 0.88, 0.34, ..., 0.72]&lt;/li&gt;
&lt;li&gt;"Steps for making spaghetti" &amp;lt;--Embeddings--&amp;gt;         [0.63, 0.90, 0.33, ..., 0.71]&lt;/li&gt;
&lt;li&gt;"What is quantum physics?" &amp;lt;--Embeddings--&amp;gt;         [0.11, 0.23, 0.56, ..., 0.19]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now calculate &lt;strong&gt;cosine similarity&lt;/strong&gt; between embeddings:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;cook pasta and making spaghetti is very similar&lt;/li&gt;
&lt;li&gt;cook pasta and quantum physics is not similar&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Semantic Search Works on Meaning, Not Just Words&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In both the word and sentence embedding examples, you’ll notice a key takeaway:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Semantic search operates on vector representations — numerical values that capture meaning — not just literal word matching.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This means even if the exact words don’t appear in the query or document, the model can still understand the &lt;em&gt;context&lt;/em&gt; and retrieve relevant results based on &lt;em&gt;meaning&lt;/em&gt;. This is what makes LLM-powered search far more powerful than traditional keyword-based methods.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Embeddings Power RAG
&lt;/h2&gt;

&lt;p&gt;In RAG, embeddings allow us to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Convert &lt;strong&gt;chunks of documents&lt;/strong&gt; into &lt;strong&gt;vectors&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Store these vectors in a &lt;strong&gt;vector database&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Embed the user &lt;strong&gt;query&lt;/strong&gt; at runtime&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use &lt;strong&gt;similarity search&lt;/strong&gt; to fetch relevant chunks&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Result: LLMs generate answers with user query + context (relevant documents).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Common Embedding Models&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;OpenAIEmbeddings&lt;/li&gt;
&lt;li&gt;HuggingFaceEmbeddings&lt;/li&gt;
&lt;li&gt;GoogleGenerativeAIEmbeddings&lt;/li&gt;
&lt;li&gt;OllamaEmbeddings
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from langchain_google_genai import GoogleGenerativeAIEmbeddings
 from langchain.schema import Document
 from langchain.text_splitter import CharacterTextSplitter

embedding = GoogleGenerativeAIEmbeddings(
    model="models/embedding-001",
    google_api_key="YOUR_API_KEY")

#Document
faq_text = """
Q: What is your return policy?
A: You can return items within 30 days for a full refund.
Q: How long does shipping take?
A: Shipping typically takes 3-5 business days.
Q: Do you offer international shipping?
A: Yes, we ship to over 50 countries.
Q: How can I track my order?
A: You will receive a tracking link via email once your order ships.
"""
#Split the doucment
text_splitter = CharacterTextSplitter(chunk_size=200, chunk_overlap=0)
documents = text_splitter.create_documents([faq_text])

doc_embeddings = embedding.embed_documents(documents)
query_embedding = embedding.embed_query("What is the return policy?")

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;embed_documents()&lt;/strong&gt; creates a vector for each chunk
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;embed_query()&lt;/strong&gt; lets you compare a query to your document embeddings&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What Are Vector Databases?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;vector database&lt;/strong&gt; is a special kind of database designed to &lt;strong&gt;store and search through embeddings&lt;/strong&gt; (vectors), which represent the &lt;strong&gt;semantic meaning&lt;/strong&gt; of things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Text (words, sentences, documents)&lt;/li&gt;
&lt;li&gt;  Images&lt;/li&gt;
&lt;li&gt;  Code&lt;/li&gt;
&lt;li&gt;  Audio&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These databases are optimized for &lt;strong&gt;fast similarity search&lt;/strong&gt; — like answering:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Find me the most similar documents to this question.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Key Idea is :
&lt;/h3&gt;

&lt;p&gt;In &lt;strong&gt;traditional databases&lt;/strong&gt; search by exact values such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM users WHERE email = 'you@example.com';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But in &lt;strong&gt;Vector Databases&lt;/strong&gt;  perform semantic search which is based on words or sentences context or meaning. Already discussed above. To perform such operations they use &lt;strong&gt;cosine similarity&lt;/strong&gt;, &lt;strong&gt;Euclidean distance&lt;/strong&gt;, etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case Flow Example&lt;/strong&gt;*&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    Your PDF → Split into chunks → Embed each chunk → Store in Vector DB

    User query → Embed query → Search DB → Get top chunks → Answer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Popular Vector Databases
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;FAISS:&lt;/strong&gt; Open-source by Facebook, fast, local&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pinecone:&lt;/strong&gt; Cloud-native, scalable, real-time updates&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Weaviate:&lt;/strong&gt; Semantic graph + vector search&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Milvus:&lt;/strong&gt; High-performance, GPU acceleration&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Qdrant:&lt;/strong&gt; Rust-based, fast, open-source&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chroma:&lt;/strong&gt; Developer-friendly, works well with LangChain&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Vector Database use cases:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Similarity Search:&lt;/strong&gt; Finds meaning, not just keywords&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memory for LLMs:&lt;/strong&gt; Used in Retrieval-Augmented Generation (RAG)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fast Search on Big Data:&lt;/strong&gt; Search millions of vectors quickly&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalable + Flexible:&lt;/strong&gt; Easily update, delete, filter, tag data&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Code Example with Chroma
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from langchain_google_genai import GoogleGenerativeAIEmbeddings
from langchain_community.vectorstores import Chroma
from langchain.schema import Document
from langchain.text_splitter import CharacterTextSplitter
from langchain_core.output_parsers import StrOutputParser

#Document
faq_text = """
Q: What is your return policy?
A: You can return items within 30 days for a full refund.
Q: How long does shipping take?
A: Shipping typically takes 3-5 business days.
Q: Do you offer international shipping?
A: Yes, we ship to over 50 countries.
Q: How can I track my order?
A: You will receive a tracking link via email once your order ships.
"""

#Split the doucment
text_splitter = CharacterTextSplitter(chunk_size=200, chunk_overlap=0)
documents = text_splitter.create_documents([faq_text])

#Embeded model
embedings = GoogleGenerativeAIEmbeddings(google_api_key=GOOGLE_API_KEY, model=EMBEDDING_MODEL_NAME)

#create vector database
vectorstore = Chroma.from_documents(documents, embedings, persist_directory='./faq.db')

query = "What is the return policy?"
results = vectorstore.similarity_search(query)
print(results[0].page_content)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You just built a semantic search engine.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;A &lt;strong&gt;vector database&lt;/strong&gt; stores and retrieves &lt;strong&gt;embeddings&lt;/strong&gt;, enabling machines to search by &lt;strong&gt;meaning&lt;/strong&gt; rather than exact matches.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;They’re essential for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Chatbots with memory&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Semantic search&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;AI-powered search engines&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;RAG pipelines&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is Cosine Similarity?
&lt;/h2&gt;

&lt;p&gt;Similarity between embeddings is usually calculated using &lt;strong&gt;cosine similarity&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Similarity(A, B) = (A · B) / (||A|| ||B||)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Ranges from -1 to 1&lt;/li&gt;
&lt;li&gt;  1 = Identical direction (most similar)&lt;/li&gt;
&lt;li&gt;  0 = Orthogonal (unrelated)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;LangChain handles this internally when using &lt;code&gt;similarity_search()&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use same model for doc/query to prevent mismatched meaning&lt;/li&gt;
&lt;li&gt;Normalize content before embedding&lt;/li&gt;
&lt;li&gt;Store metadata in chunks&lt;/li&gt;
&lt;li&gt;Choose right vector store&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What’s Next?
&lt;/h2&gt;

&lt;p&gt;In &lt;strong&gt;Part 5&lt;/strong&gt;, we’ll bring it all together using:&lt;br&gt;&lt;br&gt;
&lt;strong&gt;LangChain Chains + Output Parsers&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
So that the LLM can not just retrieve context — but generate structured, actionable answers!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Missed the earlier parts?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;a href="https://dev.to/dharmendra_singh_786/building-rag-applications-with-langchain-part-1-4f1p"&gt;Part 1 – What is RAG&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://dev.to/dharmendra_singh_786/building-rag-applications-with-langchainpart-2-4p1o"&gt;Part 2 – Document Loaders&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://dev.to/dharmendra_singh_786/building-rag-applications-with-langchainpart-3-54gb"&gt;Part 3 – Text Splitters&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>langchain</category>
      <category>langgraph</category>
      <category>rag</category>
      <category>llm</category>
    </item>
    <item>
      <title>Building RAG Applications with LangChain(Part-3)</title>
      <dc:creator>Dharmendra Singh</dc:creator>
      <pubDate>Mon, 23 Jun 2025 13:13:28 +0000</pubDate>
      <link>https://forem.com/dharmendra_singh_786/building-rag-applications-with-langchainpart-3-54gb</link>
      <guid>https://forem.com/dharmendra_singh_786/building-rag-applications-with-langchainpart-3-54gb</guid>
      <description>&lt;p&gt;&lt;strong&gt;Part 3: Text Splitters — The Art of Chunking for LLMs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Series Progress&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;a href="https://dev.to/dharmendra_singh_786/building-rag-applications-with-langchain-part-1-4f1p"&gt;Part 1: RAG Architecture&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://dev.to/dharmendra_singh_786/building-rag-applications-with-langchainpart-2-4p1o"&gt;Part 2: Document Loaders&lt;/a&gt;
You’re here: Part 3 — Text Splitting&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why Do We Need Text Splitting?
&lt;/h2&gt;

&lt;p&gt;Large documents can overwhelm LLMs if passed in raw. Text splitting is essential in Retrieval-Augmented Generation (RAG) systems for these reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Breaks long documents into manageable, context-rich chunks&lt;/li&gt;
&lt;li&gt;  Improves vector search accuracy (better embeddings)&lt;/li&gt;
&lt;li&gt;  Enables retrieving only relevant content&lt;/li&gt;
&lt;li&gt;  Prevents exceeding token limits of LLM prompts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without smart chunking, your RAG pipeline may hallucinate or return irrelevant results.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Concepts
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Chunk Size
&lt;/h3&gt;

&lt;p&gt;The maximum size of each split, typically in characters or tokens.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Bigger chunks = more context, but risk overflow&lt;/li&gt;
&lt;li&gt;  Smaller chunks = less context, but safer for prompt limits&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Chunk Overlap
&lt;/h3&gt;

&lt;p&gt;Extra content from the previous chunk to maintain continuity.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Helps the model retain context across chunks&lt;/li&gt;
&lt;li&gt;  Common values: 30–50 tokens or characters&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Common Text Splitters in LangChain
&lt;/h2&gt;

&lt;p&gt;LangChain offers various built-in splitters, each optimized for different use cases:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. &lt;code&gt;CharacterTextSplitter&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  Simple, general-purpose splitter by character length&lt;/li&gt;
&lt;li&gt;  Works well for raw or unstructured text&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. &lt;code&gt;RecursiveCharacterTextSplitter&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  Smart splitter that tries to preserve structure (e.g., paragraphs, sections)&lt;/li&gt;
&lt;li&gt;  Ideal for Markdown, source code, or articles&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. &lt;code&gt;TokenTextSplitter&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  Token-aware (e.g., works with OpenAI/Gemini tokens)&lt;/li&gt;
&lt;li&gt;  Prevents prompt overflow&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. &lt;code&gt;MarkdownHeaderTextSplitter&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  Splits based on heading levels in Markdown documents&lt;/li&gt;
&lt;li&gt;  Great for blogs, technical docs, wikis&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  5. Language-Specific Splitters
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  e.g., &lt;code&gt;PythonCodeSplitter&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  Maintains function/class blocks in source code files&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How Do Text Splitters Work?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step-by-Step Breakdown
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Receive Raw Content&lt;/strong&gt;
Usually as &lt;code&gt;Document&lt;/code&gt; objects loaded from PDFs, web pages, etc.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Choose a Splitting Strategy&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;  By characters: &lt;code&gt;\n&lt;/code&gt;, &lt;code&gt;.&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;  By tokens: using tokenizer&lt;/li&gt;
&lt;li&gt;  By structure: headers, code blocks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. Split into Segments&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Uses a hierarchy: try largest delimiter first (&lt;code&gt;\n\n&lt;/code&gt; → &lt;code&gt;\n&lt;/code&gt; → &lt;code&gt;.&lt;/code&gt; → space)&lt;/li&gt;
&lt;li&gt;  If still too long, falls back to character-level splits&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4. Build Overlapping Chunks&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Ensures each chunk fits within &lt;code&gt;chunk_size&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  Adds &lt;code&gt;chunk_overlap&lt;/code&gt; tokens for context preservation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;5. Return New Document Chunks&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Each chunk retains metadata (source, page number, etc.)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Code Example 1: Recursive Character Splitter
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from langchain.text_splitter import RecursiveCharacterTextSplitter  

splitter = RecursiveCharacterTextSplitter(  
    chunk_size=500,  
    chunk_overlap=50  
)  

chunks = splitter.split_documents(documents)  

print(f"Chunks created: {len(chunks)}")  
print(chunks[0].page_content)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Recommended for most use cases.&lt;br&gt;&lt;br&gt;
Intelligently handles structure and fallback splitting._&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Code Example 2: Token Splitter
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from langchain.text_splitter import TokenTextSplitter  

splitter = TokenTextSplitter(  
    chunk_size=200,  
    chunk_overlap=20  
)  

chunks = splitter.split_documents(documents)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Useful when working with LLMs that have strict token limits (e.g., OpenAI, Gemini, Claude)._&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Code Example 3: Markdown Header Splitter
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from langchain.text_splitter import MarkdownHeaderTextSplitter  

md_text = """# RAG Tutorial  
LangChain is awesome.  

## Embeddings  
This is how it works."""  

splitter = MarkdownHeaderTextSplitter(  
    headers_to_split_on=[('#', 'H1'), ('##', 'H2')]  
)  

docs = splitter.split_text(md_text)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Best for docs, blogs, or tutorials with clear header structure_&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Mini Workflow Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from langchain.document_loaders import PyPDFLoader  
from langchain.text_splitter import RecursiveCharacterTextSplitter  

#Load PDF  
loader = PyPDFLoader("data/report.pdf")  
documents = loader.load()  

#Split into chunks  
splitter = RecursiveCharacterTextSplitter(chunk_size=400, chunk_overlap=50)  
chunks = splitter.split_documents(documents)  

#Preview first chunks  
for chunk in chunks[:2]:  
    print(chunk.metadata)  
    print(chunk.page_content[:100])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Best Practices
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  Use &lt;code&gt;RecursiveCharacterTextSplitter&lt;/code&gt; for general use&lt;/li&gt;
&lt;li&gt;  Always set &lt;code&gt;chunk_overlap&lt;/code&gt; (30–50) to retain context&lt;/li&gt;
&lt;li&gt;  Keep &lt;code&gt;chunk_size&lt;/code&gt; within your model’s max context window&lt;/li&gt;
&lt;li&gt;  Clean up input data before splitting (especially scanned PDFs)&lt;/li&gt;
&lt;li&gt;  Preserve original metadata (title, page number, etc.) in each chunk&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  TL;DR — What to Do / What to Avoid
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Do This:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Use structure-aware splitters like &lt;code&gt;RecursiveCharacterTextSplitter&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  Tune &lt;code&gt;chunk_size&lt;/code&gt; and &lt;code&gt;chunk_overlap&lt;/code&gt; to match your use case&lt;/li&gt;
&lt;li&gt;  Retain and attach document metadata to each chunk&lt;/li&gt;
&lt;li&gt;  Use token-aware splitting for LLM compatibility&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Avoid This:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Splitting by fixed length without overlap&lt;/li&gt;
&lt;li&gt;  Using chunks that are too small or too large&lt;/li&gt;
&lt;li&gt;  Dropping metadata (leads to loss of context)&lt;/li&gt;
&lt;li&gt;  Ignoring token limitations of your LLM&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Coming Next
&lt;/h2&gt;

&lt;p&gt;In &lt;strong&gt;Part 4&lt;/strong&gt;, we’ll explore &lt;strong&gt;Embeddings and Vector Stores&lt;/strong&gt; — turning chunks into vectors and enabling semantic search through similarity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Missed a Part?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;a href="https://dev.to/dharmendra_singh_786/building-rag-applications-with-langchain-part-1-4f1p"&gt;Part 1: RAG Architecture&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://dev.to/dharmendra_singh_786/building-rag-applications-with-langchainpart-2-4p1o"&gt;Part 2: Document Loaders&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>langchain</category>
      <category>langgraph</category>
      <category>rag</category>
      <category>llm</category>
    </item>
    <item>
      <title>Building RAG Applications with LangChain(Part-2)</title>
      <dc:creator>Dharmendra Singh</dc:creator>
      <pubDate>Mon, 23 Jun 2025 13:01:54 +0000</pubDate>
      <link>https://forem.com/dharmendra_singh_786/building-rag-applications-with-langchainpart-2-4p1o</link>
      <guid>https://forem.com/dharmendra_singh_786/building-rag-applications-with-langchainpart-2-4p1o</guid>
      <description>&lt;h2&gt;
  
  
  Part 2: Document Loaders — Theory, Usage, and Examples
&lt;/h2&gt;

&lt;p&gt;Welcome back to our  &lt;strong&gt;LangChain RAG series&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/dharmendra_singh_786/building-rag-applications-with-langchain-part-1-4f1p"&gt;In Part 1, we covered the  &lt;strong&gt;architecture and theory&lt;/strong&gt;&lt;/a&gt; behind Retrieval-Augmented Generation (RAG). We broke down the full pipeline and its major components.&lt;/p&gt;

&lt;p&gt;Today, in  &lt;strong&gt;Part 2&lt;/strong&gt;, we’ll take our first deep dive — into the  &lt;strong&gt;Document Loader&lt;/strong&gt;  component.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;A RAG pipeline is only as good as the data you feed into it. That journey starts with&lt;/em&gt; document loaders_._&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What Are Document Loaders?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Document loaders&lt;/strong&gt;  are responsible for reading raw content (from files, databases, URLs, APIs, etc.) and converting it into a format that LangChain can work with — typically a list of  &lt;code&gt;Document&lt;/code&gt;  objects.&lt;/p&gt;

&lt;p&gt;Each  &lt;code&gt;Document&lt;/code&gt;  contains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;page_content&lt;/code&gt;  — the actual text&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;metadata&lt;/code&gt;  — file name, source, page number, etc.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Document(  
page_content="Technical support is available 24/7 through chat or phone.",  
metadata="faq.txt"  
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why Do We Need Document Loaders?
&lt;/h2&gt;

&lt;p&gt;LLMs like GPT or Gemini can’t natively read PDFs, CSVs, or websites. You need to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Extract text&lt;/li&gt;
&lt;li&gt;  Clean it up&lt;/li&gt;
&lt;li&gt;  Split it into chunks&lt;/li&gt;
&lt;li&gt;  Embed &amp;amp; retrieve it&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without good document ingestion, your RAG model is flying blind.&lt;/p&gt;

&lt;h2&gt;
  
  
  Supported Content Sources in LangChain
&lt;/h2&gt;

&lt;p&gt;LangChain makes it easy to load and process content from a wide variety of source types. Whether you’re working with PDFs, web pages, or structured data, there’s likely a loader (or two) that fits your use case.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common Source Types &amp;amp; Loaders
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;PDFs&lt;/strong&gt;
Use cases: Reports, eBooks, scanned documents
&lt;strong&gt;Loaders&lt;/strong&gt;:  &lt;code&gt;PyPDFLoader&lt;/code&gt;,  &lt;code&gt;PDFMinerLoader&lt;/code&gt;,  &lt;code&gt;UnstructuredPDFLoader&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Text / Markdown&lt;/strong&gt;
Use cases: Notes, technical documentation, blog posts
&lt;strong&gt;Loaders&lt;/strong&gt;:  &lt;code&gt;TextLoader&lt;/code&gt;,  &lt;code&gt;MarkdownLoader&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Word Documents&lt;/strong&gt;
Use cases: Contracts, resumes, letters
&lt;strong&gt;Loader&lt;/strong&gt;:  &lt;code&gt;Docx2txtLoader&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Web Pages&lt;/strong&gt;
Use cases: Articles, blog content, public websites
&lt;strong&gt;Loaders&lt;/strong&gt;:  &lt;code&gt;WebBaseLoader&lt;/code&gt;  (static),  &lt;code&gt;SeleniumURLLoader&lt;/code&gt;  (JavaScript-heavy)&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Images / OCR&lt;/strong&gt;
Use cases: Scanned forms, handwritten notes, image-based PDFs
&lt;strong&gt;Loader&lt;/strong&gt;:  &lt;code&gt;UnstructuredImageLoader&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;APIs &amp;amp; Structured Data&lt;/strong&gt;
Use cases: JSON files, databases, Google Sheets
&lt;strong&gt;Approach&lt;/strong&gt;: Use  &lt;strong&gt;custom loaders&lt;/strong&gt;  or make direct &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;API/database calls to fetch content&lt;/p&gt;

&lt;h2&gt;
  
  
  How Document Loaders Work
&lt;/h2&gt;

&lt;p&gt;Few examples how can load documents:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1: Load a PDF&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from langchain.document_loaders import PyPDFLoader  

loader = PyPDFLoader("files/ai_report.pdf")  
docs = loader.load()  

print(docs[0].page_content)  
print(docs[0].metadata)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Example 2: Load a website
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from langchain.document_loaders import WebBaseLoader  
loader = WebBaseLoader("https://openai.com/research")  
docs = loader.load()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Example 3: Load a folder of  &lt;code&gt;.txt&lt;/code&gt;  files
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from langchain.document_loaders import TextLoader  
from pathlib import Path  

loaders = [TextLoader(str(file)) for file in Path("notes").glob("*.txt")]  
docs = []  
for loader in loaders:  
    docs.extend(loader.load())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Example 4: Load  &lt;code&gt;.CSV&lt;/code&gt;  files
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from langchain_community.document_loaders import CSVLoader  

loader = CSVLoader(file_path='Social_Network_Ads.csv')  

docs = loader.load()  

print(len(docs))  
print(docs[1])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pro Tip: Use Metadata
&lt;/h2&gt;

&lt;p&gt;Good metadata (e.g. page number, source file, date) can be used to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Improve retrieval accuracy&lt;/li&gt;
&lt;li&gt;  Add filters (e.g. date, topic)&lt;/li&gt;
&lt;li&gt;  Show context in results&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;print(docs[0].metadata)  &lt;/p&gt;

&lt;h2&gt;
  
  
  Choosing the Right Document Loader in LangChain
&lt;/h2&gt;

&lt;p&gt;LangChain provides a wide range of document loaders tailored to different content types and use cases. Here’s a quick guide to help you choose the best one:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;PyPDFLoader&lt;/strong&gt;  — Ideal for general PDF files with mostly text content.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;PDFMinerLoader&lt;/strong&gt;  — Best for PDFs where layout and positioning of content matter.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;UnstructuredPDFLoader&lt;/strong&gt;  — Great for scanned PDFs or those with mixed content (images + text).&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;WebBaseLoader&lt;/strong&gt;  — Use this for simple, static HTML web pages.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;SeleniumURLLoader&lt;/strong&gt;  — Designed for JavaScript-heavy websites like Medium, LinkedIn, or dynamic dashboards.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;TextLoader&lt;/strong&gt;  — Perfect for plain  &lt;code&gt;.txt&lt;/code&gt;  or  &lt;code&gt;.md&lt;/code&gt;  (Markdown) files.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Docx2txtLoader&lt;/strong&gt;  — Loads content from Microsoft Word  &lt;code&gt;.docx&lt;/code&gt;  files.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Unstructured&lt;/strong&gt;  — A versatile loader for scanned images, documents, forms, and content in mixed or unknown formats.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Best Practices for Document Loading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  Clean up raw content (remove headers/footers)&lt;/li&gt;
&lt;li&gt;  Store source info for traceability&lt;/li&gt;
&lt;li&gt;  Use  &lt;code&gt;RecursiveCharacterTextSplitter&lt;/code&gt;  &lt;strong&gt;after&lt;/strong&gt;  loading&lt;/li&gt;
&lt;li&gt;  Combine multiple loaders in pipelines&lt;/li&gt;
&lt;li&gt;  Avoid unnecessary chunking during loading stage&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Putting It All Together (Mini App)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from langchain.document_loaders import PyPDFLoader  
from langchain.text_splitter import RecursiveCharacterTextSplitter  

loader = PyPDFLoader("sample.pdf")  
documents = loader.load()  

splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)  
docs = splitter.split_documents(documents)  

for doc in docs[:3]:  
    print(doc.metadata)  
    print(doc.page_content[:100])

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Coming Up Next
&lt;/h2&gt;

&lt;p&gt;In  &lt;strong&gt;Part 3&lt;/strong&gt;, we’ll explore  &lt;strong&gt;Text Splitters&lt;/strong&gt;  — how to break large documents into chunks that actually work well with vector search and LLM prompts.&lt;/p&gt;

&lt;p&gt;📖 Catch up on:&lt;br&gt;&lt;br&gt;
&lt;a href="https://dev.to/dharmendra_singh_786/building-rag-applications-with-langchain-part-1-4f1p"&gt;Part 1 — What is RAG &amp;amp; Why It Matters&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Have a use case in mind?
&lt;/h2&gt;

&lt;p&gt;Drop it in the comments! We’ll include community examples in upcoming parts.&lt;/p&gt;

</description>
      <category>langchain</category>
      <category>llm</category>
      <category>rag</category>
      <category>langgraph</category>
    </item>
    <item>
      <title>Building RAG Applications with LangChain: Part 1</title>
      <dc:creator>Dharmendra Singh</dc:creator>
      <pubDate>Mon, 23 Jun 2025 12:51:18 +0000</pubDate>
      <link>https://forem.com/dharmendra_singh_786/building-rag-applications-with-langchain-part-1-4f1p</link>
      <guid>https://forem.com/dharmendra_singh_786/building-rag-applications-with-langchain-part-1-4f1p</guid>
      <description>&lt;p&gt;&lt;strong&gt;Welcome to a brand new series&lt;/strong&gt;  where we deep-dive into building  &lt;strong&gt;RAG (Retrieval-Augmented Generation)&lt;/strong&gt;  applications using  &lt;strong&gt;LangChain&lt;/strong&gt;,  &lt;strong&gt;LLMs (like ChatGPT/Gemini)&lt;/strong&gt;, and modern vector databases.&lt;/p&gt;

&lt;p&gt;In the previous part of this series, we explored how to build foundational LLM applications using tools like chains, structured output parsers, prompt engineering, and more.&lt;/p&gt;

&lt;p&gt;👉 If you’re not yet familiar with concepts like LangChain basics, prompt templates, output parsers, LCEL (LangChain Expression Language), and chains, I recommend checking out the earlier articles in this series for a solid foundation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/@dharamai2024/supercharge-llms-with-langchain-091c8a245f19" rel="noopener noreferrer"&gt;LangChain basics &amp;amp; LCEL&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/@dharamai2024/building-rag-applications-with-langchain-part-2-4d6b82eef077" rel="noopener noreferrer"&gt;Part-2: Document Loader&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, we’re taking it a step further:  &lt;strong&gt;infusing LLMs with factual, external knowledge&lt;/strong&gt;  using RAG — one of the most important design patterns in LLM-powered systems today.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is RAG(&lt;strong&gt;Retrieval-Augmented Generation&lt;/strong&gt;)?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Retrieval:&lt;/strong&gt;  Retrieve relevant documents or passages based on the user query.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Augmentation&lt;/strong&gt;: Use the retrieved documents as additional context.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Generation&lt;/strong&gt;: Generate a response based on the retrieved content plus the user query.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Retrieval-Augmented Generation (RAG)&lt;/strong&gt;  is a technique that combines  &lt;strong&gt;information retrieval&lt;/strong&gt;  and  &lt;strong&gt;text generation&lt;/strong&gt;. Instead of asking an LLM to generate answers from its internal knowledge alone, we first  &lt;strong&gt;retrieve relevant documents&lt;/strong&gt;  from a data source and feed them into the prompt.&lt;/p&gt;

&lt;p&gt;This allows LLMs to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Generate responses grounded in external data&lt;/li&gt;
&lt;li&gt;  Work with  &lt;strong&gt;up-to-date&lt;/strong&gt;  and  &lt;strong&gt;domain-specific&lt;/strong&gt;  knowledge&lt;/li&gt;
&lt;li&gt;  Reduce hallucination&lt;/li&gt;
&lt;li&gt;  Enable enterprise and private data use&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Think of RAG as “search + summarize” powered by an LLM.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Why Use RAG?
&lt;/h3&gt;

&lt;p&gt;Retrieval-Augmented Generation (RAG) offers key advantages over using traditional LLMs alone. Here's how they compare:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Both Traditional LLMs and RAG-enabled LLMs&lt;/em&gt;&lt;/strong&gt; are trained on large datasets.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Traditional LLMs&lt;/em&gt;&lt;/strong&gt; cannot access real-time or private data, but  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;RAG-enabled LLMs&lt;/em&gt;&lt;/strong&gt; can, via external sources or databases.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Traditional LLMs&lt;/em&gt;&lt;/strong&gt; are prone to hallucinations, while &lt;strong&gt;&lt;em&gt;RAG-enabled LLMs&lt;/em&gt;&lt;/strong&gt; are more reliable due to grounding with real data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Traditional LLMs&lt;/em&gt;&lt;/strong&gt; often give generic or unverified answers, whereas &lt;strong&gt;&lt;em&gt;RAG-enabled LLMs&lt;/em&gt;&lt;/strong&gt; provide grounded, source-backed responses.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Traditional LLMs&lt;/em&gt;&lt;/strong&gt; may not be ideal for production use alone,  but &lt;strong&gt;&lt;em&gt;RAG-enabled LLMs&lt;/em&gt;&lt;/strong&gt; are well-suited for real-world production apps.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you’re building apps like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  AI search assistants&lt;/li&gt;
&lt;li&gt;  Chat with PDFs or websites&lt;/li&gt;
&lt;li&gt;  Domain-specific Q&amp;amp;A&lt;/li&gt;
&lt;li&gt;  Legal/medical document readers
…you’ll want RAG.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Core Components of a RAG Pipeline
&lt;/h2&gt;

&lt;p&gt;Here’s a breakdown of  &lt;strong&gt;each core building block&lt;/strong&gt;  in a LangChain-based RAG app:&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Document Loader
&lt;/h2&gt;

&lt;p&gt;LangChain offers a wide variety of  &lt;strong&gt;document loaders&lt;/strong&gt;  to help you ingest and process data from various sources and formats. These loaders are essential for preparing unstructured data for use in LLM-powered applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  Supported Sources
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  Local files (PDFs, text, markdown, etc.)&lt;/li&gt;
&lt;li&gt;  URLs and web pages&lt;/li&gt;
&lt;li&gt;  APIs and JSON endpoints&lt;/li&gt;
&lt;li&gt;  Databases (e.g., SQL, MongoDB)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Common Formats
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  PDF, CSV, Markdown, HTML, DOCX&lt;/li&gt;
&lt;li&gt;  Web pages and plain text&lt;/li&gt;
&lt;li&gt;  Notion, Airtable, and more&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Under-the-Hood Tools
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;unstructured&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;BeautifulSoup&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;PyMuPDF&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;pdfminer.six&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;pypdf&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;html2text&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Popular LangChain Loaders
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;PyPDFLoader&lt;/code&gt;  – For reading PDF files&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;WebBaseLoader&lt;/code&gt;  – For scraping and parsing content from web pages&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;UnstructuredFileLoader&lt;/code&gt;  – For general-purpose file parsing using the  &lt;code&gt;unstructured&lt;/code&gt;  library&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;BSHTMLLoader&lt;/code&gt;  – Parses raw HTML using BeautifulSoup&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;CSVLoader&lt;/code&gt;  – Ingests CSV files into document chunks&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;NotionDBLoader&lt;/code&gt;  – Loads structured content directly from Notion databases&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;DirectoryLoader&lt;/code&gt;  – Loads multiple documents from a folder in bulk&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These loaders make it easy to turn raw content into structured  &lt;code&gt;Document&lt;/code&gt;  objects ready for chunking, embedding, or retrieval.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from langchain.document_loaders import PyPDFLoader  
loader = PyPDFLoader("sample.pdf")  
documents = loader.load()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Text Splitter
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  Splits large texts into manageable chunks.&lt;/li&gt;
&lt;li&gt;  Improves vector relevance and performance.&lt;/li&gt;
&lt;li&gt;  Tools:  &lt;code&gt;RecursiveCharacterTextSplitter&lt;/code&gt;,  &lt;code&gt;TokenTextSplitter&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from langchain.text_splitter import RecursiveCharacterTextSplitter  
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)  
chunks = splitter.split_documents(documents)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Embeddings &amp;amp; Vector Store
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  Converts text chunks into  &lt;strong&gt;numerical vectors&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;  Stores them in a  &lt;strong&gt;vector database&lt;/strong&gt;  for similarity search.&lt;/li&gt;
&lt;li&gt;  Tools:  &lt;code&gt;OpenAIEmbeddings&lt;/code&gt;,  &lt;code&gt;GooglePalmEmbeddings&lt;/code&gt;,  &lt;code&gt;FAISS&lt;/code&gt;,  &lt;code&gt;Chroma&lt;/code&gt;,  &lt;code&gt;Pinecone&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from langchain.vectorstores import FAISS  
from langchain.embeddings import OpenAIEmbeddings  
db = FAISS.from_documents(chunks, OpenAIEmbeddings())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Retriever
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  Interfaces with the vector store to fetch similar documents based on a query.&lt;/li&gt;
&lt;li&gt;  Returns top  &lt;code&gt;k&lt;/code&gt;  relevant chunks.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;retriever = db.as_retriever(search_type="similarity", k=3)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. Prompt Template
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  Formats the retrieved chunks and the user’s question into a single prompt.&lt;/li&gt;
&lt;li&gt;  May include instructions for the LLM.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
template = """Use the context below to answer the question:  
{context}  
Question: {question}  
Answer:  
"""
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  6. LLM / ChatModel
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  The large language model (ChatGPT, Gemini, Claude) that processes the prompt.&lt;/li&gt;
&lt;li&gt;  Can be tuned for summarization, Q&amp;amp;A, or reasoning.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  7. RAG Chain
&lt;/h2&gt;

&lt;p&gt;LangChain lets you connect all these with a  &lt;code&gt;RetrievalQA&lt;/code&gt;  or a custom  &lt;strong&gt;LCEL chain&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from langchain.chains import RetrievalQA  

qa_chain = RetrievalQA.from_chain_type(  
    llm=llm,  
    retriever=retriever,  
    chain_type="stuff"  # or refine, map_reduce  
)  
qa_chain.run("What did the author say about LangChain?")

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;LCEL chain:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from langchain_core.prompts import PromptTemplate  
from langchain.chains.combine_documents.stuff import StuffDocumentsChain  
from langchain_core.runnables import RunnablePassthrough  

## Define a simple prompt  
prompt = PromptTemplate.from_template(  
    "Answer the following question based on the context:\n\n{context}\n\nQuestion: {question}"  
)  

## Combine documents using the 'stuff' method  
document_chain = prompt | llm  

## Build the full LCEL chain  
qa_chain = {  
    "context": retriever | RunnablePassthrough(),  
    "question": RunnablePassthrough()  
} | document_chain  

## Invoke the chain  
response = qa_chain.invoke("What did the author say about LangChain?")  
print(response)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;RAG Flow Diagram&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[Document Loader] → [Text Splitter] → [Embeddings]  &lt;br&gt;
                                       ↓  &lt;br&gt;
                                   [Vector Store]  &lt;br&gt;
                                       ↑  &lt;br&gt;
                            [Retriever (k documents)]  &lt;br&gt;
                                       ↑  &lt;br&gt;
[User Query] → [Prompt Template] + [Docs] → [LLM] → [Answer]&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Why RAG?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;RAG bridges the gap between static LLMs and dynamic, real-world applications. Instead of retraining models, we  &lt;strong&gt;teach them via retrieval&lt;/strong&gt;  — making them faster, safer, and more context-aware.&lt;/p&gt;

&lt;p&gt;Whether you’re building internal tools, smart search engines, or AI copilots — RAG is a must-have skill.&lt;/p&gt;

&lt;p&gt;This article outlines the complete technology stack we use to build Retrieval-Augmented Generation (RAG) applications, along with the reasoning behind their growing importance in the GenAI landscape. Beginning with this introduction, we’ll explore each component of the RAG architecture in detail. Once we’ve covered all the essential building blocks, we’ll move on to developing several real-world, end-to-end RAG applications.&lt;/p&gt;

&lt;p&gt;Let’s get started — the RAG journey begins here.&lt;/p&gt;

</description>
      <category>langchain</category>
      <category>llm</category>
      <category>ai</category>
      <category>langgraph</category>
    </item>
  </channel>
</rss>
