LangChain
LangChain is an open-source framework designed to simplify the development of applications powered by LLMs.
Why Do we need LangChain?
Although LLMs are powerful, they have several limitations:
- No Long-term Memory: Traditional LLMs process each request independently and they do not remember past conversation
- No Access to Private Knowledge: LLMs cannot ans the info about company docs etc
- No Real-Time information
- Inablity to use External Tools: Tasks like database queries, web searches, and API interactions require additional system, LLMs alone can’t handle it.
Langchain help developers to build applications by combining:
- LLMs
- Prompt Templates
- Memory Systems
- Retrieval Mechanisms
- Vector Databases
- External Tools and APIs
- Agents
Instead of a simple workflow:
User → LLM →Response
Langchain enables:
User →Prompt →LLM →Memory →Tools →Database →Response
Core Components
- Models: It is the foundation of every LanChain applications. It supports
- OpenAI, Anthropic Claude, Google Gemini, Meta Llama, Open-Source Hugging Face Models
- These models perform tasks such as text generation, summarization, Translation, Question answering etc
Code Example
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o-mini",temperature=0.7)
response=llm.invoke("Explain Deep Learning")
print(response.content)
2. Prompt Templates: It allow developers to create reusable and dynamic prompts.
Hardcoded Prompt:
prompt = """
Explain ML
"""
It is not reusable
Prompt Template Example
from langchain.prompts import PromptTemplate
prompt = PromptTemplate(input_variables=["topic"],template="""Explain{topic}""")
prompt.format(topic="NLP")
ChatPromptTemplates: It is used for chat models
Structure:
System Message, Human Message, AI Message
from langchain.prompts import ChatPrompt Template
prompt=ChatPromptTemplate.from_messages(
[( "system","You are an AI Professor"),
("human","Explain {topic}")
])
Types of Prompting:
- Zero-Shot Prompting: In this, no example has provided in prompt
- One-Shot Prompting: It has one example
- Few-Shot Prompting: It has multiple examples
- Chain of Thought Prompting: It encourages reasoning i.e., think step-by-step.
- ReAct Prompting: Reason+Action.
3. Chains: It combines multiple operations into a single worlflow. Instead of manually calling each component, LangChain allows developers to connect them into a pipeline.
Get Shraddha Tiwari’s stories in your inbox
Join Medium for free to get updates from this writer.
Types of Chain:
- Simple Chain: Input →Prompt →LLM →Output
- Sequential Chain: Output of one chain becomes input of another
Input →Summary Generator →Quiz Generator →Output
Example:
chain1 = prompt1|llm
chain2 = prompt2|llm
final_chain = chain1|chain2
3. Parallel Chain: Multiple chains run simultaneously.
Input →(Summary, Sentiment, Keywords) →Combined
It is useful for speed.
4. Output Parsers: LLMs returns text, but Applications often need structured data.
Without Parser:
Name: Kajal
Age: 18
With Parser:
{
“name”:”kajal”,
“age”:18
}
Types
- StrOutputParser: It returns String
from langchain_core.output_parsers import StrOutputParser
2. JsonOutputParser: Return JSON
from langchain.output_parsers import JsonOutputParser
3. PydanticOutputParser: Returns validated objects
class User(BaseModel):
name:str
age:int
5. Memory: It lets LLMs remember the past conversation memory
Types
- ConversationBufferMemory: It stores entire chat.
- ConversationBufferWindowMemory: Stores last N conversations
- ConversationSummaryMemory: It stores summary, instead of entire chat
- Vector Memory: It stores conversations as embeddings. Useful for embeddings.
6. Document Loaders: It load external data
PDF: PyPDFLoader, DOCX: Docx2txtLoader, CSV: CSVLoader, Website: WenBaseLoader, Text: TextLoader, JSON: JSONLoader
from langchain_community.document_loaders import PyPDFLoader
loader = PyPDFLoader("notes.pdf")
notes = loader.load()
7. Text Splitters: It break large documents into chunks. LLMs have token limits, So the solution is:
PDF →Chunk1, Chunk2, Chunk3 etc
RecursiveCharacterTextSplitter
splitter = RecursiveCharacterTextSplitter(chunk_size=1000.chunk_overlap=200)
Without chunk_overlap context will be lost.
8. Embeddings: Numerical representation of text, as system can’t understand words
Models:
- OpenAI Embeddings, Sentence Transformers, BGE, E5, Instructor XL










