Mental Model and Core
What is LangChain? The shift from standard programming to orchestrating LLMs.
🧑🏫 Sabse pehle — simple mein samjho#
Normal programming mein har cheez rule-based hoti hai (if-else, loops). Par jab AI (LLMs jaise ChatGPT) beech mein aata hai, toh rules kaam nahi karte kyunki AI text generate karta hai jo hamesha thoda alag ho sakta hai. LangChain ek framework hai jo LLMs ke chaotic nature ko tame karta hai. Ye tumhe tools deta hai jisse tum AI ko specific data (PDFs) padha sako, usko internet search karne ki power (Tools) de sako, aur uski previous baaton ko yaad rakhne ki kshamta (Memory) de sako. Simply put: LangChain LLM ka dimag (brain) aur baki duniya ke beech ka bridge hai.
The Shift in Programming Paradigm#
In traditional software development (Node.js, Django), you write deterministic code: given input A, the system applies strict logic and always returns output B.
When building AI applications, you are writing non-deterministic code. You provide an instruction (a Prompt), and the Large Language Model (LLM) generates an output probabilistically. LangChain was built to provide structure and reliability around this non-deterministic process.
What LangChain Actually Does#
LangChain does NOT build or train AI models. You still use models from OpenAI (GPT-4), Anthropic (Claude), or local models (via Ollama). LangChain simply orchestrates how you interact with them.
It handles the complex plumbing required to build robust AI apps:
- Format Input: Connecting to various models using a single, unified syntax.
- Add Context: Injecting relevant data (from your own databases or PDFs) into the prompt before sending it to the LLM.
- Parse Output: Forcing the LLM to return strictly formatted JSON instead of conversational text so your application can actually use the result in a database.
- Chain Logic: Taking the output of one LLM call and immediately using it as the input for another LLM call or a normal Python function.
The Core Components (The LangChain Ecosystem)#
LangChain is built out of distinct, modular components. Understanding these is the key to mastering the framework:
- Models (LLMs & ChatModels): The core engine (e.g.,
ChatOpenAI). - Prompts (PromptTemplates): The structured instructions you give to the model, containing dynamic variables.
- Output Parsers: Tools that extract specific data structures (like JSON or lists) from the raw text generated by the model.
- Retrieval (RAG): The system for loading documents, splitting them, converting them to numbers (embeddings), and storing them in Vector Databases so the LLM can search through your private data.
- Agents & Tools: Giving the LLM the ability to "think" and decide to use external functions (like searching Google or querying a SQL database) to answer a user's question.
- LCEL (LangChain Expression Language): The modern, pipe-based
|syntax used to connect all these components together seamlessly.
Python vs JavaScript#
LangChain was originally built in Python, and the Python ecosystem remains slightly more advanced (especially regarding data science tools and vector stores). However, langchain.js (for Node.js/TypeScript) is heavily supported and follows almost the exact same architecture. These notes will focus on the concepts, which apply perfectly to both languages, though code examples will generally lean towards Python syntax (which is the industry standard for AI orchestration).