LangChain
LangChain is a framework designed to simplify the development of applications that use Large Language Models (LLMs). It provides tools to integrate LLMs with external data sources, memory, retrieval-augmented generation (RAG), agents, and chains of operations.
1. What is LangChain?
LangChain enables developers to build powerful applications that leverage LLMs like OpenAI’s GPT, Cohere, Mistral, DeepSeek, and more.
Why Use LangChain?
- Simplifies LLM integration with APIs, documents, and databases.
- Provides modular components for easy prototyping and scaling.
- Supports memory and state management for multi-turn conversations.
- Built-in retrieval and knowledge augmentation for reducing hallucinations.
- Enables reasoning and action using LLM Agents.
2. Core Components of LangChain
LangChain consists of six primary components:
Component | Description |
---|---|
LLMs | Interface for calling language models (GPT-4, LLaMA, Claude, etc.) |
Chains | Sequences of operations that process inputs and outputs |
Agents | AI decision-making system that dynamically selects tools |
Memory | Stores conversation history and user state |
Retrieval | Enables RAG with vector databases |
Tools & Utilities | Functions like Google Search, APIs, or code execution |
3. Setting Up LangChain
Install LangChain and Dependencies
pip install langchain openai chromadb faiss-cpu tiktoken
Set Up API Keys (Example: OpenAI)
import os
"OPENAI_API_KEY"] = "your-api-key" os.environ[
4. Using LLMs in LangChain
Basic Example with OpenAI’s GPT-4
from langchain.chat_models import ChatOpenAI
= ChatOpenAI(model_name="gpt-4", temperature=0.7)
llm = llm.predict("What is LangChain?")
response print(response)
Using LLaMA or Other Local Models
from langchain.llms import Ollama
= Ollama(model="llama2")
llm = llm("Tell me about AI applications.")
response print(response)
5. Chains: Creating Pipelines
A Chain is a sequence of operations (e.g., input → LLM → output).
Basic LLM Chain
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
= PromptTemplate.from_template("What are the top 3 applications of {technology}?")
prompt = LLMChain(llm=llm, prompt=prompt)
chain
= chain.run(technology="Machine Learning")
response print(response)
Sequential Chains (Multiple Steps)
from langchain.chains import SimpleSequentialChain
= LLMChain(llm=llm, prompt=PromptTemplate.from_template("Explain {concept}"))
chain1 = LLMChain(llm=llm, prompt=PromptTemplate.from_template("Summarize in one line: {text}"))
chain2
= SimpleSequentialChain(chains=[chain1, chain2])
pipeline = pipeline.run("Quantum Computing")
response print(response)
6. Agents: Dynamic AI with Tool Use
Agents allow an LLM to choose tools dynamically rather than following a fixed flow.
Basic Agent with OpenAI Functions
from langchain.agents import AgentType, initialize_agent
from langchain.tools import Tool
from langchain.chat_models import ChatOpenAI
def search_tool(query):
return f"Searching for {query} on the web..."
= [Tool(name="WebSearch", func=search_tool, description="Searches the web")]
tools
= initialize_agent(
agent =tools,
tools=ChatOpenAI(model="gpt-4"),
llm=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
agent=True
verbose
)
= agent.run("What is the latest AI research?")
response print(response)
Self-Reflecting AI Agent
from langchain.agents import OpenAIFunctionsAgent
from langchain.agents.agent import AgentExecutor
= OpenAIFunctionsAgent.from_llm(llm=ChatOpenAI(model="gpt-4"))
agent = AgentExecutor(agent=agent, tools=tools)
executor
= executor.invoke("Find the latest AI papers.")
response print(response)
7. Memory and State Management
By default, LLMs do not remember past interactions. Memory in LangChain enables persistence.
Adding Memory to Conversations
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationChain
= ConversationBufferMemory()
memory = ConversationChain(llm=llm, memory=memory)
conversation
print(conversation.run("Hello, who are you?"))
print(conversation.run("Can you summarize our conversation?"))
Different Memory Types
Memory Type | Use Case |
---|---|
ConversationBufferMemory | Stores complete conversation history |
ConversationSummaryMemory | Summarizes past interactions |
VectorStoreRetrieverMemory | Uses embeddings to store long-term memory |
8. Retrieval-Augmented Generation (RAG)
RAG combines vector search with LLMs to reduce hallucinations.
Loading a Document and Storing Embeddings
from langchain.document_loaders import TextLoader
from langchain.vectorstores import FAISS
from langchain.embeddings import OpenAIEmbeddings
# Load and split documents
= TextLoader("docs.txt")
loader = loader.load()
documents
# Store as vector embeddings
= OpenAIEmbeddings()
embeddings = FAISS.from_documents(documents, embeddings) vectorstore
Retrieving and Passing to LLM
= vectorstore.as_retriever()
retriever = retriever.get_relevant_documents("What is AI?") retrieved_docs
9. LangChain Tools and Integrations
LangChain connects with APIs, databases, and search engines.
Google Search API
from langchain.tools import Tool
from langchain.utilities import GoogleSearchAPIWrapper
= GoogleSearchAPIWrapper()
search = Tool(name="Google Search", func=search.run)
tool
= tool.run("Latest AI news")
result print(result)
Code Execution
from langchain.tools import PythonREPLTool
= PythonREPLTool()
python_tool print(python_tool.run("import math; math.sqrt(16)"))
10. Scaling and Optimization
Optimizing Token Usage
import tiktoken
= tiktoken.get_encoding("cl100k_base")
encoder print(len(encoder.encode("This is a test message."))) # Token count
Using Local LLMs Instead of API Calls
For privacy or cost efficiency, use models like LLaMA or Mistral:
from langchain.llms import Ollama
= Ollama(model="mistral") local_llm
Batch Processing for Efficiency
= llm.batch(["Explain AI", "Define RAG", "What is LangChain?"])
responses print(responses)
11. Example Architectures and Use Cases
Use Case | LangChain Components Used |
---|---|
AI-Powered Chatbots | Memory, Chains, LLMs |
Retrieval-Augmented QA Systems | Retrieval, Vector Stores, LLMs |
AI Agents for Research | Agents, Tools, API Integrations |
Automated Code Generation | LLMs, Python Execution Tools |
Personalized Assistants | Memory, State Tracking |
Final Thoughts
- LangChain simplifies LLM development by abstracting complex workflows.
- It integrates retrieval, memory, and agents for dynamic applications.
- It supports local and cloud-based LLMs, ensuring flexibility.
- Optimizing context size, retrieval methods, and caching improves efficiency.
LangChain is the foundation for building advanced, production-ready AI applications with modular, scalable components.