概述
LangChain 是构建 LLM 应用的领先框架,提供丰富的抽象和工具。其核心理念是将 LLM 应用分解为可组合的组件:模型、提示词、链、Agent、记忆、工具等。LangChain 支持 100+ LLM 提供商和数千种集成,是 LLM 应用开发的事实标准。LangChain 生态包括 LangSmith(监控)、LangServe(部署)、LangGraph(有状态 Agent)等。
安装
LangChain 通过 pip 安装,按需安装不同的包。核心包提供基础功能,社区包提供第三方集成。建议使用虚拟环境管理依赖。
# Install core packages
pip install langchain
pip install langchain-core
pip install langchain-community
# Install specific integrations
pip install langchain-openai
pip install langchain-anthropic
pip install langchain-google-genai
# Install all dependencies
pip install langchain[all]LLM
LangChain 支持多种 LLM 接口,包括 OpenAI、Anthropic、Google、本地模型等。通过统一接口,可以方便地切换不同模型。支持同步和异步调用、流式输出、批处理等。
from langchain_openai import ChatOpenAI
from langchain_anthropic import ChatAnthropic
# OpenAI
llm = ChatOpenAI(model="gpt-4o", temperature=0.7)
response = llm.invoke("Explain quantum computing")
# Anthropic
llm = ChatAnthropic(model="claude-3-5-sonnet-20241022")
# Streaming output
for chunk in llm.stream("Write a poem"):
print(chunk.content, end="")提示词
LangChain 提供强大的提示词模板系统,支持变量插值、少样本示例、部分格式化等。PromptTemplate 用于文本模型,ChatPromptTemplate 用于聊天模型。支持从文件加载提示词模板。
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages([
("system", "You are a {role} expert."),
("human", "Please explain {topic}."),
])
chain = prompt | llm
response = chain.invoke({"role": "AI", "topic": "deep learning"})链
Chain 是 LangChain 的核心概念,将多个组件按顺序组合。LCEL(LangChain Expression Language)使用管道符 | 组合组件,支持流式、批处理、异步等。链可以嵌套和复用。
from langchain_core.output_parsers import StrOutputParser
# LCEL chain
chain = prompt | llm | StrOutputParser()
result = chain.invoke({"role": "AI", "topic": "RAG"})
# Complex chain
from langchain_core.runnables import RunnablePassthrough
rag_chain = (
{"context": retriever, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)Agent
Agent 是能够自主决策和调用工具的 LLM 应用。LangChain 支持多种 Agent 类型,如 ReAct、OpenAI Functions、Tool Calling 等。Agent 可以根据输入动态选择工具和执行路径。LangGraph 提供更强大的有状态 Agent 构建能力。
from langchain.agents import create_tool_calling_agent, AgentExecutor
tools = [search_tool, calculator_tool]
agent = create_tool_calling_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools)
result = executor.invoke({"input": "What is the size of the AI market in 2024?"})记忆
Memory 让应用保持上下文。LangChain 提供多种记忆类型:ConversationBufferMemory(完整历史)、ConversationSummaryMemory(摘要)、ConversationBufferWindowMemory(窗口)等。支持持久化存储。
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory(
memory_key="chat_history",
return_messages=True
)
chain = prompt | llm | StrOutputParser()
# Use memory in the chain
chain_with_memory = (
RunnablePassthrough.assign(
history=lambda x: memory.load_memory_variables({})["chat_history"]
)
| prompt
| llm
)向量存储
向量存储是 RAG 应用的核心。LangChain 支持 50+ 向量数据库,包括 Chroma、Pinecone、Weaviate、FAISS 等。提供统一的接口用于文档存储和相似度搜索。
from langchain_chroma import Chroma
from langchain_openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings()
vectorstore = Chroma.from_documents(
documents=docs,
embedding=embeddings,
persist_directory="./chroma_db"
)
retriever = vectorstore.as_retriever(search_kwargs={"k": 5})
docs = retriever.invoke("query question")RAG
RAG(检索增强生成)是 LangChain 的重要应用场景。通过检索相关文档,增强 LLM 的回答能力。LangChain 提供完整的 RAG 工具链,包括文档加载、切分、嵌入、检索、生成等。
from langchain_community.document_loaders import WebBaseLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
# Load and split documents
loader = WebBaseLoader("https://example.com")
docs = loader.load()
splitter = RecursiveCharacterTextSplitter(chunk_size=1000)
splits = splitter.split_documents(docs)
# Create vector store
vectorstore = Chroma.from_documents(splits, embeddings)
retriever = vectorstore.as_retriever()
# RAG chain
rag_chain = (
{"context": retriever, "question": RunnablePassthrough()}
| prompt
| llm
)工具
工具扩展 LLM 的能力,使其能执行操作。LangChain 提供丰富的内置工具,如搜索、计算、API 调用等。支持使用 @tool 装饰器或 BaseTool 类进行自定义工具开发。
from langchain_core.tools import tool
@tool
def search_web(query: str) -> str:
"""Search the web for information."""
# Implement search logic
return search_results
@tool
def calculate(expression: str) -> str:
"""Calculate a math expression."""
return str(eval(expression))
tools = [search_web, calculate]回调
回调用于监控和记录 LLM 应用的执行。可以追踪 token 用量、执行时间、错误等。LangSmith 提供强大的监控和调试能力,支持对链执行流的可视化分析。
from langchain_core.callbacks import BaseCallbackHandler
class MyHandler(BaseCallbackHandler):
def on_llm_start(self, serialized, prompts, **kwargs):
print(f"LLM start: {serialized}")
def on_llm_end(self, response, **kwargs):
print(f"LLM end: {response}")
chain.invoke("question", config={"callbacks": [MyHandler()]})部署
LangChain 应用可通过 LangServe 部署为 REST API。LangServe 提供 REST 接口、Playground、流式支持等。也支持部署到 AWS、GCP、Vercel 等云平台。LangSmith 提供生产监控能力。
# Deploy with LangServe
from langserve import add_routes
from fastapi import FastAPI
app = FastAPI()
add_routes(app, chain, path="/chat")
# Run: uvicorn server:app --reload
# Access: http://localhost:8000/chat/playgroundLangServe gives every chain a REST API plus a Playground UI for free.
Configuration
LangChain is configured in Python code with provider-specific packages. Install langchain-openai, langchain-anthropic, etc. and set API keys as environment variables. LCEL chains compose with the pipe | operator. LangSmith (set LANGCHAIN_API_KEY) provides tracing and monitoring. Vector store and embedding choices are made per application.
# .env
OPENAI_API_KEY=your-key
ANTHROPIC_API_KEY=your-key
LANGCHAIN_API_KEY=your-key # for LangSmith tracing
LANGCHAIN_TRACING_V2=true
# Python: pick a model
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o", temperature=0.7)
# LCEL chain
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
chain = prompt | llm | StrOutputParser()
# Vector store
from langchain_chroma import Chroma
vectorstore = Chroma(embedding_function=embeddings,
persist_directory="./chroma_db")Enable LangSmith tracing early (LANGCHAIN_TRACING_V2=true)—it makes chain debugging dramatically easier.
FAQ
Common questions cover model switching, cost, vector store choice, LangSmith vs LangServe, and LCEL. LangChain's unified interface lets you swap models by changing one line. LangSmith is for monitoring/tracing; LangServe is for deploying chains as APIs.
Q: How do I switch models?
A: Change the model class (ChatOpenAI -> ChatAnthropic) and the env var;
LCEL chains work unchanged.
Q: How do I cut costs?
A: Use gpt-4o-mini for simple tasks, cache responses, and trim context
before passing to the LLM.
Q: Which vector store should I use?
A: Chroma for local dev, Pinecone/Weaviate for production, FAISS for
in-memory experiments.
Q: LangSmith vs LangServe?
A: LangSmith monitors and traces LLM calls; LangServe deploys chains as
REST APIs. They are complementary.
Q: What is LCEL?
A: LangChain Expression Language—compose chains with the | pipe operator,
getting streaming, async, and batch for free.Prefer LCEL (the | pipe syntax) over legacy chains; it gives streaming, async, and batch support for free.
Ready to try LangChain?
Visit the official site for the latest version and full documentation.
Visit LangChain