LangChain: A Comprehensive Guide
LangChain is a powerful framework that simplifies working with Large Language Models (LLMs) by providing composable components for building applications such as chatbots, autonomous agents, document processing, and more.
1. What is LangChain?
LangChain is an open-source Python and JavaScript framework designed to orchestrate LLM workflows efficiently. It provides modular components to help with:
- Prompt management (creating reusable, structured prompts)
- Memory handling (persisting conversation history)
- Data retrieval (connecting to external sources)
- Agent-based decision making (using LLMs to interact with APIs)
- Tool integrations (connecting to APIs, databases, and web search)
Why Use LangChain?
- Standardized Components: Easily build and scale LLM-powered applications.
- Extensibility: Supports OpenAI, Hugging Face, local models, and vector databases.
- Abstraction: Simplifies LLM interactions with structured chaining.
2. Installation
LangChain can be installed via pip:
pip install langchain openai
You may also need:
pip install chromadb tiktoken pydantic transformers
3. Key Concepts
1. LLMs (Language Models)
LangChain integrates with various models, including:
- OpenAI’s GPT models (
openai
) - Hugging Face models (
transformers
) - Local LLMs (
llama.cpp
,ollama
)
Example usage:
from langchain.llms import OpenAI
llm = OpenAI(model_name="gpt-3.5-turbo", openai_api_key="your-api-key")
response = llm("Explain LangChain in simple terms.")
print(response)
2. Prompts
Prompts define structured inputs for LLMs.
Basic PromptTemplate
from langchain.prompts import PromptTemplate
template = PromptTemplate(
input_variables=["topic"],
template="Tell me a short fact about {topic}."
)
prompt = template.format(topic="Python programming")
print(prompt)
3. Chains
Chains allow multiple steps to be linked together.
Simple LLMChain
from langchain.chains import LLMChain
chain = LLMChain(llm=llm, prompt=template)
response = chain.run("Artificial Intelligence")
print(response)
Sequential Chains
from langchain.chains import SimpleSequentialChain
chain1 = LLMChain(llm=llm, prompt=PromptTemplate(
input_variables=["question"], template="Explain {question} briefly."
))
chain2 = LLMChain(llm=llm, prompt=PromptTemplate(
input_variables=["explanation"], template="Give a real-world example: {explanation}."
))
seq_chain = SimpleSequentialChain(chains=[chain1, chain2])
print(seq_chain.run("Quantum Computing"))
4. Memory (Stateful Interactions)
To maintain conversation history:
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
memory.save_context({"input": "Who is Albert Einstein?"}, {"output": "A physicist who developed the theory of relativity."})
print(memory.load_memory_variables({}))
5. Tools & Agents
Agents allow LLMs to use external tools like APIs and databases.
Example: Agent using Web Search
from langchain.agents import load_tools, initialize_agent
from langchain.llms import OpenAI
llm = OpenAI(model_name="gpt-4", openai_api_key="your-api-key")
tools = load_tools(["serpapi"], serpapi_api_key="your-serpapi-key")
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
response = agent.run("What's the latest tech news?")
print(response)
6. Vector Stores & Retrieval (RAG)
To retrieve information from documents, LangChain supports:
- ChromaDB (lightweight, local vector database)
- Pinecone (scalable, cloud vector search)
- FAISS (Facebook AI Similarity Search)
Example: Loading Documents and Storing in a Vector Database
from langchain.document_loaders import TextLoader
from langchain.vectorstores import Chroma
from langchain.embeddings.openai import OpenAIEmbeddings
loader = TextLoader("sample.txt")
docs = loader.load()
vectorstore = Chroma.from_documents(docs, OpenAIEmbeddings())
retriever = vectorstore.as_retriever()
results = retriever.get_relevant_documents("What is LangChain?")
print(results)
4. Advanced Use Cases
1. Building a Chatbot
from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationalRetrievalChain
chat = ChatOpenAI(model_name="gpt-4", openai_api_key="your-api-key")
chat_chain = ConversationalRetrievalChain.from_llm(
llm=chat, retriever=retriever, memory=memory
)
response = chat_chain.run("How does LangChain help developers?")
print(response)
2. Using Local LLMs (e.g., Llama, Mistral)
from langchain.llms import Ollama
local_llm = Ollama(model="mistral") # Uses ollama to run models like LLaMA locally
response = local_llm("Explain LangChain.")
print(response)
5. Alternatives to LangChain
If LangChain feels too heavyweight, consider:
- LlamaIndex (for RAG applications)
- Haystack (for document-based AI search)
- OpenAI function calling (if using only OpenAI API)
6. Summary
Feature | Description |
---|---|
LLMs | Works with OpenAI, Hugging Face, local models |
Prompts | Helps structure queries effectively |
Chains | Sequences multiple steps together |
Memory | Stores conversation history |
Agents | Uses external tools dynamically |
Vector Stores | Enables document-based retrieval |
LangChain is ideal for rapid prototyping and LLM-based applications, providing modular, scalable, and extensible workflows.