💻 Practice Notebook
Master the concepts from this page with hands-on practice: 💻 VS Code | 🚀 Colab | 📥 Download Notebook Large Language Models (LLMs) have transformed how we build software. However, building production-grade GenAI applications requires orchestration. This module introduces the fundamentals of LangChain, explains the problems it solves, and walks you through setting up a modern GenAI project.1. Traditional vs. GenAI Applications
Building applications with Generative AI requires a paradigm shift from traditional software development:2. Two Kinds of GenAI Applications
LLM-powered systems are generally categorized into two workflow architectures:- Sequential Workflows (Deterministic): The execution path is hardcoded and predefined by the developer. The inputs and outputs flow sequentially from one step to another (e.g., Prompt -> LLM -> Parser -> Database).
- Agentic Workflows (Autonomous): The LLM operates as an autonomous agent inside a loop. Given a task, the model evaluates the current state and dynamically decides which actions to take or tools (such as web search, calculator, or DB query) to invoke at runtime.
3. The Challenges of Raw API Integrations
Directly writing code against raw LLM provider APIs (like OpenAI, Google, or Anthropic) introduces several challenges in real-world software engineering:- API Fragmentation: Every model provider has its own proprietary SDK, request payload structure, and response format. Switching providers means rewriting your entire code integration.
- Complex Pipeline Orchestration: Real-world GenAI applications rarely rely on a single API call. They require linking prompts, vector search retrievers, output parsers, and custom tools in sequence.
- State & Memory Management: LLMs are stateless by design. Developers must manually manage conversation history and context window limits.
How LangChain Solves This
LangChain acts as a unified abstraction layer over LLMs:- Standardized Interfaces: Write code against generic classes (
ChatModel,PromptTemplate,BaseOutputParser) and easily swap underlying models/providers with a single line of code. - LangChain Expression Language (LCEL): A declarative composition system utilizing the pipe operator (
|) to build and stream multi-step GenAI pipelines. - Ecosystem Modularity: It splits components into light, specialized libraries (
langchain-core, provider packages likelangchain-groq, andlangchain-community).
4. Direct APIs vs. LangChain
To understand why LangChain is needed, let’s compare direct API integrations for three popular providers (OpenAI, Gemini, Hugging Face) against LangChain’s unified syntax.3.1 Direct Provider APIs (Fragmentation)
Every provider requires a unique SDK, setup protocol, and response extraction syntax:OpenAI Direct API
Google Gemini Direct API
Hugging Face Inference API
3.2 LangChain’s Simplified & Unified Syntax
LangChain unifies all these disparate APIs behind a single interface. Switching between providers only requires changing model configuration variables:5. Main LangChain Modules
LangChain divides its components into specialized modules for clean dependency management:langchain-core: The foundational package defining interfaces for models (BaseChatModel), templates (BasePromptTemplate), and the LCEL chaining logic.- Provider Integration Packages: Specific packages (e.g.
langchain-google-genai,langchain-groq) containing lightweight wrapper logic for provider-specific APIs. langchain-community: Integrations maintained by the community for third-party vector databases, document loaders, and tools.
6. Setting Up a GenAI Project (Step-by-Step)
We will useuv, a fast, modern package and project manager for Python, to set up our application.
Step 6.1: Initialize the Project & Virtual Environment
Open your terminal and run the following commands:Step 6.2: Add Dependencies
Add the core LangChain package, provider integration packages, and a library to read environment variables:Step 6.3: Set Up Your Keys (.env)
Create a file named .env in the root of your project directory and add your API keys:
Step 6.4: Load Environment Variables in Python
To read the keys from your.env file and make them available to your application:
- Import
load_dotenvfrom thedotenvlibrary. - Call
load_dotenv()at the very start of your python script.
os.environ system dictionary. LangChain automatically looks for variables named GROQ_API_KEY and GOOGLE_API_KEY in os.environ, allowing you to initialize models without hardcoding credentials in your source code.
7. Initializing and Calling Models
Here is how to write python scripts to call either Groq or Google Gemini using LangChain.7.1 Initializing with Groq
7.2 Initializing with Google Gemini
[!NOTE] When usinginit_chat_model, LangChain automatically detects theGROQ_API_KEYorGOOGLE_API_KEYfrom your environment variables.
8. Practice Exercises
Practice 1: Dual-Provider Setup & Comparison
Write a script that loads environment variables, prompts both Groq (llama-3.3-70b-versatile) and Google (gemini-2.5-flash) with the question "State the main goal of prompt engineering in 5 words.", and prints the response from each model.
Solution
Solution
💻 Practice Notebooks
Master all the concepts from this module with hands-on practice:- Practice in VS Code: Open the notebook in your local editor. Requires a local
.envfile containing your API keys. - Practice in Google Colab: Open the notebook directly in Colab. Setup cells are included to install packages and request API keys.