0% found this document useful (0 votes)
239 views143 pages

Lang Chain

LangChain is an open-source framework designed for building applications powered by large language models (LLMs), offering modular components and tools for developers. It supports various AI applications such as chatbots and retrieval-augmented generation, while being actively developed and free to use. The framework includes features like model agnostic development, memory handling, and structured output capabilities to enhance application functionality.

Uploaded by

Maneesh Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
239 views143 pages

Lang Chain

LangChain is an open-source framework designed for building applications powered by large language models (LLMs), offering modular components and tools for developers. It supports various AI applications such as chatbots and retrieval-augmented generation, while being actively developed and free to use. The framework includes features like model agnostic development, memory handling, and structured output capabilities to enhance application functionality.

Uploaded by

Maneesh Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 143

A little background!

29 January 2025 08:28

0. Announcement Page 1
What is LangChain
29 January 2025 08:35

LangChain is an open source framework that helps in building LLM based applications. It
provides modular components and end-to-end tools that help developers build complex AI
applications, such as chatbots, question-answering systems, retrieval-augmented generation
(RAG), autonomous agents, and more.

1. Supports all the major LLMs


2. Simplifies developing LLM based applications
3. Integrations available for all major tools
4. Open source/Free/Actively developed
5. Supports all major GenAI use cases

0. Announcement Page 2
Why LangChain first
29 January 2025 08:35

0. Announcement Page 3
Curriculum Structure
29 January 2025 08:36

0. Announcement Page 4
My Focus
29 January 2025 08:35

1. Updated information
2. Clarity
3. Conceptual understanding
4. The 80 percent approach

0. Announcement Page 5
Timeline
29 January 2025 08:37

0. Announcement Page 6
What is LangChain
07 January 2025 23:14

LangChain is an open-source framework for developing applications powered by


large language models (LLMs).

1. Introduction to LangChain Page 7


Why do we need LangChain
21 January 2025 23:34

Example Queries
1. Explain page number 5 as if I am a 5 year old
2. Generate a True False exercise on Linear
Regression
3. Generate notes for Decision Trees

1. Introduction to LangChain Page 8


1. Introduction to LangChain Page 9
Benefits
21 January 2025 23:34
Concept of chains

Model Agnostic Development

Complete ecosystem

Memory and state handling

1. Introduction to LangChain Page 10


What can you build?
21 January 2025 23:34

Conversational Chatbots

AI Knowledge Assistants

AI Agents

Workflow Automation

Summarization/Research Helpers

1. Introduction to LangChain Page 11


Alternatives
21 January 2025 23:34

LlamaIndex

Haystack

1. Introduction to LangChain Page 12


Recap
23 January 2025 10:29

LangChain is an open-source framework for developing applications


powered by large language models (LLMs).

2. LangChain Components Page 13


LangChain Components
23 January 2025 10:30

2. LangChain Components Page 14


Models
23 January 2025 10:30

In LangChain, “models” are the core interfaces through which you interact with AI models.

2. LangChain Components Page 15


Prompts
23 January 2025 10:30

1. Dynamic & Reusable Prompts

2. Role-Based Prompts

3. Few Shot Prompting

2. LangChain Components Page 16


2. LangChain Components Page 17
Chains
23 January 2025 10:31

2. LangChain Components Page 18


Indexes
23 January 2025 10:31

Indexes connect your application to external knowledge—such as PDFs,


websites or databases

2. LangChain Components Page 19


Memory
23 January 2025 10:31

LLM API calls are stateless

• ConversationBufferMemory: Stores a transcript of recent messages. Great for


short chats but can grow large quickly.

• ConversationBufferWindowMemory: Only keeps the last N interactions to avoid


excessive token usage.

• Summarizer-Based Memory: Periodically summarizes older chat segments to keep


a condensed memory footprint.

• Custom Memory: For advanced use cases, you can store specialized state (e.g.,
the user’s preferences or key facts about them) in a custom memory class.

2. LangChain Components Page 20


Agents
23 January 2025 10:31

2. LangChain Components Page 21


Recap
10 February 2025 09:21

3. Models Page 22
What are Models
07 January 2025 23:15

The Model Component in LangChain is a crucial part of the framework, designed to facilitate
interactions with various language models and embedding models.

It abstracts the complexity of working directly with different LLMs, chat models, and
embedding models, providing a uniform interface to communicate with them. This makes it
easier to build applications that rely on AI-generated text, text embeddings for similarity
search, and retrieval-augmented generation (RAG).

3. Models Page 23
Plan of Action
10 February 2025 09:21

3. Models Page 24
Language Models

Language Models are AI systems designed to process, generate, and understand natural
language text.

LLMs - General-purpose models that is used for raw text generation. They take a string(or plain
text) as input and returns a string( plain text). These are traditionally older models and are not
used much now.

Chat Models - Language models that are specialized for conversational tasks. They take a
sequence of messages as inputs and return chat messages as outputs (as opposed to using
plain text). These are traditionally newer models and used more in comparison to the LLMs.

3. Models Page 25
Setup
10 February 2025 09:21

3. Models Page 26
Demo

1. OpenAI
2. Anthropic
3. Google
4. HuggingFace

3. Models Page 27
Open Source Models
11 February 2025 08:58

Open-source language models are freely available AI models that can be downloaded,
modified, fine-tuned, and deployed without restrictions from a central provider. Unlike closed-
source models such as OpenAI's GPT-4, Anthropic's Claude, or Google's Gemini, open-source
models allow full control and customization.

Some Famous Open Source Models

Where to find them?


HuggingFace - The largest repository of open-source LLMs

Ways to use Open-source Models

Disadvantages

3. Models Page 28
3. Models Page 29
Embedding Models
07 January 2025 23:17

3. Models Page 30
Recap
24 February 2025 09:42

4. Prompts Page 31
A mistake from my side!
24 February 2025 09:42

4. Prompts Page 32
What are Prompts
10 January 2025 08:37

Prompts are the input instructions or queries given to a model to guide its output.

4. Prompts Page 33
Static vs Dynamic Prompts
14 February 2025 00:01

paper_input = st.selectbox( "Select Research Paper Name", ["Select...", "Attention Is All You Need",
"BERT: Pre-training of Deep Bidirectional Transformers", "GPT-3: Language Models are Few-Shot
Learners", "Diffusion Models Beat GANs on Image Synthesis"] )

style_input = st.selectbox( "Select Explanation Style", ["Beginner-Friendly", "Technical", "Code-


Oriented", "Mathematical"] )

length_input = st.selectbox( "Select Explanation Length", ["Short (1-2 paragraphs)", "Medium (3-5
paragraphs)", "Long (detailed explanation)"] )

Please summarize the research paper titled "{paper_input}" with the following
specifications:
Explanation Style: {style_input}
Explanation Length: {length_input}
1. Mathematical Details:
- Include relevant mathematical equations if present in the paper.
- Explain the mathematical concepts using simple, intuitive code snippets
where applicable.
2. Analogies:
- Use relatable analogies to simplify complex ideas.
If certain information is not available in the paper, respond with: "Insufficient
information available" instead of guessing.
Ensure the summary is clear, accurate, and aligned with the provided style and
length.

A PromptTemplate in LangChain is a structured way to create prompts


dynamically by inserting variables into a predefined template. Instead of
hardcoding prompts, PromptTemplate allows you to define placeholders that can
be filled in at runtime with different inputs.

This makes it reusable, flexible, and easy to manage, especially when working
with dynamic user inputs or automated workflows.

4. Prompts Page 34
Prompt Template
10 January 2025 08:37

A PromptTemplate in LangChain is a structured way to create prompts


dynamically by inserting variables into a predefined template. Instead of
hardcoding prompts, PromptTemplate allows you to define placeholders that
can be filled in at runtime with different inputs.

This makes it reusable, flexible, and easy to manage, especially when working
with dynamic user inputs or automated workflows.

Why use PromptTemplate over f strings?


1. Default validation
2. Reusable
3. LangChain Ecosystem

4. Prompts Page 35
Messages
14 February 2025 00:02

4. Prompts Page 36
Chat Prompt Templates
14 February 2025 00:02

4. Prompts Page 37
Message Placeholder
14 February 2025 00:09

A MessagesPlaceholder in LangChain is a special placeholder used inside a


ChatPromptTemplate to dynamically insert chat history or a list of messages at runtime.

4. Prompts Page 38
Recap
28 February 2025 00:12

5. Structured Output Page 39


Structured Output
28 February 2025 00:13

In LangChain, structured output refers to the practice of having language models return
responses in a well-defined data format (for example, JSON), rather than free-form text. This
makes the model output easier to parse and work with programmatically.

Source - https://www.linkedin.com/pulse/structured-outputs-from-llms-langchain-output-parsers-vijay-chaudhary-
wgjqc

5. Structured Output Page 40


Why do we need Structured Output
28 February 2025 00:13

Data Extraction
API building
Agents

5. Structured Output Page 41


5. Structured Output Page 42
Ways to get Structured Output
28 February 2025 00:14

5. Structured Output Page 43


with_structured_output
28 February 2025 00:15
You are an AI assistant that extracts structured
insights from text. Given a product review,
extract: - Summary: A brief overview of the main
points. - Sentiment: Overall tone of the review
(positive, neutral, negative). Return the response
in JSON format.

5. Structured Output Page 44


TypedDict
01 March 2025 12:59

TypedDict is a way to define a dictionary in Python where you specify what keys and values
should exist. It helps ensure that your dictionary follows a specific structure.

Why use TypedDict?

• It tells Python what keys are required and what types of values they should have.
• It does not validate data at runtime (it just helps with type hints for better coding).

-> simple TypedDict


-> Annotated TypedDict
-> Literal
-> More complex -> with pros and cons

5. Structured Output Page 45


Pydantic
01 March 2025 12:59
Basic example
Default values
Optional fields
Pydantic is a data validation and data parsing library for Python. It ensures that the data you Coerce
work with is correct, structured, and type-safe. Builtin validation
Field Function -> default values, constraints, description, regex
expressions
Returns pydantic object -> convert to json/dict

5. Structured Output Page 46


Json
01 March 2025 18:45

5. Structured Output Page 47


When to use what?
01 March 2025 12:59

5. Structured Output Page 48


A few things to remember!
03 March 2025 12:41

5. Structured Output Page 49


Recap
06 March 2025 16:28

6. Output Parsers Page 50


Output Parsers
06 March 2025 16:29

Output Parsers in LangChain help convert raw LLM responses into structured formats like
JSON, CSV, Pydantic models, and more. They ensure consistency, validation, and ease of use in
applications.

6. Output Parsers Page 51


StrOutputParser
06 March 2025 16:29

The StrOutputParser is the simplest output parser in LangChain. It is used to parse the output
of a Language Model (LLM) and return it as a plain string.

6. Output Parsers Page 52


JSONOutputParser
06 March 2025 16:29

6. Output Parsers Page 53


StructuredOutputParser
06 March 2025 16:29

StructuredOutputParser is an output parser in LangChain that helps extract structured JSON


data from LLM responses based on predefined field schemas.

It works by defining a list of fields (ResponseSchema) that the model should return, ensuring
the output follows a structured format.

6. Output Parsers Page 54


PydanticOutputParser
06 March 2025 16:30

6. Output Parsers Page 55


Recap
11 March 2025 17:26

7. Chains Page 56
What & Why
11 March 2025 17:26

7. Chains Page 57
Simple Chain
11 March 2025 17:26

7. Chains Page 58
Sequential Chain
11 March 2025 17:26

7. Chains Page 59
Parallel Chain
11 March 2025 17:26

7. Chains Page 60
Conditional Chain
11 March 2025 17:27

7. Chains Page 61
Recap
18 March 2025 16:55

8. Runnables Page 62
The Why
18 March 2025 16:59

8. Runnables Page 63
8. Runnables Page 64
8. Runnables Page 65
8. Runnables Page 66
The What
19 March 2025 00:12

8. Runnables Page 67
Recap
20 March 2025 10:08

9. LCEL Page 68
Plan of Action
20 March 2025 11:02

9. LCEL Page 69
1. RunnableSequence
20 March 2025 12:10

RunnableSequence is a sequential chain of runnables in LangChain that executes each step


one after another, passing the output of one step as the input to the next.

It is useful when you need to compose multiple runnables together in a structured workflow.

9. LCEL Page 70
2. RunnableParallel
20 March 2025 18:33

RunnableParallel is a runnable primitive that allows multiple runnables to execute in parallel.

Each runnable receives the same input and processes it independently, producing a dictionary
of outputs.

9. LCEL Page 71
3. RunnablePassthrough
20 March 2025 22:34

RunnablePassthrough is a special Runnable primitive that simply returns the input as output
without modifying it.

9. LCEL Page 72
4. RunnableLambda
20 March 2025 23:18

RunnableLambda is a runnable primitive that allows you to apply custom Python functions
within an AI pipeline.

It acts as a middleware between different AI components, enabling preprocessing,


transformation, API calls, filtering, and post-processing in a LangChain workflow.

9. LCEL Page 73
5. RunnableBranch
21 March 2025 08:02

RunnableBranch is a control flow component in LangChain that allows you to conditionally


route input data to different chains or runnables based on custom logic.

It functions like an if/elif/else block for chains — where you define a set of condition functions,
each associated with a runnable (e.g., LLM call, prompt chain, or tool). The first matching
condition is executed. If no condition matches, a default runnable is used (if provided).

9. LCEL Page 74
LCEL
21 March 2025 08:39

9. LCEL Page 75
Plan of Action
27 March 2025 10:59

RAG is a technique that combines information retrieval with language generation,


where a model retrieves relevant documents from a knowledge base and then uses
them as context to generate accurate and grounded responses.

Benefits of using RAG

1. Use of up-to-date information


2. Better privacy
3. No limit of document size

10.Document Loaders Page 76


10.Document Loaders Page 77
Document Loaders in LangChain
27 March 2025 16:20

Document loaders are components in LangChain used to load data from various sources into a
standardized format (usually as Document objects), which can then be used for chunking,
embedding, retrieval, and generation.

10.Document Loaders Page 78


TextLoader
27 March 2025 16:50

TextLoader is a simple and commonly used document loader in LangChain that reads plain text
(.txt) files and converts them into LangChain Document objects.

Use Case

• Ideal for loading chat logs, scraped text, transcripts, code snippets, or any plain text data
into a LangChain pipeline.

Limitation

• Works only with .txt files

10.Document Loaders Page 79


PyPDFLoader
27 March 2025 17:50

PyPDFLoader is a document loader in LangChain used to load content from PDF files and
convert each page into a Document object.

Limitations:
• It uses the PyPDF library under the hood — not great with scanned PDFs or complex
layouts.

10.Document Loaders Page 80


DirectoryLoader
27 March 2025 18:44

DirectoryLoader is a document loader that lets you load multiple documents from a directory
(folder) of files.

10.Document Loaders Page 81


Load vs Lazy load
27 March 2025 23:51

10.Document Loaders Page 82


WebBaseLoader
28 March 2025 00:34

WebBaseLoader is a document loader in LangChain used to load and extract text content from
web pages (URLs).

It uses BeautifulSoup under the hood to parse HTML and extract visible text.

When to Use:
• For blogs, news articles, or public websites where the content is primarily text-based and
static.

Limitations:
• Doesn’t handle JavaScript-heavy pages well (use SeleniumURLLoader for that).
• Loads only static content (what's in the HTML, not what loads after the page renders).

10.Document Loaders Page 83


CSVLoader
28 March 2025 01:48

CSVLoader is a document loader used to load CSV files into LangChain


Document objects — one per row, by default.

10.Document Loaders Page 84


Other Document Loaders
28 March 2025 01:58

10.Document Loaders Page 85


Text Splitting
01 April 2025 18:10

Text Splitting is the process of breaking large chunks of text (like articles, PDFs, HTML pages, or
books) into smaller, manageable pieces (chunks) that an LLM can handle effectively.

Large Text
Chunks

• Overcoming model limitations: Many embedding models and language models


have maximum input size constraints. Splitting allows us to process documents
that would otherwise exceed these limits.

• Downstream tasks - Text Splitting improves nearly every LLM powered task

• Optimizing computational resources: Working with smaller chunks of text can be


more memory-efficient and allow for better parallelization of processing tasks.

11. Text Splitters Page 86


11. Text Splitters Page 87
1. Length Based Text Splitting
01 April 2025 18:10

Space exploration has led to incredible scientific discoveries. Space exploration has led to incredible scientific discoveries. From landing
From landing on the Moon to exploring Mars, humanity on the Moon to explorin
continues to push the boundaries of what’s possible beyond our
planet.
g Mars, humanity continues to push the boundaries of what’s possible beyond our
These missions have not only expanded our knowledge of the planet. These missi
universe but have also contributed to advancements in
technology here on Earth. Satellite communications, GPS, and
even certain medical imaging techniques trace their roots back
ons have not only expanded our knowledge of the universe but have also
to innovations driven by space programs.
contributed to advancements in

n technology here on Earth. Satellite communications, GPS, and even certain


medical imaging techniqu

es trace their roots back to innovations driven by space programs.

11. Text Splitters Page 88


2. Text-Structured Based
01 April 2025 18:10

My name is Nitish
I am 35 years old

I live in Gurgaon
How are you

11. Text Splitters Page 89


11. Text Splitters Page 90
3. Document-Structured Based
01 April 2025 18:11

11. Text Splitters Page 91


4. Semantic Meaning Based
01 April 2025 18:11

Farmers were working hard in the fields, preparing the soil and planting seeds for
the next season. The sun was bright, and the air smelled of earth and fresh grass.
The Indian Premier League (IPL) is the biggest cricket league in the world. People
all over the world watch the matches and cheer for their favourite teams.

Terrorism is a big danger to peace and safety. It causes harm to people and creates
fear in cities and villages. When such attacks happen, they leave behind pain and
sadness. To fight terrorism, we need strong laws, alert security forces, and support
from people who care about peace and safety.

11. Text Splitters Page 92


Why Vector Stores?
05 April 2025 17:40

Movie Movie Director Actor Genre Release Outcom


id name Date e
M001 3 Idiots Raju Hirani Aamir Khan Drama, 2009 Super Hit
Romance
M002 Chennai Rohit Shah Rukh Romance, 2014 Super Hit
Express Shetty Khan Comedy
M003 Inception C Nolan L Di Caprio Thriller, Sci-Fi 2009 Blockbust
er
M004 Stree Amar Rajkumar Horror, 2019 Hit
Kaushik Rao Comedy

Movie id Plot
M001 In the present day, Farhan receives a call from Chatur, saying that
Ranchi is coming. Farhan is so excited that he fakes a heart attack to
get off a flight and picks up Raju from his home (who forgets to
wear his pants). Farhan and Raju meet Chatur at the water tower of
their college ICE (Imperial College of Engineering), where Chatur
informs them that he has found Rancho. Chatur taunts Farhan and
Raju that now he has a mansion worth $3.5 million in the US, with a
heated swimming pool, a maple wood floor living room and a
Lamborghini for a car. Chatur reveals that Rancho is in Shimla…

M002 Rahul Mithaiwala (Shahrukh Khan) is a forty-year old bachelor who


lives in Mumbai. His parents died in a car accident when he was
eight years old and was brought up by grandparents. His
grandfather has a sweet-selling chain store - Y.Y. Mithaiwala. Before
his birth centenary celebration, two of Rahul's friends suggest a
vacation in Goa which he accepts. On the eve of the celebration, his
grandfather dies whilst watching a cricket match. His grandmother
tells him that his grandfather desired to have his ashes divided into
two parts - to be immersed in the Ganges River and Rameswaram
respectively. She requests Rahul to go to Rameswaram and
immerse them. Rahul reluctantly accepts her request but was also
eager to attend the Goa trip…

M003 Dominick "Dom" Cobb (Leonardo DiCaprio) and business partner


Arthur (Joseph Gordon-Levitt) are "extractors", people who perform
corporate espionage using an experimental military technology to
infiltrate the subconscious of their targets and extract information
while experiencing shared dreaming. Their latest target is Japanese
businessman Saito (Ken Watanabe). The extraction from Saito fails
when sabotaged by a memory of Cobb's deceased wife Mal (Marion
Cotillard). After Cobb's and Arthur's associate sells them out, Saito
reveals that he was actually auditioning the team to perform the
difficult act of "inception": planting an idea in a person's
subconscious…

M004 In the peculiar town of Chanderi, India, the residents believe in the
myth of an angry woman ghost, referred to as "Stree" (Hindi for
woman) (Flora Saini), who stalks men during Durga Puja festival.
This is explained by the sudden disappearance of these men, leaving
their clothes behind. She is said to stalk the men of the town,
whispering their names and causing disappearances if they look
back at her.

The whole town protects itself from Stree during the 4 nights of
Durga Puja by writing "OO Stree, Kal Aana" on their walls.
Additionally, men are advised to avoid going out alone after 10 PM
during the festival and to move in groups for safety. This practice
reflects a societal parallel to the precautions typically advised to
women for their own protection…

12. Vector Store Page 93


What are Vector Stores
05 April 2025 17:38

A vector store is a system designed to store and retrieve data represented as numerical
vectors.

Key Features
1. Storage – Ensures that vectors and their associated metadata are retained, whether in-
memory for quick lookups or on-disk for durability and large-scale use.

2. Similarity Search - Helps retrieve the vectors most similar to a query vector.

3. Indexing - Provide a data structure or method that enables fast similarity searches on
high-dimensional vectors (e.g., approximate nearest neighbor lookups).

4. CRUD Operations - Manage the lifecycle of data—adding new vectors, reading them,
updating existing entries, removing outdated vectors.

Use-cases
1. Semantic Search
2. RAG
3. Recommender Systems
4. Image/Multimedia search

12. Vector Store Page 94


Vector Store Vs Vector Database
05 April 2025 17:40

• Vector Store
○ Typically refers to a lightweight library or service that focuses on storing vectors
(embeddings) and performing similarity search.

○ May not include many traditional database features like transactions, rich query
languages, or role-based access control.

○ Ideal for prototyping, smaller-scale applications

○ Examples: FAISS (where you store vectors and can query them by similarity, but you
handle persistence and scaling separately).

• Vector Database
○ A full-fledged database system designed to store and query vectors.

○ Offers additional “database-like” features:


▪ Distributed architecture for horizontal scaling
▪ Durability and persistence (replication, backup/restore)
▪ Metadata handling (schemas, filters)
▪ Potential for ACID or near-ACID guarantees
▪ Authentication/authorization and more advanced security

○ Geared for production environments with significant scaling, large datasets

○ Examples: Milvus, Qdrant, Weaviate.

A vector database is effectively a vector store with extra database features (e.g.,
clustering, scaling, security, metadata filtering, and durability)

12. Vector Store Page 95


Vector Stores in LangChain
05 April 2025 17:41

• Supported Stores: LangChain integrates with multiple vector stores (FAISS, Pinecone, Chroma,
Qdrant, Weaviate, etc.), giving you flexibility in scale, features, and deployment.

• Common Interface: A uniform Vector Store API lets you swap out one backend (e.g., FAISS) for
another (e.g., Pinecone) with minimal code changes.

• Metadata Handling: Most vector stores in LangChain allow you to attach metadata (e.g.,
timestamps, authors) to each document, enabling filter-based retrieval.

12. Vector Store Page 96


Chroma Vector Store
05 April 2025 17:41

Chroma is a lightweight, open-source vector database that is especially friendly for local
development and small- to medium-scale production needs.

12. Vector Store Page 97


Code
05 April 2025 17:41

12. Vector Store Page 98


Recap
12 April 2025 13:51

13. Retrievers Page 99


What are Retrievers
10 April 2025 07:56

A retriever is a component in LangChain that fetches relevant documents from a


data source in response to a user’s query.

There are multiple types of retrievers

All retrievers in LangChain are runnables

13. Retrievers Page 100


Types of Retrievers
10 April 2025 16:23

13. Retrievers Page 101


Wikipedia Retriever
10 April 2025 16:23

A Wikipedia Retriever is a retriever that queries the Wikipedia API to fetch relevant content for
a given query.

13. Retrievers Page 102


Vector Store Retriever
10 April 2025 16:24

A Vector Store Retriever in LangChain is the most common type of retriever that lets you
search and fetch documents from a vector store based on semantic similarity using vector
embeddings.

13. Retrievers Page 103


Maximal Marginal Relevance (MMR)
10 April 2025 16:24

MMR is an information retrieval algorithm designed to reduce redundancy in the retrieved


results while maintaining high relevance to the query.

13. Retrievers Page 104


Multi-Query Retriever
10 April 2025 16:26

13. Retrievers Page 105


Contextual Compression Retriever
10 April 2025 16:29

The Contextual Compression Retriever in LangChain is an advanced retriever that improves


retrieval quality by compressing documents after retrieval — keeping only the relevant
content based on the user's query.

13. Retrievers Page 106


More Retrievers
10 April 2025 16:29

13. Retrievers Page 107


Recap
16 April 2025 12:45

14. RAG Page 108


What is RAG?
16 April 2025 12:43

(Prompt)

In-Context Learning is a core capability of Large Language Models (LLMs) like


GPT-3/4, Claude, and Llama, where the model learns to solve a task purely by
seeing examples in the prompt—without updating its weights.

14. RAG Page 109


seeing examples in the prompt—without updating its weights.

An emergent property is a behaviour or ability that suddenly appears in a system


when it reaches a certain scale or complexity—even though it was not explicitly
programmed or expected from the individual components.

14. RAG Page 110


Understanding RAG
16 April 2025 12:44

• Indexing
• Retrieval
• Augmentation
• Generation

Indexing - Indexing is the process of preparing your knowledge base so that it


can be efficiently searched at query time. This steps consists of 4 sub-steps.

1. Document Ingestion - You load your source knowledge into memory.

2. Text Chunking - Break large documents into small, semantically meaningful chunks

14. RAG Page 111


3. Embedding Generation - Convert each chunk into a dense vector (embedding) that captures its
meaning.

4. Storage in a Vector Store - Store the vectors along with the original chunk text + metadata in a
vector database.

14. RAG Page 112


Retrieval - Retrieval is the real-time process of finding the most
relevant pieces of information from a pre-built index (created during
indexing) based on the user’s question.

Augmentation - Augmentation refers to the step where the retrieved documents (chunks of
relevant context) are combined with the user’s query to form a new, enriched prompt for
the LLM.

14. RAG Page 113


Generation - Generation is the final step where a Large Language
Model (LLM) uses the user’s query and the retrieved & augmented
context to generate a response.

14. RAG Page 114


Problem Statement
21 April 2025 08:55

15. RAG using LangChain Page 115


Plan of Action
21 April 2025 08:56

15. RAG using LangChain Page 116


Improvements
22 April 2025 08:50

1. UI based enhancements

2. Evaluation
a. Ragas
b. LangSmith

3. Indexing
a. Document Ingestion
b. Text Splitting
c. Vector Store

4. Retrieval
a. Pre-Retrieval
i. Query rewriting using LLM
ii. Multi-query generation
iii. Domain aware routing

b. During Retrieval
i. MMR
ii. Hybrid Retrieval
iii. Reranking

c. Post-Retrieval
i. Contextual Compression

5. Augmentation
a. Prompt Templating
b. Answer grounding
c. Context window optimization

6. Generation
a. Answer with Citation
b. Guard railing

7. System Design
a. Multimodal
b. Agentic
c. Memory based

15. RAG using LangChain Page 117


Overview
23 April 2025 15:51

16. Tools Page 118


What is a Tool?
23 April 2025 15:45

A tool is just a Python function (or API) that is packaged in a way the LLM can understand
and call when needed.

How Tools fits into the Agent ecosystem

An AI agent is an LLM-powered system that can autonomously think, decide, and


take actions using external tools or APIs to achieve a goal.

16. Tools Page 119


Built-in Tools
23 April 2025 15:46

A built-in tool is a tool that LangChain already provides for you —it’s pre-built, production-
ready, and requires minimal or no setup.

You don’t have to write the function logic yourself — you just import and use it.

16. Tools Page 120


Custom Tools
23 April 2025 15:46

A custom tool is a tool that you define yourself.

16. Tools Page 121


Ways to create Custom Tools
23 April 2025 15:46

A Structured Tool in LangChain is a special type of tool where the input


to the tool follows a structured schema, typically defined using a
Pydantic model.

BaseTool is the abstract base class for all tools in LangChain.


It defines the core structure and interface that any tool must follow,
whether it's a simple one-liner or a fully customized function.

All other tool types like @tool, StructuredTool are built on top of
BaseTool

16. Tools Page 122


Toolkits
23 April 2025 15:47

A toolkit is just a collection (bundle) of related tools that serve a common purpose —
packaged together for convenience and reusability.

In LangChain:

• A toolkit might be: GoogleDriveToolKit


• And it can contain the following tools

16. Tools Page 123


Quick Revision
25 April 2025 16:18

17. Tool Calling Page 124


Tool Binding
25 April 2025 16:45

17. Tool Calling Page 125


Tool Calling
25 April 2025 16:54

17. Tool Calling Page 126


Tool Execution
25 April 2025 17:12

17. Tool Calling Page 127


Currency Conversion Tool
25 April 2025 18:02

17. Tool Calling Page 128


What are AI Agents
29 April 2025 22:53

18. Agents Page 129


18. Agents Page 130
18. Agents Page 131
18. Agents Page 132
Building an agent in LangChain
29 April 2025 22:53

18. Agents Page 133


Explanation
29 April 2025 22:53

18. Agents Page 134


1. ReAct
01 May 2025 14:17

ReAct is a design pattern used in AI agents that stands for Reasoning + Acting. It
allows a language model (LLM) to interleave internal reasoning (Thought) with
external actions (like tool use) in a structured, multi-step process.

Instead of generating an answer in one go, the model thinks step by step, deciding what it
needs to do next and optionally calling tools (APIs, calculators, web search, etc.) to help it.

18. Agents Page 135


2. Agent & Agent Executor
01 May 2025 14:49

18. Agents Page 136


3. Creating an Agent
01 May 2025 16:29

18. Agents Page 137


4. Creating an Agent Executor
01 May 2025 16:30

18. Agents Page 138


5. Flow Chart
01 May 2025 16:30

18. Agents Page 139


6. Example
01 May 2025 17:23

18. Agents Page 140


18. Agents Page 141
18. Agents Page 142
18. Agents Page 143

You might also like