LangChain Complete Guide for Python Developers
Advertisement
Introduction
LangChain has become the de facto standard framework for building LLM applications. It provides abstractions for language models, prompts, chains, and tools, making it easier to build complex AI workflows. This guide walks you through LangChain's core concepts and shows you how to build production-ready applications.
- What is LangChain?
- Installation and Setup
- Building Your First Chain
- Working with Memory
- Chains: Sequential Operations
- Agents and Tools
- Document Processing and RAG
- Best Practices
- Conclusion
- FAQ
What is LangChain?
LangChain is a framework that simplifies building applications powered by language models. It connects LLMs to external data sources, allows complex reasoning through chains, and integrates with dozens of tools and APIs.
Key components:
- Models: Interfaces to LLMs (OpenAI, Anthropic, Hugging Face)
- Prompts: Templates for constructing model inputs
- Chains: Sequences of calls to models or other tools
- Memory: Persisting conversation history
- Agents: Using tools to accomplish tasks autonomously
- Document Loaders: Ingesting data from PDFs, URLs, databases
Installation and Setup
pip install langchain langchain-openai langchain-community python-dotenv
Configure your API keys:
import os
from dotenv import load_dotenv
load_dotenv()
os.environ["OPENAI_API_KEY"] = "your-key-here"
Building Your First Chain
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
# Initialize the model
llm = ChatOpenAI(model="gpt-4", temperature=0.7)
# Create a prompt template
prompt = ChatPromptTemplate.from_template(
"Explain {topic} in one sentence for a {audience}."
)
# Build the chain using LCEL (LangChain Expression Language)
chain = prompt | llm | StrOutputParser()
# Execute
result = chain.invoke({
"topic": "quantum computing",
"audience": "10-year-old child"
})
print(result)
This demonstrates LCEL (LangChain Expression Language), the pipe operator syntax for composing chains declaratively.
Working with Memory
Conversational applications require remembering past messages. LangChain provides several memory types:
from langchain_core.messages import HumanMessage, AIMessage
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4")
# Simple conversation without built-in memory
messages = [
("system", "You are a helpful AI assistant."),
("user", "What is machine learning?"),
]
response = llm.invoke(messages)
print(response.content)
# Add to conversation history
messages.append(("assistant", response.content))
messages.append(("user", "Can you give me an example?"))
response2 = llm.invoke(messages)
For production, use ConversationBufferMemory or ConversationSummaryMemory:
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationChain
memory = ConversationBufferMemory()
conversation = ConversationChain(
llm=llm,
memory=memory,
verbose=True
)
conversation.predict(input="Hello!")
Chains: Sequential Operations
Chains execute multiple steps in sequence. Use LCEL to compose them:
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import JsonOutputParser
from pydantic import BaseModel, Field
class BlogPost(BaseModel):
title: str = Field(description="Blog post title")
outline: list[str] = Field(description="5-point outline")
llm = ChatOpenAI(model="gpt-4")
parser = JsonOutputParser(pydantic_object=BlogPost)
prompt = ChatPromptTemplate.from_template(
"Create a blog post about {topic}.\n{format_instructions}"
).partial(format_instructions=parser.get_format_instructions())
chain = prompt | llm | parser
result = chain.invoke({"topic": "LLM safety"})
print(result)
Agents and Tools
Agents use LLMs to decide which tools to call. Define tools using decorators:
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from langchain.agents import create_react_agent, AgentExecutor
from langchain import hub
@tool
def get_weather(location: str) -> str:
"""Get current weather for a location."""
return f"Sunny, 72°F in {location}"
@tool
def search_web(query: str) -> str:
"""Search the web for information."""
return f"Search results for: {query}"
tools = [get_weather, search_web]
llm = ChatOpenAI(model="gpt-4")
# Use ReAct prompt from LangChain Hub
prompt = hub.pull("hwchase17/react")
agent = create_react_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
result = executor.invoke({
"input": "What is the weather in San Francisco and who won the World Series last year?"
})
Document Processing and RAG
LangChain excels at building RAG systems:
from langchain_community.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import Chroma
# Load documents
loader = PyPDFLoader("document.pdf")
docs = loader.load()
# Split into chunks
splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200
)
chunks = splitter.split_documents(docs)
# Create vector store
embeddings = OpenAIEmbeddings()
vectorstore = Chroma.from_documents(chunks, embeddings)
# Query
results = vectorstore.similarity_search("What is the main topic?", k=3)
for doc in results:
print(doc.page_content)
Best Practices
- Use Callbacks for Monitoring: Track token usage and latency
- Implement Retry Logic: Handle rate limits gracefully
- Cache Embeddings: Avoid redundant API calls
- Stream Long Responses: Use
.stream()for better UX - Test with Multiple Models: Don't lock into one provider
Conclusion
LangChain abstracts away complexity, enabling rapid development of LLM applications. Whether building chatbots, agents, or RAG systems, LangChain's composable architecture scales from prototypes to production systems.
FAQ
Q: Should I use LangChain or build custom code? A: LangChain is best for rapid prototyping and standard patterns. For highly specific workflows, custom implementations offer more control and can be more efficient.
Q: How do I optimize LangChain for production? A: Use async/await for concurrency, implement caching, monitor costs, and consider switching to lighter frameworks like LLM routing libraries as you mature.
Q: Can LangChain work with local models? A: Yes, through Ollama, Hugging Face, or Replicate integrations. LangChain supports both API-based and self-hosted models.
Advertisement