LangChain Framework
AI applications ko build, chain, aur manage karna seekho LangChain ke saath
Why Learn LangChain?
LangChain se AI applications build karte hain � chains, agents, tools sab LangChain se manage hote hain. Production AI apps ke liye zaroori hai. Ye framework LLMs ke saath complex workflows ko simple banata hai.
LangChain ke Core Concepts
CHAINS
Sequential operations ka pipeline jo multiple steps ko ek saath connect karta hai
AGENTS
AI with tools � decision making aur dynamic tool usage
PROMPT TEMPLATES
Reusable prompts jo different inputs ke saath kaam karte hain
MEMORY
Conversation history store aur manage karna for context retention
LangChain Basics
LangChain ek Python framework hai jo LLM-powered applications banane ke liye use hota hai. Ye provide karta hai:
- Chains - Multiple components ko connect karna
- Agents - Dynamic tool selection aur execution
- Prompt Templates - Reusable prompt structures
- Memory - Conversation context maintain karna
- Document Loaders - Different data sources se data load karna
- Vector Stores - Embeddings store aur retrieve karna
Code Example: LangChain Basics
# LangChain basics # pip install langchain openai # Simple chain from langchain.prompts import PromptTemplate from langchain.chains import LLMChain template = """You are a helpful assistant. Answer in Hinglish: Question: {question} Answer:""" prompt = PromptTemplate(template=template, input_variables=["question"]) # RAG with LangChain from langchain.vectorstores import FAISS from langchain.embeddings import OpenAIEmbeddings # documents = ["doc1", "doc2"] # vectorstore = FAISS.from_documents(documents, OpenAIEmbeddings()) # relevant_docs = vectorstore.similarity_search("query", k=2) print("LangChain components ready!") print("Chains, Agents, Memory sab available hain")
Prompt Templates Deep Dive
Prompt templates ko different use cases ke liye customize kar sakte ho:
- Variable placeholders use karo ({question}, {context})
- Few-shot examples add karo for better responses
- System instructions define karo for role-playing
- Output format specify karo (JSON, list, etc.)
✓ Interactive Editor: Create Your LangChain Chain
Memory Management
LangChain mein different memory types available hain:
- ConversationBufferMemory - Poori conversation store karta hai
- ConversationSummaryMemory - Summary store karta hai (token save)
- ConversationBufferWindowMemory - Sliding window approach
- ConversationTokenBufferMemory - Token limit ke saath
Agents Overview
Agents LangChain ka sabse powerful feature hain:
- Dynamic Tool Selection - Kaunsa tool use karna hai decide karta hai
- Reasoning - Step-by-step thinking karta hai
- Loop Execution - Jab tak task complete nahi hota, tools use karta hai
- Human-in-the-loop - Zarurat par human se input leta hai
Code Example: Memory Integration
# Memory integration with LangChain from langchain.memory import ConversationBufferMemory from langchain.chains import ConversationChain # Memory create karo memory = ConversationBufferMemory() # Conversation chain with memory # chain = ConversationChain( # llm=llm, # memory=memory, # verbose=True # ) # Memory types: # 1. ConversationBufferMemory - Full history # 2. ConversationSummaryMemory - Summary # 3. ConversationBufferWindowMemory - Sliding window print("Memory types available:") print("1. Buffer Memory") print("2. Summary Memory") print("3. Window Memory")
Exercise Time!
LangChain mein Chain kya hai?
Key Takeaways
- LangChain AI applications ke liye ek complete framework hai
- Chains, Agents, Memory - ye three pillars hain LangChain ke
- Prompt Templates se reusable prompts bana sakte ho
- Memory se conversation context maintain hota hai
- Agents dynamic tool usage enable karte hain
- Production mein LangChain bahut useful hai