Module 2 of 12
Curriculum Hub & Architecture
Current Study Module
LangChain - Getting Started & Core Setup
LangChain Expression Language (LCEL)
Module 01 • Environment & First Chain

Getting Started with LangChain

LangChain breaks down large language model engineering into clean, modular components. In this module, you will set up your development environment, configure secure API credentials, initialize your first ChatModel, and construct a production-ready runnable chain in both Python and TypeScript.

1. Package Installation & Architecture

LangChain uses a decoupled package architecture to optimize bundle sizes and decouple provider release cycles:

  • langchain-core: Base abstractions, Runnable protocol, PromptTemplates, and OutputParsers.
  • langchain-openai / @langchain/openai: Dedicated integrations with OpenAI models (GPT-4o, GPT-4o-mini).
  • langchain-community: Third-party integrations for vector stores, tools, and local models like Ollama.
Python Installation:
# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install modern modular packages
pip install langchain-core langchain-openai python-dotenv
TypeScript / Node.js Installation:
# Install via npm or pnpm
npm install @langchain/core @langchain/openai dotenv

2. Environment Configuration

Never hardcode API keys directly into application logic. Create a .env file in your project root:

# .env configuration
OPENAI_API_KEY="sk-proj-your-openai-api-key-here"
LANGCHAIN_TRACING_V2="true"
LANGCHAIN_API_KEY="lsv2_pt_your-langsmith-key-here"
LANGCHAIN_PROJECT="hifi-langchain-tutorial"
Security Note: Always add .env to your .gitignore file to prevent accidental credential leakage into public GitHub repositories.

3. Constructing Your First Runnable Chain (Python & TypeScript)

Every standard LangChain pipeline consists of three fundamental stages: (1) PromptTemplate converts raw user inputs into structured ChatMessages, (2) ChatModel evaluates the messages and generates model responses, and (3) OutputParser extracts the clean text string or JSON object.

Python Implementation (LCEL):
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

# Load environment credentials
load_dotenv()

# 1. Define prompt template with role structuring
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a senior systems engineer specializing in distributed cloud architecture."),
    ("human", "Explain the concept of {topic} in exactly 2 concise bullet points.")
])

# 2. Configure model
llm = ChatOpenAI(
    model="gpt-4o-mini",
    temperature=0.3,
    max_tokens=250
)

# 3. Create chain via pipe operator
chain = prompt | llm | StrOutputParser()

# 4. Invoke synchronous execution
response = chain.invoke({"topic": "Eventual Consistency in NoSQL databases"})
print("--- Direct Invocation Output ---")
print(response)
TypeScript / Next.js Implementation:
import { ChatOpenAI } from "@langchain/openai";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { StringOutputParser } from "@langchain/core/output_parsers";

// 1. Initialize template
const prompt = ChatPromptTemplate.fromMessages([
  ["system", "You are an expert full-stack engineer."],
  ["human", "What is the primary benefit of {framework} in production?"]
]);

// 2. Initialize Model
const model = new ChatOpenAI({
  model: "gpt-4o-mini",
  temperature: 0.2,
});

// 3. Compose Runnable Pipeline
const chain = prompt.pipe(model).pipe(new StringOutputParser());

// 4. Invoke
const result = await chain.invoke({ framework: "Next.js App Router" });
console.log(result);

4. Zero-Cost Local Inference with Ollama (Llama 3 / Mistral)

LangChain allows you to switch between proprietary cloud models and free local open-source models by changing just one line of code:

# Install community package: pip install langchain-community
from langchain_community.chat_models import ChatOllama
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

# Point to locally running Ollama instance (e.g. running Llama 3.1)
local_llm = ChatOllama(model="llama3.1", temperature=0.1)

local_chain = (
    ChatPromptTemplate.from_template("Translate the following technical term into Hindi: {term}")
    | local_llm
    | StrOutputParser()
)

print(local_chain.invoke({"term": "Distributed Hash Table"}))
Key Takeaways & What’s Next
  • Modern LangChain uses modular packages (langchain-core, langchain-openai).
  • Chains are formed using the declarative pipe syntax (prompt | model | parser).
  • In the next module, we master LCEL (LangChain Expression Language) for async batching, real-time token streaming, and parallel fallbacks.

Explore Related Tools

Hand-picked utilities and calculators related to this tool.

Related Tools