LangChain E-Book

LangChain E-Book - Complete Guide

📚 Table of Contents

📘 1. Introduction to LangChain

• What is LangChain?

LangChain is a powerful framework designed to build applications using Large Language Models (LLMs). It simplifies the process of creating intelligent applications by providing tools, components, and interfaces to work with AI models.

• Key Features:

  • Modular Design: Easy to use building blocks
  • Integration: Works with multiple LLM providers (OpenAI, Google, Anthropic, etc.)
  • Memory Management: Maintains conversation context
  • Agent Framework: Creates autonomous AI agents
  • Data Connection: Connects to external data sources

• Who Uses LangChain?

Developers, Data Scientists, AI Engineers, Businesses building chatbots, and companies implementing RAG (Retrieval Augmented Generation) systems.

💡 2. Why LangChain?

• Problems LangChain Solves:

Problem LangChain Solution Benefit
Complex LLM Integration Unified Interface Easy switching between models
No Conversation Memory Built-in Memory Context-aware responses
Limited Knowledge RAG System Access to custom data
Static Responses Agents & Tools Dynamic actions
Hard to Scale Modular Components Easy maintenance

🎯 3. Core Concepts

• The Building Blocks:

🤖 Models

The AI brain that generates text, embeddings, or other outputs.

📝 Prompts

Instructions given to the model to guide its responses.

⛓️ Chains

Sequences of operations that combine multiple components.

🧠 Memory

Stores conversation history for context-aware interactions.

🤵 Agents

Autonomous entities that make decisions and use tools.

🛠️ Tools

External functions agents can use (APIs, databases, calculators).

🧩 4. LangChain Components

• Component Breakdown:

Component Purpose Example Use Python Import
LLMs Text generation Chatbots, content creation langchain.llms
Chat Models Conversational AI Customer support bots langchain.chat_models
Embeddings Text vectorization Semantic search langchain.embeddings
Vector Stores Store embeddings Document retrieval langchain.vectorstores
Document Loaders Load data PDF, CSV processing langchain.document_loaders
Text Splitters Chunk documents Large document handling langchain.text_splitter

📊 5. Architecture Flowchart

• LangChain Application Flow:

📥 User Input

Question or Request

🔄 Prompt Template

Format the input

🧠 Memory Check

Load conversation history

🔍 Retrieval (Optional)

Search vector database

🤖 LLM Processing

Generate response

🛠️ Agent Decision

Use tools if needed

💾 Save to Memory

Store interaction

📤 Output Response

Return to user

⚙️ 6. Installation & Setup

• Installation Steps:

Python Installation:

# Install LangChain
pip install langchain

# Install OpenAI integration
pip install langchain-openai

# Install Community packages
pip install langchain-community

# Install vector stores
pip install chromadb faiss-cpu

# Install document loaders
pip install pypdf python-docx

Environment Setup:

# Create .env file
OPENAI_API_KEY=your_api_key_here
ANTHROPIC_API_KEY=your_api_key_here
GOOGLE_API_KEY=your_api_key_here

# Load in Python
from dotenv import load_dotenv
load_dotenv()

🤖 7. Language Models

• Types of Models:

Model Type Provider Best For Example Code
GPT-4 OpenAI Complex reasoning ChatOpenAI(model="gpt-4")
GPT-3.5 Turbo OpenAI Fast responses ChatOpenAI(model="gpt-3.5-turbo")
Claude Anthropic Long contexts ChatAnthropic(model="claude-3")
Gemini Google Multimodal tasks ChatGoogleGenerativeAI()
Llama 2 Meta Open-source projects CTransformers()

Python Example - Using OpenAI:

from langchain_openai import ChatOpenAI

# Initialize the model
llm = ChatOpenAI(
    model="gpt-3.5-turbo",
    temperature=0.7,
    max_tokens=1000
)

# Generate response
response = llm.invoke("What is LangChain?")
print(response.content)

📝 8. Prompts & Templates

• What are Prompt Templates?

Prompt templates are reusable text structures that help format inputs consistently. They use variables that can be filled with dynamic content.

• Types of Prompt Templates:

  • String Prompt Templates: Simple text with variables
  • Chat Prompt Templates: For conversational models
  • Few-Shot Prompt Templates: Include examples
  • System Message Templates: Set model behavior

Python Example - Basic Template:

from langchain.prompts import PromptTemplate

# Create template
template = """
You are a helpful assistant. 
Answer the following question in simple terms:

Question: {question}

Answer:"""

prompt = PromptTemplate(
    template=template,
    input_variables=["question"]
)

# Use template
formatted_prompt = prompt.format(
    question="What is machine learning?"
)
print(formatted_prompt)

Python Example - Chat Template:

from langchain.prompts import ChatPromptTemplate

# Create chat template
chat_template = ChatPromptTemplate.from_messages([
    ("system", "You are a {role} assistant."),
    ("human", "Hello, how are you?"),
    ("ai", "I'm doing well, thanks!"),
    ("human", "{user_input}")
])

# Format
messages = chat_template.format_messages(
    role="helpful",
    user_input="Explain LangChain"
)

⛓️ 9. Chains

• What are Chains?

Chains combine multiple components (prompts, models, tools) into a single workflow. They allow you to create complex applications by connecting simple building blocks.

• Chain Types:

Chain Type Purpose When to Use
LLMChain Basic prompt + model Simple queries
Sequential Chain Run chains in order Multi-step processes
Router Chain Route to different chains Dynamic workflows
Retrieval QA Chain Question answering with docs Knowledge base queries
Conversation Chain Chat with memory Chatbots

Python Example - Simple Chain:

from langchain_openai import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain

# Setup
llm = ChatOpenAI(temperature=0.7)

prompt = PromptTemplate(
    input_variables=["product"],
    template="What is a good name for a company that makes {product}?"
)

# Create chain
chain = LLMChain(llm=llm, prompt=prompt)

# Run chain
result = chain.run(product="eco-friendly water bottles")
print(result)

Python Example - Sequential Chain:

from langchain.chains import SimpleSequentialChain

# Chain 1: Generate product name
chain1 = LLMChain(llm=llm, prompt=prompt1)

# Chain 2: Generate tagline
chain2 = LLMChain(llm=llm, prompt=prompt2)

# Combine chains
overall_chain = SimpleSequentialChain(
    chains=[chain1, chain2],
    verbose=True
)

# Run
result = overall_chain.run("smart home devices")

🧠 10. Memory

• Why Memory Matters:

Memory allows chatbots to remember previous conversations, making interactions feel natural and context-aware. Without memory, each message is treated independently.

• Memory Types:

Memory Type How It Works Best For
ConversationBufferMemory Stores all messages Short conversations
ConversationBufferWindowMemory Keeps last N messages Long conversations
ConversationSummaryMemory Summarizes past messages Cost optimization
ConversationEntityMemory Tracks entities mentioned Complex interactions
VectorStoreMemory Semantic search in history Large histories

Python Example - Buffer Memory:

from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationChain

# Initialize memory
memory = ConversationBufferMemory()

# Create conversation chain
conversation = ConversationChain(
    llm=llm,
    memory=memory,
    verbose=True
)

# Chat
response1 = conversation.predict(input="Hi, my name is Amit")
response2 = conversation.predict(input="What is my name?")
# Output: "Your name is Amit"

Python Example - Window Memory:

from langchain.memory import ConversationBufferWindowMemory

# Keep last 3 exchanges
memory = ConversationBufferWindowMemory(k=3)

conversation = ConversationChain(
    llm=llm,
    memory=memory
)

# Only last 3 exchanges are remembered

🤵 11. Agents

• What are Agents?

Agents are autonomous systems that can make decisions, use tools, and take actions to accomplish goals. They decide which tools to use and when, based on the task at hand.

• Agent Components:

  • Agent: The core decision-maker
  • Tools: Functions the agent can use
  • Executor: Runs the agent's decisions
  • Memory: Stores conversation context

• Agent Types:

Agent Type Description Use Case
Zero-shot ReAct Decides without examples General purpose tasks
Conversational Chat-based with memory Customer support
Self-ask with Search Breaks down questions Research tasks
Plan and Execute Creates action plan first Complex workflows

Python Example - Agent with Tools:

from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType
from langchain_openai import ChatOpenAI

# Define tools
def calculate(expression):
    return eval(expression)

def search_wikipedia(query):
    return f"Wikipedia result for: {query}"

tools = [
    Tool(
        name="Calculator",
        func=calculate,
        description="Useful for math calculations"
    ),
    Tool(
        name="Wikipedia",
        func=search_wikipedia,
        description="Useful for general knowledge"
    )
]

# Create agent
agent = initialize_agent(
    tools=tools,
    llm=ChatOpenAI(temperature=0),
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True
)

# Use agent
result = agent.run("What is 25 * 4 + 10?")

🛠️ 12. Tools

• What are Tools?

Tools are functions that agents can call to perform specific tasks. They extend the capabilities of LLMs by allowing them to interact with external systems.

• Common Tool Categories:

Category Examples Purpose
Search Tools Google, Wikipedia, DuckDuckGo Information retrieval
API Tools Weather, News, Stock prices Real-time data
Database Tools SQL, MongoDB queries Data operations
Math Tools Calculator, WolframAlpha Calculations
File Tools Read, Write, Parse files File operations

Python Example - Custom Tool:

from langchain.tools import Tool
from datetime import datetime

# Create custom function
def get_current_time(query):
    return datetime.now().strftime("%Y-%m-%d %H:%M:%S")

# Wrap in Tool
time_tool = Tool(
    name="CurrentTime",
    func=get_current_time,
    description="Returns current date and time"
)

# Use with agent
agent = initialize_agent(
    tools=[time_tool],
    llm=llm,
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION
)

result = agent.run("What time is it?")

💾 13. Vector Stores

• What are Vector Stores?

Vector stores are databases that store text as numerical vectors (embeddings). They enable semantic search - finding documents based on meaning rather than exact keywords.

• Popular Vector Stores:

Vector Store Type Best For Cost
Chroma Open-source Local development Free
FAISS Open-source Large scale, fast Free
Pinecone Cloud service Production apps Paid
Weaviate Open-source/Cloud Hybrid search Free/Paid
Qdrant Open-source Filtering support Free/Paid

Python Example - Chroma Vector Store:

from langchain.vectorstores import Chroma
from langchain_openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter

# Sample documents
documents = [
    "LangChain is a framework for building LLM applications.",
    "Vector stores enable semantic search.",
    "Embeddings convert text to numbers."
]

# Split text
text_splitter = CharacterTextSplitter(chunk_size=100)
docs = text_splitter.create_documents(documents)

# Create embeddings
embeddings = OpenAIEmbeddings()

# Create vector store
vectorstore = Chroma.from_documents(
    documents=docs,
    embedding=embeddings
)

# Similarity search
results = vectorstore.similarity_search("What is LangChain?")
print(results[0].page_content)

🔍 14. Retrievers

• What are Retrievers?

Retrievers are interfaces that fetch relevant documents based on a query. They connect vector stores to chains, enabling RAG (Retrieval Augmented Generation) systems.

• Retriever Types:

  • Vector Store Retriever: Similarity-based search
  • SVM Retriever: Uses Support Vector Machines
  • TF-IDF Retriever: Traditional keyword matching
  • Multi-Query Retriever: Generates multiple queries
  • Ensemble Retriever: Combines multiple retrievers

Python Example - RAG System:

from langchain.chains import RetrievalQA
from langchain_openai import ChatOpenAI
from langchain.vectorstores import Chroma
from langchain_openai import OpenAIEmbeddings

# Setup vector store
vectorstore = Chroma.from_documents(
    documents=docs,
    embedding=OpenAIEmbeddings()
)

# Create retriever
retriever = vectorstore.as_retriever(
    search_type="similarity",
    search_kwargs={"k": 3}
)

# Create QA chain
qa_chain = RetrievalQA.from_chain_type(
    llm=ChatOpenAI(temperature=0),
    chain_type="stuff",
    retriever=retriever
)

# Ask question
question = "What is LangChain used for?"
answer = qa_chain.run(question)
print(answer)

🎯 15. Real-World Use Cases

• LangChain Applications:

💬 Customer Support Chatbot

Components Used:

  • Chat Models
  • Conversation Memory
  • RAG with FAQ documents

Benefit: 24/7 automated support with context awareness

📄 Document Q&A System

Components Used:

  • Document Loaders
  • Vector Stores
  • Retrieval Chains

Benefit: Instant answers from large document collections

🔬 Research Assistant

Components Used:

  • Agents
  • Search Tools
  • Sequential Chains

Benefit: Automated literature review and summarization

💼 Sales Email Generator

Components Used:

  • Prompt Templates
  • LLM Chains
  • Output Parsers

Benefit: Personalized email campaigns at scale

📊 Data Analysis Agent

Components Used:

  • Pandas Agent
  • Code Generation
  • Result Interpretation

Benefit: Natural language data queries

🏫 Educational Tutor

Components Used:

  • Chat Models
  • Memory Systems
  • Custom Prompts

Benefit: Personalized learning experience

💰 16. Pricing Guide (in Rupees)

• LangChain Framework Cost:

LangChain itself is FREE and open-source. However, you pay for the services you use with it (LLM APIs, vector databases, etc.).

• Approximate API Costs (₹ per 1000 tokens):

Service Model Input Cost Output Cost
OpenAI GPT-4 ₹2.50 ₹5.00
OpenAI GPT-3.5 Turbo ₹0.04 ₹0.08
Anthropic Claude 3 ₹2.50 ₹12.50
Google Gemini Pro ₹0.42 ₹1.25
OpenAI Embeddings ₹0.01 -

• Vector Store Pricing:

Service Free Tier Paid Plan (Monthly)
Chroma Unlimited (self-hosted) Free
Pinecone 100K vectors ₹5,800+
Weaviate Cloud Limited ₹2,100+
Qdrant Cloud 1GB storage ₹1,650+

• Example Cost Calculation:

Scenario: Customer support chatbot processing 10,000 queries/month

  • Average query: 100 tokens input + 200 tokens output
  • Using GPT-3.5 Turbo:
  • Input cost: (10,000 × 100 / 1000) × ₹0.04 = ₹40
  • Output cost: (10,000 × 200 / 1000) × ₹0.08 = ₹160
  • Vector store (Chroma self-hosted): ₹0
  • Total monthly cost: ₹200

🗺️ 17. LangChain Mind Map

• Complete LangChain Ecosystem:

🎯 LangChain
🤖 Models
• LLMs • Chat Models • Embeddings
📝 Prompts
• Templates • Examples • Selectors
⛓️ Chains
• LLM Chain • Sequential • Router
🧠 Memory
• Buffer • Window • Summary
🤵 Agents
• ReAct • Conversational • Plan & Execute
🛠️ Tools
• Search • APIs • Databases
💾 Vector Stores
• Chroma • FAISS • Pinecone
📄 Documents
• Loaders • Splitters • Transformers

❓ 18. Questions & Answers

• Frequently Asked Questions:

Answer: OpenAI API provides direct access to GPT models, while LangChain is a framework that sits on top of multiple LLM APIs (including OpenAI). LangChain adds features like memory management, chains, agents, and integrations with vector stores. Think of OpenAI as the engine and LangChain as the complete car with steering, navigation, and safety features.

Answer: No! LangChain itself is completely free and open-source. However, you will need to pay for the services you use with it, such as OpenAI API calls, cloud vector databases (like Pinecone), or other third-party integrations. Many vector stores like Chroma and FAISS are also free if you self-host them.

Answer: RAG stands for Retrieval Augmented Generation. It's a technique where you first retrieve relevant documents from a knowledge base (using vector search), then feed those documents to the LLM along with the user's question. This allows the LLM to answer questions based on your custom data, not just its training data. RAG is essential for building Q&A systems over company documents, PDFs, or databases.

Answer:

  • Chroma: Best for local development and prototyping (free, easy setup)
  • FAISS: Best for large-scale, fast searches (free, Facebook-developed)
  • Pinecone: Best for production apps needing managed service (paid, highly scalable)
  • Weaviate: Best for hybrid search (keywords + semantic) needs
  • For beginners: Start with Chroma for simplicity

Answer:

  • Chain: A predetermined sequence of steps. You define exactly what happens and in what order. Example: "First generate a product name, then create a tagline, then write a description."
  • Agent: An autonomous decision-maker. It decides which tools to use and when, based on the task. Example: "Answer this question using any tools you need" - the agent might search Wikipedia, use a calculator, or query a database as needed.
  • Use chains when the workflow is predictable
  • Use agents when you need flexibility and decision-making

Answer: Memory in LangChain stores conversation history so the chatbot can remember previous messages. When a new message arrives, memory retrieves relevant past messages and includes them in the prompt sent to the LLM. Different memory types handle this differently: Buffer Memory keeps everything, Window Memory keeps the last N messages, and Summary Memory compresses old messages into summaries to save tokens.

Answer: Yes! LangChain supports local models through libraries like HuggingFace Transformers, CTransformers, and LlamaCpp. You can run models like Llama 2, Mistral, or Falcon on your own hardware without API costs. This is great for privacy-sensitive applications or when you want to avoid API charges, but requires more technical setup and computational resources.

Answer: Embeddings are numerical representations of text. They convert words, sentences, or documents into arrays of numbers (vectors) that capture semantic meaning. Similar texts have similar embeddings. This enables semantic search - finding documents based on meaning rather than exact keyword matches. For example, "automobile" and "car" would have very similar embeddings even though they're different words.

Answer:

  1. Load the document using Document Loaders (PDFLoader, TextLoader, etc.)
  2. Split into chunks using Text Splitters (typically 500-1000 characters per chunk)
  3. Create embeddings for each chunk
  4. Store in vector database (Chroma, FAISS, etc.)
  5. Use retriever to fetch relevant chunks when answering questions

This allows you to work with documents of any size without hitting token limits.

Answer:

  • Be Specific: Clearly define what you want the model to do
  • Provide Examples: Use few-shot prompts with 2-3 examples
  • Set Context: Define the model's role (e.g., "You are a helpful teacher")
  • Use Templates: Create reusable PromptTemplates with variables
  • Format Output: Specify the desired output format (JSON, bullet points, etc.)
  • Iterate: Test and refine prompts based on results
  • Control Temperature: Use 0-0.3 for factual responses, 0.7-1.0 for creative tasks

Answer:

  • Use cheaper models: GPT-3.5 instead of GPT-4 when possible
  • Implement caching: Store and reuse responses for common queries
  • Use ConversationSummaryMemory: Compress conversation history
  • Optimize chunk sizes: Find the balance between context and tokens
  • Set max_tokens limits: Prevent unnecessarily long responses
  • Use streaming: Start displaying responses before completion
  • Consider local models: For non-critical tasks, use open-source models
  • Batch requests: Process multiple queries together when possible

Answer:

  • Stuff: Puts all documents into one prompt. Fast but limited by token limits. Best for small document sets.
  • Map_Reduce: Processes each document separately, then combines results. Slower but handles many documents. Good for summarization.
  • Refine: Processes documents sequentially, refining the answer with each document. Best for questions needing iterative improvement.
  • Map_Rerank: Scores each document's relevance and returns the best. Useful when you need a single best answer.

© 2024 LangChain E-Book | Created for Educational Purpose | All prices are approximate in Indian Rupees (₹)

Scroll to Top