LangChain is a framework designed to streamline the development of applications powered by large language models (LLMs). It enables developers to compose modular, reusable components that handle prompting, model interaction, data retrieval, memory, and orchestration.
Core Capabilities
- Context Integration: Seamlessly inject external information—such as user input, examples, or retrieved documents—into prompts before model invocation.
- Reasoning Orchestration: Leverage LLMs not just for generation, but for dynamic decision-making, tool selection, and step-by-step task decomposition.
- External Data Connectivity: Bridge LLMs with private or real-time data sources via vector stores, APIs, databases, or file systems.
- Interactive Interfaces: Support streaming responses, synchronous calls, and stateful conversations.
Key Abstraction Layers
Models
- Integrates with diverse backends: OpenAI, Anthropic, Ollama, Hugging Face Transformers, Azure OpenAI, and more.
- Includes mock implementations (
FakeListLLM,FakeStreamingLLM) for testing pipelines without API costs. - Tracks token usage and supports streaming output chunk-by-chunk.
Prompts
- Offers flexible templating via
ChatPromptTemplate,PromptTemplate, and partial variable binding. - Enables prompt versioning, serialization, and composition.
Indexes & Retrieval
- Handles document ingestion: loading (PDF, TXT, DOCX, HTML), splitting (by character, token, or semantic boundary), and embedding.
- Supports vector storage and similarity search using FAISS, Chroma, Qdrant, Milvus, or Weaviate.
Memory
- Maintains conversational state across turns using buffer-based, summary, or entity-aware strategies.
Chains
- Encapsulates sequences of operations: e.g.,
LLMChain,RetrievalQAChain,TransformChain. - Built on the LangChain Expression Language (LCEL), enabling declarative, composable pipelines.
Agents
- Employs LLMs as controllers that dynamically choose tools (e.g., calculator, web search, database query) based on intermediate reasoning.
- Supports ReAct, Plan-and-Execute, and self-reflection patterns.
Callbacks
- Provides hooks for logging, tracing, metrics collection, and custom instrumentation during chain execution.
Environment Setup
Recommended stack:
- OS: Linux or Windows 10/11
- Python: ≥ 3.10
- Optional:
conda,Docker,Ollama,JupyterLab
Install core dependencies:
pip install langchain-community langchain-core langchain-text-splitters
For local inference with Ollama:
# Install Ollama from https://ollama.com
ollama pull qwen2:1.5b # lightweight local model
Launch Jupyter for iterative development:
pip install jupyterlab
jupyter lab
Composing a Basic Chain with LCEL
The LangChain Expression Language (LCEL) simplifies pipeline construction using the | operator. Below is an example that accepts a topic, formats a prompt, invokes a local LLM, and parses the result into a plain string:
from langchain_community.llms import Ollama
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
prompt = ChatPromptTemplate.from_template("Summarize the role of {person} in 'Romance of the Three Kingdoms'.")
llm = Ollama(model="qwen2:1.5b", temperature=0.3)
parser = StrOutputParser()
pipeline = prompt | llm | parser
result = pipeline.invoke({"person": "Cao Cao"})
print(result)
This outputs a concise narrative derived entirely from the model’s internal knowledge—no external context is used.
Enhancing Accuracy with RAG
RAG (Retrieval-Augmented Generation) addresses three key LLM limitations:
- Static Knowledge: Models cannot access post-training data or proprietary content.
- Hallucination Risk: Outputs may appear confident but factually incorrect when unsupported by training data.
- Data Governance: Sensitive documents shouldn’t leave secure infrastructure.
RAG resolves these by retrieving relevant snippets before generation, grounding responses in verifiable sources.
End-to-End RAG Workflow
-
Ingestion:
- Load documents (e.g., HR policies, manuals, reports)
- Split text meaningfully (e.g., by section or paragraph)
- Embed chunks into dense vectors
- Store vectors + metadata in a vector database
-
Query Time:
- Accept user question
- Retrieve top-k semantically similar chunks
- Assemble augmented prompt with context + question
- Invoke LLM to synthesize final answer
Practical Implementation
from langchain_community.document_loaders import TextLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import FAISS
from langchain_community.embeddings import OllamaEmbeddings
from langchain_core.prompts import ChatPromptTemplate
from langchain.chains import ConversationalRetrievalChain
from langchain.memory import ConversationBufferMemory
# Load and split
loader = TextLoader("data/hr_policy.txt", encoding="utf-8")
documents = loader.load()
splitter = RecursiveCharacterTextSplitter(chunk_size=150, chunk_overlap=20)
chunks = splitter.split_documents(documents)
# Embed and index
embedder = OllamaEmbeddings(model="nomic-embed-text")
vectorstore = FAISS.from_documents(chunks, embedder)
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
# Prompt template
template = """Answer based only on the following context. If unsure, say 'I don't know'.\n\nContext:\n{context}\n\nQuestion:\n{question}\n\nAnswer:\n"""
prompt = ChatPromptTemplate.from_template(template)
# Build conversational RAG chain
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
qa_chain = ConversationalRetrievalChain.from_llm(
llm=Ollama(model="qwen2:1.5b"),
retriever=retriever,
memory=memory,
combine_docs_chain_kwargs={"prompt": prompt}
)
response = qa_chain.invoke({"question": "What are the working hours for the Wuxi factory?"})
print(response["answer"])
This implementation retrieves policy excerpts containing time-related clauses, injects them into a structured prompt, and directs the LLM to generate a precise, grounded response—without hallucinating outside the provided context.
Extensibility Options
- Swap embeddings: Try
BGEEmbeddings,HuggingFaceEmbeddings, orFastEmbeddingsfor speed. - Replace vector store: Use
Chromafor simplicity orQdrantfor production-grade filtering and scalability. - Add preprocessing: Apply summarization, entity extraction, or metadata tagging before indexing.
- Introduce reranking: Insert
CohereRerankorCrossEncoderRerankerto improve retrieval relevance. - Enable multi-query expansion: Transform single questions into multiple semantic variants for broader recall.