Part 24: LangChain Foundations - Chains, Prompts & Memory

Learn LangChain chains, prompts, output parsers, memory types, document loaders, text splitters, retrievers, and callbacks for LLM applications.

Part 24: LangChain Foundations - Chains, Prompts & Memory

← Back to Master Index


1. Why LangChain in 2026?

LangChain is the most popular framework for building LLM applications. Engineers with LangChain expertise command 40-70% higher salaries in AI engineering roles.

Key Components

  • Chains: Compose LLM calls and other components
  • Prompts: Template and manage prompts
  • Memory: Maintain conversation state
  • Document Loaders: Load various document types
  • Retrievers: Connect to vector stores

2. LangChain Basics

Installation

pip install langchain langchain-community langchain-chroma
pip install -U langchain-google-genai  # For Google models

LLM Integration

from langchain_openai import ChatOpenAI
from langchain_google_genai import ChatGoogleGenerativeAI

# OpenAI LLM
llm = ChatOpenAI(
    model="gpt-4",
    temperature=0.7,
    max_tokens=1000
)

# Google Gemini
llm_gemini = ChatGoogleGenerativeAI(
    model="gemini-pro",
    temperature=0.7
)

3. Prompts and Prompt Templates

Basic Prompt

from langchain_core.prompts import ChatPromptTemplate

# Create prompt template
prompt = ChatPromptTemplate.from_template("""
You are a helpful assistant. Answer the following question:
Question: {question}
Answer:
""")

# Format and invoke
formatted_prompt = prompt.format(question="What is machine learning?")
response = llm.invoke(formatted_prompt)

Complex Prompt Templates

# Multi-variable prompt
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a {role} assistant."),
    ("human", "Answer this question: {question}\n\nContext: {context}")
])

# Format with variables
formatted_messages = prompt.format_messages(
    role="technical expert",
    question="Explain RAG systems",
    context="RAG combines retrieval and generation..."
)

Prompt Serialization

import json

# Save prompt template
prompt_dict = {
    "template": prompt.template,
    "messages": prompt.messages
}

with open("prompt_template.json", "w") as f:
    json.dump(prompt_dict, f)

4. Chains and Runnables

Simple Chain

from langchain_core.runnables import RunnablePassthrough, RunnableParallel

# Create a simple chain
chain = (
    {"question": RunnablePassthrough()} 
    | prompt 
    | llm
)

# Invoke chain
response = chain.invoke("What is AI?")

Parallel Chains

# Run multiple chains in parallel
parallel_chain = RunnableParallel({
    "summary": summary_chain,
    "keywords": keyword_chain,
    "sentiment": sentiment_chain
})

# Combine results
combined_chain = parallel_chain | combine_results_chain

Conditional Chains

from langchain_core.runnables import RunnableBranch

conditional_chain = RunnableBranch(
    [
        (lambda x: x["type"] == "technical", technical_chain),
        (lambda x: x["type"] == "creative", creative_chain),
    ],
    default_chain  # Default if no condition matches
)

5. Memory and State

Conversation Buffer Memory

from langchain_core.memory import ConversationBufferMemory

memory = ConversationBufferMemory(
    memory_key="chat_history",
    return_messages=True
)

# Use in chain
chain = ConversationChain(
    llm=llm,
    memory=memory,
    prompt=prompt
)

# First interaction
chain.invoke("My name is John")

# Second interaction (remembers name)
chain.invoke("What is my name?")

Conversation Summary Memory

from langchain_core.memory import ConversationSummaryMemory

memory = ConversationSummaryMemory(
    llm=llm,
    memory_key="summary"
)

# Automatically summarizes long conversations

Entity Memory

from langchain.memory import EntityMemory

memory = EntityMemory(llm=llm)

# Extract and store entities
memory.save_entity("John", "person", "likes programming")
memory.save_entity("Python", "technology", "programming language")

6. Document Loaders

File Loaders

from langchain_community.document_loaders import (
    TextLoader,
    CSVLoader,
    PDFPlumberLoader,
    UnstructuredWordDocumentLoader
)

# Load text file
loader = TextLoader("document.txt")
docs = loader.load()

# Load PDF
loader = PDFPlumberLoader("document.pdf")
docs = loader.load()

# Load CSV
loader = CSVLoader("data.csv")
docs = loader.load()

Web Loaders

from langchain_community.document_loaders import WebBaseLoader

# Load web page
loader = WebBaseLoader("https://example.com")
docs = loader.load()

# Load multiple URLs
urls = ["https://example1.com", "https://example2.com"]
loader = WebBaseLoader(urls)
docs = loader.load()

Custom Loader

from langchain_core.document_loaders import BaseLoader
from langchain_core.documents import Document

class CustomLoader(BaseLoader):
    def __init__(self, source):
        self.source = source
    
    def load(self):
        # Custom loading logic
        with open(self.source, 'r') as f:
            content = f.read()
        
        return [Document(
            page_content=content,
            metadata={"source": self.source}
        )]

7. Text Splitters

Character-based Splitting

from langchain_text_splitters import RecursiveCharacterTextSplitter

text_splitter = RecursiveCharacterTextSplitter(
    chunk_size=1000,
    chunk_overlap=200,
    length_function=len,
    is_separator_regex=False,
)

# Split documents
texts = text_splitter.split_text("Your large text here...")

# Split documents
docs = text_splitter.split_documents(documents)

Token-based Splitting

from langchain_text_splitters import TokenTextSplitter

text_splitter = TokenTextSplitter(
    chunk_size=256,
    chunk_overlap=32,
    encoding="cl100k_base"  # OpenAI tokenizer
)

texts = text_splitter.split_text("Large text...")

Semantic Chunking

from langchain_text_splitters import SemanticChunker

# Use embeddings for semantic splitting
text_splitter = SemanticChunker(embeddings)

texts = text_splitter.split_text("Large text with semantic boundaries...")

8. Resource Directory: LangChain

Best Books

BookAuthorPriceKey Topics
Learning LangChainO'ReillyPaidLangChain guide
Building LLM ApplicationsO'ReillyPaidProduction LLMs
Practical LangChainPacktPaidHands-on
AI EngineeringO'ReillyPaidAI systems

Best Udemy Courses

CourseInstructorPrice (INR)Key Topics
LangChain & LLMsInstructor₹1,999-2,999LangChain mastery
Building AI AppsInstructor₹1,499-2,299AI applications
RAG with LangChainInstructor₹1,499-2,299RAG systems
LangChain CookbookInstructor₹999-1,499Recipes

Best O'Reilly Resources

ResourceTopicAccess
Learning LangChainO'ReillyPaid
Building LLM ApplicationsO'ReillyPaid
Practical LangChainO'ReillyPaid

Best LinkedIn Learning Courses

CourseInstructorAccess
LangChain Essential TrainingInstructorPaid
LLM ApplicationsInstructorPaid
AI EngineeringInstructorPaid

Free Resources

PlatformResourceLink
LangChain DocsOfficial docspython.langchain.com
LangChain CookbookGitHubgithub.com/langchain-ai/langchain-cookbook
Awesome LangChainGitHubgithub.com/iff pore/awesome-langchain
LangChain UniversityFree coursesgithub.com/langchain-ai/langchain-university

9. Common LangChain Interview Questions

QuestionAnswer
What are LangChain chains?Chains combine LLMs with other components for complex workflows.
Difference between Runnable and Chain?Runnable is newer, more flexible interface for composing operations.
How to manage prompt templates?Use PromptTemplate and ChatPromptTemplate classes.
What is memory in LangChain?Memory components store conversation history and context.
How to handle streaming responses?Use stream() method and callbacks for real-time output.

10. Part Navigation

Previous Parts

Part 23: RAG Architectures

Next Parts

Part 25: LangGraph · Part 26: Multi-Agent Systems


Proceed to Part 25: LangGraph →

Comments

Comments are powered by giscus. Set PUBLIC_GISCUS_REPO_ID and PUBLIC_GISCUS_CATEGORY_ID in your environment to enable them.