Langchain components
LangChain is a powerful framework for building applications that use Large Language Models (LLMs). It modularizes LLM workflows into reusable and extensible components, allowing developers to create complex applications like chatbots, agents, and RAG pipelines.

1. Models (LLMs, ChatModels)
- Purpose: To interact with language models like OpenAI’s GPT, Anthropic, HuggingFace, etc.
- Types:
LLM: For single-turn completions (e.g., text completion).ChatModel: For multi-turn conversations (e.g., ChatGPT-like behavior).
2. Prompts
- Purpose: To structure the inputs sent to LLMs.
- Subcomponents:
PromptTemplate: Templates with input variables (e.g.,"Translate {text} to French").ChatPromptTemplate: Specialized for chat models with roles (user, assistant, system).FewShotPromptTemplate: Includes examples to guide the model via few-shot learning.
3. Chains
- Purpose: Combine LLMs with prompts and logic.
- Types:
LLMChain: Basic chain combining a prompt and an LLM.SimpleSequentialChain: Executes chains one after another.SequentialChain: Supports passing outputs between chains.RouterChain: Routes input to different chains dynamically.
4. Tools
- Purpose: External functions or APIs the model can call.
- Examples:
- Search engine queries
- Calculator
- SQL/NoSQL database access
- Python function executor
5. Agents
- Purpose: LLMs that decide which tools to use in which order.
- Agent Types:
ReAct Agent: Reasoning and acting with tools.Conversational Agent: Maintains memory of past interactions.- LangChain Agent Framework: Manages tool usage, state, memory, and decision logic.
6. Memory
- Purpose: Stores state and conversation history.
- Types:
ConversationBufferMemory: Raw message buffer.ConversationSummaryMemory: Summarizes history using an LLM.VectorStoreRetrieverMemory: Stores past interactions as embeddings.
7. Document Loaders
- Purpose: Extract text from various file formats (PDF, HTML, Markdown, etc.).
- Examples:
UnstructuredPDFLoaderTextLoaderWebBaseLoader
8. Text Splitters (Chunking)
- Purpose: Break large documents into smaller chunks (for embeddings).
- Strategies:
- RecursiveCharacterTextSplitter
- TokenTextSplitter (based on LLM token limits)
9. Embeddings
- Purpose: Convert text chunks into vector representations.
- Providers:
- OpenAI (
text-embedding-ada-002) - Sentence Transformers (HuggingFace)
- Cohere
10. Vector Stores
- Purpose: Store and search text embeddings.
- Supported DBs:
- Pinecone
- Chroma
- FAISS
- Weaviate
- Usage: Index chunks → retrieve relevant ones based on query similarity.
11. Retrieval
- Purpose: Search relevant chunks using a query.
- Often used in RAG pipelines to ground LLM responses on retrieved data.
12. Output Parsers
- Purpose: Parse structured outputs from LLMs.
- Useful for extracting JSON, tables, or specific formats from raw LLM output.
13. Callbacks
- Purpose: For logging, tracing, and observing intermediate steps.
Using models with Langchain
- Refer Here for models api
- Refer Here for chat models
- Messages
LLM
- A Large Language Model:
- Is trained on massive text corpora.
- Predicts the next word/token in a sentence.
- Powers applications like text generation, summarization, translation, etc.
- Examples: GPT-3, GPT-4, LLaMA, Cohere, Anthropic Claude.
Chat Model
- A Chat Model:
- Is a conversational interface to an LLM.
- Supports multi-turn dialogues.
- Maintains role-based inputs: user, assistant, system.
- Example: ChatGPT, Claude, ChatOpenAI in LangChain.
Setting up Langchain
- Install dependencies
pip install langchain openai
Using a Text Completion LLM in LangChain
- Step 1: Import and intialize
from langchain.llms import OpenAI
llm = OpenAI(model_name="text-davinci-003")
- Provide a Prompt
prompt = "Write a motivational quote for programmers."
response = llm(prompt)
print(response)
Using a Chat Model in LangChain
-
Refer Here for official docs and Refer Here for Messages
-
Step 1: Import Modules
from langchain.chat_models import ChatOpenAI
from langchain.schema import HumanMessage
- Step 2: Intialize Chat Model
chat = ChatOpenAI(model_name="gpt-4")
- Step 3: Send a Message
messages = [HumanMessage(content="What is the capital of France?")]
response = chat(messages)
print(response.content)
Using model with Hugging face
- Step 1: Install transformers
pip install transformers
- Step 2: Generate text using gpt2
from transformers import pipeline
generator = pipeline("text-generation", model="gpt2")
result = generator("What is the future of AI?", max_length=50, do_sample=True)
print(result[0]["generated_text"])
- Step 3 install extra
pip install langchain huggingface_hub
- Step 4: Using a Model from langchain model hub by setting an api token
from langchain.llms import HuggingFaceHub
# Make sure you’ve set your token in environment: HUGGINGFACEHUB_API_TOKEN
llm = HuggingFaceHub(repo_id="google/flan-t5-small", model_kwargs={"temperature": 0.5})
response = llm("Translate English to French: How are you?")
print(response)
- Step 5: load a local model
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
from langchain.llms import HuggingFacePipeline
# Load model & tokenizer locally
model_name = "distilgpt2" # or any downloaded model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# Create text generation pipeline
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
# Wrap with LangChain
llm = HuggingFacePipeline(pipeline=pipe)
# Generate response
prompt = "Once upon a time"
print(llm(prompt))
- Model Message and Prompt

