LangChain Framework Basics

Comprehensive practical guide (Hindi+English mix) to LangChain: architecture, chains, agents, memory, prompts, document loaders, retrievers, integrations, deployment and hands-on projects.

LangChain Framework Basics — एक व्यावहारिक गाइड

LangChain आज के समय में LLM-based applications के लिए सबसे लोकप्रिय open-source frameworks में से एक है। यह developers को language models जैसे OpenAI GPT, LLaMA, Claude आदि को integrate कर production-ready pipelines बनाने की सुविधा देता है। इस ब्लॉग में हम LangChain के architecture, core modules और practical implementation पर detail में चर्चा करेंगे।

1. LangChain क्या है?

LangChain एक Python और JavaScript आधारित framework है, जो modular components जैसे chains, agents, memory, retrievers, prompts और document loaders प्रदान करता है। इसका उद्देश्य LLM applications के लिए reusable, testable और production-ready code बनाना है।

2. Architecture Overview

  • LLM Wrappers: OpenAI, Anthropic, HuggingFace, Azure OpenAI APIs के connectors।
  • Prompt Templates: Pre-defined prompt structures for reuse।
  • Chains: Sequential pipelines जो कई steps को जोड़ते हैं।
  • Memory: Stateful interactions को store करने की सुविधा।
  • Agents: Dynamic task execution with tools।
  • Retrievers & Loaders: External knowledge को integrate करना।

3. LangChain Core Components

3.1 Chains

Chains allow multiple calls to LLM or other components sequentially। उदाहरण के लिए, एक QA system के लिए text retrieval → summarization → answer generation chain बन सकती है।

3.2 Agents

Agents का इस्तेमाल तब होता है जब task execution dynamic हो। Agents tools का इस्तेमाल कर real-time में decide करते हैं कि कौन सा action लेना है। उदाहरण: web search, calculator, API calls।

3.3 Memory

Memory past conversations को store करता है, जिससे chatbots context-aware हो जाते हैं। इसमें short-term और long-term memory options available हैं।

3.4 Prompt Templates

LangChain में prompt templates से आप pre-defined reusable prompts बना सकते हैं।

3.5 Document Loaders और Retrievers

External data जैसे PDFs, websites, CSVs को load और retrieve करने के लिए loaders और retrievers का इस्तेमाल किया जाता है।

4. LangChain Installation

pip install langchain openai chromadb

5. Practical Example: LangChain QA Bot

एक simple QA chatbot के लिए LangChain का इस्तेमाल:

from langchain.llms import OpenAI
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate

template = PromptTemplate(
    input_variables=["question"],
    template="Answer the question: {question}"
)

llm = OpenAI(temperature=0.7)
chain = LLMChain(llm=llm, prompt=template)
print(chain.run("What is LangChain?"))
    

6. Integrations

LangChain में कई integrations हैं जैसे Pinecone, Weaviate, ChromaDB, Elasticsearch आदि। इनका उपयोग vector storage और retrieval के लिए होता है।

7. Deployment

LangChain apps को आप FastAPI, Flask या serverless platforms (AWS Lambda, Vercel) पर deploy कर सकते हैं। Docker images से production-grade deployment आसान होता है।

8. Best Practices

  • Prompt engineering पर ध्यान दें।
  • LLM calls optimize करें।
  • Memory और retrievers का सही use करें।
  • Agents को controlled environment में चलाएं।

9. Hands-on Projects Ideas

  • Custom domain-specific chatbot
  • Automated document summarizer
  • Financial data analysis assistant

LangChain का ecosystem rapidly evolve कर रहा है। नए modules और connectors हर महीने release होते हैं। इस गाइड को follow करके आप LLM-based applications efficiently बना पाएंगे।

10. Conclusion

LangChain developers को LLMs के साथ production-ready solutions बनाने के लिए एक structured approach देता है। अगर आप conversational AI, document QnA, या knowledge retrieval solutions बनाना चाहते हैं तो LangChain सीखना आवश्यक है।