> ## Documentation Index
> Fetch the complete documentation index at: https://genai.codewithsiva.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Introduction to LangChain

> Why LangChain, project setup with uv, and main capabilities

* **Orchestration Need**: While LLMs are powerful, production-grade GenAI applications require coordinating prompts, models, retrievers, and memory.
* **Module Goal**: Introduce LangChain fundamentals, detail raw API integration challenges, and walk through project setup.

### 1. Traditional vs. GenAI Applications

* **Paradigm Shift**: Developing GenAI applications requires a fundamental change in software development practices:

| Aspect           | Traditional Software                                                    | GenAI Applications                                                                                  |
| ---------------- | ----------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------- |
| **Logic**        | Deterministic and rule-based (defined by code loops, `if-else` blocks). | Probabilistic (guided by LLM semantic reasoning and prompts).                                       |
| **Input/Output** | Structured data (JSON, databases, arguments).                           | Unstructured natural language (text, speech, images).                                               |
| **Execution**    | Consistent and predictable; same inputs yield exact same outputs.       | Dynamic; outputs can vary (non-deterministic) depending on context, temperature, and model updates. |

### 2. Two Kinds of GenAI Applications

* **Architecture Categorization**: LLM-powered systems are split into two core workflow designs:

1. **Sequential Workflows (Deterministic)**:
   * **Definition**: Linear execution path predefined entirely by the developer. Inputs and outputs flow sequentially from step to step.
   * **Common Frameworks**:
     * **LangChain**: Uses **LCEL (LangChain Expression Language)** to chain components.
     * **LlamaIndex**: Uses **Query Engines** and **Ingestion Pipelines** for structured data.
     * **Haystack**: Uses Directed Acyclic Graphs (DAGs) to orchestrate runs.
     * **Semantic Kernel**: Microsoft's SDK for sequential workflows.
   * **Why**:
     * Designs predictable, repeatable pathways.
     * Built-in capabilities for streaming, batching, and async execution.
     * Control flow logic remains entirely inside the codebase rather than the model.

2. **Agentic Workflows (Autonomous)**:
   * **Definition**: Stateful feedback loops where the LLM operates as a dynamic decision-maker, determining its own execution path.
   * **Common Frameworks**:
     * **LangGraph**: LangChain's system for stateful, cyclical multi-agent graphs.
     * **CrewAI**: Orchestrator for structured, role-playing autonomous agent teams.
     * **Microsoft AutoGen**: Conversational agent programming framework.
     * **LlamaIndex Workflows**: Event-driven agentic loops.
   * **Why**:
     * Resolves complex, open-ended tasks that linear pipelines cannot.
     * Allows stateful loops, branching conditions, and human-in-the-loop steps.
     * Model dynamically selects and queries external tools (Python, SQL, web searches) based on live environment feedback.

### 3. The Challenges of Raw API Integrations

* **Integration Issues**: Direct integration with raw LLM provider APIs introduces three software engineering challenges:
  * **API Fragmentation**: Multi-vendor SDKs have distinct request formats, payload structures, and response schemas. Swapping vendors requires major codebase refactoring.
  * **Orchestration Complexity**: Production-grade apps require combining prompts, vector stores, output parsers, and custom tools in sequence.
  * **Statelessness**: LLMs do not retain chat history; developers must manually maintain conversation logs and calculate token limits.

#### How Orchestration Frameworks Address These Challenges

* **Unified Abstraction Layer**: Frameworks like LangChain simplify developer workflows:
  * **Standardized Interfaces**: Use unified component classes (e.g. models, prompts, parsers), enabling provider swaps with minimal code changes.
  * **Declarative Composition**: Offer visual/expressive syntax (e.g., LCEL) to easily string components together.
  * **Modular Libraries**: Decouple core classes from integrations, allowing developers to import lightweight packages and prevent bloated dependencies.
  * **Built-in Memory**: Provide native state containers to automatically track, truncate, and save conversation histories.

### 4. Main LangChain Modules & Capabilities

* **Modular Services**: LangChain provides components to build custom GenAI apps:
  * **Chat Models**: Standardized messaging interface to query diverse LLM vendors.
  * **Prompt Templates**: Utilities to structure and format inputs with dynamic variables.
  * **Output Parsers**: Extract and parse raw string outputs into structured JSON or Pydantic formats.
  * **LCEL (LangChain Expression Language)**: Declarative engine to chain models, prompts, and parsers.
  * **Document Loaders & Vector Stores**: Tools to load raw files (PDFs, CSVs) and query them for RAG.
  * **Tools (Function Calling)**: Allow LLMs to access external services (APIs, databases, Python runtimes).
  * **Agents (LangGraph)**: Stateful loops where the LLM decides actions and calls tools.
  * **Memory**: Helpers to automatically persist and pass conversation context.

#### 4.1 Library Architecture & Segregation

* **Package Segregation**: LangChain splits its codebase into separate libraries to keep installs lightweight:
  * **Core Abstractions (`langchain-core`)**: Holds basic base classes and LCEL engine (zero third-party dependencies).
  * **Partner Packages (First-Party)**: Provider-specific libraries (e.g., `langchain-openai`, `langchain-google-genai`) maintained for high performance.
  * **Community Integrations (`langchain-community`)**: Community-maintained integrations for vector databases, tools, and loaders.

#### 4.2 Application Workflows & Module Mapping

* **Module Mapping**: Architectures rely on specific tool combinations:

| Application Pattern               | Target Goal                                 | Key Modules & Libraries Used                                                                    |
| :-------------------------------- | :------------------------------------------ | :---------------------------------------------------------------------------------------------- |
| **Conversational Chatbot**        | Maintain multi-turn dialogue with users     | `Chat Models` + `Prompt Templates` + `Memory` (`langchain-core`)                                |
| **Retrieval-Augmented Gen (RAG)** | Ground answers in proprietary documents     | `Chat Models` + `Document Loaders` + `Vector Stores` (`langchain-community` & partner packages) |
| **Autonomous Agent**              | Solve open-ended tasks using external tools | `Chat Models` + `Tools` (Function Calling) + `State Graphs` (`LangGraph`)                       |

### 5. Setting Up a GenAI Project (Step-by-Step)

We will use **`uv`**, a fast, modern package and project manager for Python, to set up our application.

#### Step 5.1: Initialize the Project & Virtual Environment

Open your terminal and run the following commands:

```bash theme={null}
# Initialize a new project directory
uv init genai-app
cd genai-app

# Create and activate a virtual environment
uv venv
source .venv/bin/activate
```

#### Step 5.2: Add Dependencies

Add the core LangChain package, provider integration packages, and a library to read environment variables:

```bash theme={null}
# Add LangChain core and provider-specific integrations
uv add langchain-core langchain-groq langchain-google-genai python-dotenv
```

#### Step 5.3: Set Up Your Keys (`.env`)

Create a file named `.env` in the root of your project directory and add your API keys:

```ini theme={null}
# Groq API Key (Fast inference for open models)
GROQ_API_KEY=gsk_your_groq_api_key_here

# Google Gemini API Key
GOOGLE_API_KEY=AIzaSyYourGeminiApiKeyHere
```

#### Step 5.4: Load Environment Variables in Python

To read the keys from your `.env` file and make them available to your application:

1. Import `load_dotenv` from the `dotenv` library.
2. Call `load_dotenv()` at the very start of your python script.

```python theme={null}
from dotenv import load_dotenv

# Search and load keys from the local .env file
load_dotenv()
```

* **Environment Loading**: `load_dotenv()` parses key-value pairs from your local `.env` file and populates the system environment variables (`os.environ`).
* **Automatic Detection**: LangChain dynamically reads API keys (e.g., `GROQ_API_KEY`, `GOOGLE_API_KEY`) from environment variables when initializing models.
* **Security Benefit**: Prevents hardcoding sensitive credentials and API keys in your application source code.

## Practice & Exercises

To reinforce what you've learned in this section, practice with the interactive notebook:

<CardGroup cols={1}>
  <Card title="Practice & Exercises" icon="laptop-code">
    Verify your environment setup and run your first import tests.

    [💻 VS Code](vscode://file/Users/sivaprasad/Downloads/GenAI%20With%20Python/public/notebooks/langchain/Intro.ipynb) | [🚀 Colab](https://colab.research.google.com/github/prasad230776/genai-course/blob/master/public/notebooks/langchain/Intro.ipynb) | <a href="/public/notebooks/langchain/Intro.ipynb" download>📥 Download</a>
  </Card>
</CardGroup>
