Installation and Basic Setup
What you'll learn: How to install LangChain, manage its dependencies, and configure your first connection to an LLM using API keys.
Getting LangChain Installed
LangChain is a Python framework that orchestrates interactions with LLMs, vector databases, and other AI tools. Installing it is straightforward, but since it's modular, you'll need to choose which components you need.
Basic installation:
pip install langchain
For working with specific LLMs (like OpenAI), you'll also need provider-specific packages:
pip install langchain-openai
Think of LangChain like a universal adapter kit—the core package gives you the orchestration tools, but you add specific "plugs" for each service you want to connect to.
Managing Dependencies
LangChain has many optional dependencies. Start minimal and add what you need:
pip install langchain langchain-openai python-dotenv
The python-dotenv package helps you manage API keys securely by loading them from environment files.
Configuring Your First LLM Connection
Create a .env file in your project root to store API keys securely:
OPENAI_API_KEY=sk-your-key-here
Then connect to an LLM in Python:
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
# Load environment variables
load_dotenv()
# Initialize LLM with API key from environment
llm = ChatOpenAI(
model="gpt-3.5-turbo",
temperature=0.7,
api_key=os.getenv("OPENAI_API_KEY")
)
Never hardcode API keys in your scripts—always use environment variables or secure key management systems.
Key Takeaway: LangChain installation is modular—install the core package plus provider-specific integrations, then configure LLM connections using environment variables to keep API keys secure and separate from your code.