langchain
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseLangChain - Build LLM Applications with Agents & RAG
LangChain - 基于Agents与RAG构建LLM应用程序
The most popular framework for building LLM-powered applications.
一款最受欢迎的LLM驱动应用程序构建框架。
When to use LangChain
何时使用LangChain
Use LangChain when:
- Building agents with tool calling and reasoning (ReAct pattern)
- Implementing RAG (retrieval-augmented generation) pipelines
- Need to swap LLM providers easily (OpenAI, Anthropic, Google)
- Creating chatbots with conversation memory
- Rapid prototyping of LLM applications
- Production deployments with LangSmith observability
Metrics:
- 119,000+ GitHub stars
- 272,000+ repositories use LangChain
- 500+ integrations (models, vector stores, tools)
- 3,800+ contributors
Use alternatives instead:
- LlamaIndex: RAG-focused, better for document Q&A
- LangGraph: Complex stateful workflows, more control
- Haystack: Production search pipelines
- Semantic Kernel: Microsoft ecosystem
在以下场景使用LangChain:
- 构建具备工具调用与推理能力的智能体(ReAct模式)
- 实现RAG(检索增强生成)管道
- 需要轻松切换LLM提供商(OpenAI、Anthropic、Google)
- 创建带对话记忆功能的聊天机器人
- LLM应用程序的快速原型开发
- 结合LangSmith可观测性的生产部署
相关数据:
- 119,000+ GitHub星标
- 272,000+个仓库使用LangChain
- 500+种集成(模型、向量存储、工具)
- 3,800+贡献者
可选择的替代方案:
- LlamaIndex: 专注RAG,更适合文档问答场景
- LangGraph: 处理复杂有状态工作流,提供更多控制权
- Haystack: 生产级搜索管道
- Semantic Kernel: 微软生态系统适配
Quick start
快速开始
Installation
安装
bash
undefinedbash
undefinedCore library (Python 3.10+)
核心库(Python 3.10+)
pip install -U langchain
pip install -U langchain
With OpenAI
搭配OpenAI使用
pip install langchain-openai
pip install langchain-openai
With Anthropic
搭配Anthropic使用
pip install langchain-anthropic
pip install langchain-anthropic
Common extras
常用扩展
pip install langchain-community # 500+ integrations
pip install langchain-chroma # Vector store
undefinedpip install langchain-community # 500+种集成
pip install langchain-chroma # 向量存储
undefinedBasic LLM usage
基础LLM使用
python
from langchain_anthropic import ChatAnthropicpython
from langchain_anthropic import ChatAnthropicInitialize model
初始化模型
llm = ChatAnthropic(model="claude-sonnet-4-5-20250929")
llm = ChatAnthropic(model="claude-sonnet-4-5-20250929")
Simple completion
简单补全
response = llm.invoke("Explain quantum computing in 2 sentences")
print(response.content)
undefinedresponse = llm.invoke("用2句话解释量子计算")
print(response.content)
undefinedCreate an agent (ReAct pattern)
创建智能体(ReAct模式)
python
from langchain.agents import create_agent
from langchain_anthropic import ChatAnthropicpython
from langchain.agents import create_agent
from langchain_anthropic import ChatAnthropicDefine tools
定义工具
def get_weather(city: str) -> str:
"""Get current weather for a city."""
return f"It's sunny in {city}, 72°F"
def search_web(query: str) -> str:
"""Search the web for information."""
return f"Search results for: {query}"
def get_weather(city: str) -> str:
"""获取指定城市的当前天气。"""
return f"{city}当前天气晴朗,气温72°F"
def search_web(query: str) -> str:
"""在网络上搜索信息。"""
return f"搜索结果:{query}"
Create agent (<10 lines!)
创建智能体(仅需不到10行代码!)
agent = create_agent(
model=ChatAnthropic(model="claude-sonnet-4-5-20250929"),
tools=[get_weather, search_web],
system_prompt="You are a helpful assistant. Use tools when needed."
)
agent = create_agent(
model=ChatAnthropic(model="claude-sonnet-4-5-20250929"),
tools=[get_weather, search_web],
system_prompt="你是一个乐于助人的助手,必要时使用工具。"
)
Run agent
运行智能体
result = agent.invoke({"messages": [{"role": "user", "content": "What's the weather in Paris?"}]})
print(result["messages"][-1].content)
undefinedresult = agent.invoke({"messages": [{"role": "user", "content": "巴黎的天气如何?"}]})
print(result["messages"][-1].content)
undefinedCore concepts
核心概念
1. Models - LLM abstraction
1. 模型 - LLM抽象层
python
from langchain_openai import ChatOpenAI
from langchain_anthropic import ChatAnthropic
from langchain_google_genai import ChatGoogleGenerativeAIpython
from langchain_openai import ChatOpenAI
from langchain_anthropic import ChatAnthropic
from langchain_google_genai import ChatGoogleGenerativeAISwap providers easily
轻松切换提供商
llm = ChatOpenAI(model="gpt-4o")
llm = ChatAnthropic(model="claude-sonnet-4-5-20250929")
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash-exp")
llm = ChatOpenAI(model="gpt-4o")
llm = ChatAnthropic(model="claude-sonnet-4-5-20250929")
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash-exp")
Streaming
流式输出
for chunk in llm.stream("Write a poem"):
print(chunk.content, end="", flush=True)
undefinedfor chunk in llm.stream("写一首诗"):
print(chunk.content, end="", flush=True)
undefined2. Chains - Sequential operations
2. 链 - 顺序操作流
python
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplatepython
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplateDefine prompt template
定义提示词模板
prompt = PromptTemplate(
input_variables=["topic"],
template="Write a 3-sentence summary about {topic}"
)
prompt = PromptTemplate(
input_variables=["topic"],
template="写一段关于{topic}的3句话摘要"
)
Create chain
创建链
chain = LLMChain(llm=llm, prompt=prompt)
chain = LLMChain(llm=llm, prompt=prompt)
Run chain
运行链
result = chain.run(topic="machine learning")
undefinedresult = chain.run(topic="机器学习")
undefined3. Agents - Tool-using reasoning
3. 智能体 - 工具调用与推理
ReAct (Reasoning + Acting) pattern:
python
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain.tools import ToolReAct(推理+行动)模式:
python
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain.tools import ToolDefine custom tool
定义自定义工具
calculator = Tool(
name="Calculator",
func=lambda x: eval(x),
description="Useful for math calculations. Input: valid Python expression."
)
calculator = Tool(
name="Calculator",
func=lambda x: eval(x),
description="适用于数学计算。输入:有效的Python表达式。"
)
Create agent with tools
创建带工具的智能体
agent = create_tool_calling_agent(
llm=llm,
tools=[calculator, search_web],
prompt="Answer questions using available tools"
)
agent = create_tool_calling_agent(
llm=llm,
tools=[calculator, search_web],
prompt="使用可用工具回答问题"
)
Create executor
创建执行器
agent_executor = AgentExecutor(agent=agent, tools=[calculator], verbose=True)
agent_executor = AgentExecutor(agent=agent, tools=[calculator], verbose=True)
Run with reasoning
带推理的运行
result = agent_executor.invoke({"input": "What is 25 * 17 + 142?"})
undefinedresult = agent_executor.invoke({"input": "25 * 17 + 142等于多少?"})
undefined4. Memory - Conversation history
4. 内存 - 对话历史管理
python
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationChainpython
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationChainAdd memory to track conversation
添加内存以跟踪对话
memory = ConversationBufferMemory()
conversation = ConversationChain(
llm=llm,
memory=memory,
verbose=True
)
memory = ConversationBufferMemory()
conversation = ConversationChain(
llm=llm,
memory=memory,
verbose=True
)
Multi-turn conversation
多轮对话
conversation.predict(input="Hi, I'm Alice")
conversation.predict(input="What's my name?") # Remembers "Alice"
undefinedconversation.predict(input="你好,我是Alice")
conversation.predict(input="我叫什么名字?") # 会记住"Alice"
undefinedRAG (Retrieval-Augmented Generation)
RAG(检索增强生成)
Basic RAG pipeline
基础RAG管道
python
from langchain_community.document_loaders import WebBaseLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
from langchain_chroma import Chroma
from langchain.chains import RetrievalQApython
from langchain_community.document_loaders import WebBaseLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
from langchain_chroma import Chroma
from langchain.chains import RetrievalQA1. Load documents
1. 加载文档
loader = WebBaseLoader("https://docs.python.org/3/tutorial/")
docs = loader.load()
loader = WebBaseLoader("https://docs.python.org/3/tutorial/")
docs = loader.load()
2. Split into chunks
2. 分割为文本块
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200
)
splits = text_splitter.split_documents(docs)
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200
)
splits = text_splitter.split_documents(docs)
3. Create embeddings and vector store
3. 创建嵌入与向量存储
vectorstore = Chroma.from_documents(
documents=splits,
embedding=OpenAIEmbeddings()
)
vectorstore = Chroma.from_documents(
documents=splits,
embedding=OpenAIEmbeddings()
)
4. Create retriever
4. 创建检索器
retriever = vectorstore.as_retriever(search_kwargs={"k": 4})
retriever = vectorstore.as_retriever(search_kwargs={"k": 4})
5. Create QA chain
5. 创建问答链
qa_chain = RetrievalQA.from_chain_type(
llm=llm,
retriever=retriever,
return_source_documents=True
)
qa_chain = RetrievalQA.from_chain_type(
llm=llm,
retriever=retriever,
return_source_documents=True
)
6. Query
6. 查询
result = qa_chain({"query": "What are Python decorators?"})
print(result["result"])
print(f"Sources: {result['source_documents']}")
undefinedresult = qa_chain({"query": "什么是Python装饰器?"})
print(result["result"])
print(f"来源:{result['source_documents']}")
undefinedConversational RAG with memory
带内存的对话式RAG
python
from langchain.chains import ConversationalRetrievalChainpython
from langchain.chains import ConversationalRetrievalChainRAG with conversation memory
带对话内存的RAG
qa = ConversationalRetrievalChain.from_llm(
llm=llm,
retriever=retriever,
memory=ConversationBufferMemory(
memory_key="chat_history",
return_messages=True
)
)
qa = ConversationalRetrievalChain.from_llm(
llm=llm,
retriever=retriever,
memory=ConversationBufferMemory(
memory_key="chat_history",
return_messages=True
)
)
Multi-turn RAG
多轮RAG对话
qa({"question": "What is Python used for?"})
qa({"question": "Can you elaborate on web development?"}) # Remembers context
undefinedqa({"question": "Python有什么用途?"})
qa({"question": "能否详细说明其在Web开发中的应用?"}) # 会记住上下文
undefinedAdvanced agent patterns
高级智能体模式
Structured output
结构化输出
python
from langchain_core.pydantic_v1 import BaseModel, Fieldpython
from langchain_core.pydantic_v1 import BaseModel, FieldDefine schema
定义 schema
class WeatherReport(BaseModel):
city: str = Field(description="City name")
temperature: float = Field(description="Temperature in Fahrenheit")
condition: str = Field(description="Weather condition")
class WeatherReport(BaseModel):
city: str = Field(description="城市名称")
temperature: float = Field(description="华氏温度")
condition: str = Field(description="天气状况")
Get structured response
获取结构化响应
structured_llm = llm.with_structured_output(WeatherReport)
result = structured_llm.invoke("What's the weather in SF? It's 65F and sunny")
print(result.city, result.temperature, result.condition)
undefinedstructured_llm = llm.with_structured_output(WeatherReport)
result = structured_llm.invoke("旧金山的天气如何?气温65华氏度,晴朗")
print(result.city, result.temperature, result.condition)
undefinedParallel tool execution
并行工具执行
python
from langchain.agents import create_tool_calling_agentpython
from langchain.agents import create_tool_calling_agentAgent automatically parallelizes independent tool calls
智能体自动并行执行独立工具调用
agent = create_tool_calling_agent(
llm=llm,
tools=[get_weather, search_web, calculator]
)
agent = create_tool_calling_agent(
llm=llm,
tools=[get_weather, search_web, calculator]
)
This will call get_weather("Paris") and get_weather("London") in parallel
此请求会并行调用get_weather("Paris")和get_weather("London")
result = agent.invoke({
"messages": [{"role": "user", "content": "Compare weather in Paris and London"}]
})
undefinedresult = agent.invoke({
"messages": [{"role": "user", "content": "对比巴黎和伦敦的天气"}]
})
undefinedStreaming agent execution
流式智能体执行
python
undefinedpython
undefinedStream agent steps
流式输出智能体步骤
for step in agent_executor.stream({"input": "Research AI trends"}):
if "actions" in step:
print(f"Tool: {step['actions'][0].tool}")
if "output" in step:
print(f"Output: {step['output']}")
undefinedfor step in agent_executor.stream({"input": "研究AI发展趋势"}):
if "actions" in step:
print(f"工具:{step['actions'][0].tool}")
if "output" in step:
print(f"输出:{step['output']}")
undefinedCommon patterns
常见模式
Multi-document QA
多文档问答
python
from langchain.chains.qa_with_sources import load_qa_with_sources_chainpython
from langchain.chains.qa_with_sources import load_qa_with_sources_chainLoad multiple documents
加载多个文档
docs = [
loader.load("https://docs.python.org"),
loader.load("https://docs.numpy.org")
]
docs = [
loader.load("https://docs.python.org"),
loader.load("https://docs.numpy.org")
]
QA with source citations
带来源引用的问答
chain = load_qa_with_sources_chain(llm, chain_type="stuff")
result = chain({"input_documents": docs, "question": "How to use numpy arrays?"})
print(result["output_text"]) # Includes source citations
undefinedchain = load_qa_with_sources_chain(llm, chain_type="stuff")
result = chain({"input_documents": docs, "question": "如何使用numpy数组?"})
print(result["output_text"]) # 包含来源引用
undefinedCustom tools with error handling
带错误处理的自定义工具
python
from langchain.tools import tool
@tool
def risky_operation(query: str) -> str:
"""Perform a risky operation that might fail."""
try:
# Your operation here
result = perform_operation(query)
return f"Success: {result}"
except Exception as e:
return f"Error: {str(e)}"python
from langchain.tools import tool
@tool
def risky_operation(query: str) -> str:
"""执行可能失败的高风险操作。"""
try:
# 你的操作逻辑
result = perform_operation(query)
return f"成功:{result}"
except Exception as e:
return f"错误:{str(e)}"Agent handles errors gracefully
智能体可优雅处理错误
agent = create_agent(model=llm, tools=[risky_operation])
undefinedagent = create_agent(model=llm, tools=[risky_operation])
undefinedLangSmith observability
LangSmith可观测性
python
import ospython
import osEnable tracing
启用追踪
os.environ["LANGCHAIN_TRACING_V2"] = "true"
os.environ["LANGCHAIN_API_KEY"] = "your-api-key"
os.environ["LANGCHAIN_PROJECT"] = "my-project"
os.environ["LANGCHAIN_TRACING_V2"] = "true"
os.environ["LANGCHAIN_API_KEY"] = "your-api-key"
os.environ["LANGCHAIN_PROJECT"] = "my-project"
All chains/agents automatically traced
所有链/智能体自动被追踪
agent = create_agent(model=llm, tools=[calculator])
result = agent.invoke({"input": "Calculate 123 * 456"})
agent = create_agent(model=llm, tools=[calculator])
result = agent.invoke({"input": "计算123 * 456"})
View traces at smith.langchain.com
在smith.langchain.com查看追踪记录
undefinedundefinedVector stores
向量存储
Chroma (local)
Chroma(本地)
python
from langchain_chroma import Chroma
vectorstore = Chroma.from_documents(
documents=docs,
embedding=OpenAIEmbeddings(),
persist_directory="./chroma_db"
)python
from langchain_chroma import Chroma
vectorstore = Chroma.from_documents(
documents=docs,
embedding=OpenAIEmbeddings(),
persist_directory="./chroma_db"
)Pinecone (cloud)
Pinecone(云端)
python
from langchain_pinecone import PineconeVectorStore
vectorstore = PineconeVectorStore.from_documents(
documents=docs,
embedding=OpenAIEmbeddings(),
index_name="my-index"
)python
from langchain_pinecone import PineconeVectorStore
vectorstore = PineconeVectorStore.from_documents(
documents=docs,
embedding=OpenAIEmbeddings(),
index_name="my-index"
)FAISS (similarity search)
FAISS(相似度搜索)
python
from langchain_community.vectorstores import FAISS
vectorstore = FAISS.from_documents(docs, OpenAIEmbeddings())
vectorstore.save_local("faiss_index")python
from langchain_community.vectorstores import FAISS
vectorstore = FAISS.from_documents(docs, OpenAIEmbeddings())
vectorstore.save_local("faiss_index")Load later
后续加载
vectorstore = FAISS.load_local("faiss_index", OpenAIEmbeddings())
undefinedvectorstore = FAISS.load_local("faiss_index", OpenAIEmbeddings())
undefinedDocument loaders
文档加载器
python
undefinedpython
undefinedWeb pages
网页
from langchain_community.document_loaders import WebBaseLoader
loader = WebBaseLoader("https://example.com")
from langchain_community.document_loaders import WebBaseLoader
loader = WebBaseLoader("https://example.com")
PDFs
from langchain_community.document_loaders import PyPDFLoader
loader = PyPDFLoader("paper.pdf")
from langchain_community.document_loaders import PyPDFLoader
loader = PyPDFLoader("paper.pdf")
GitHub
GitHub
from langchain_community.document_loaders import GithubFileLoader
loader = GithubFileLoader(repo="user/repo", file_filter=lambda x: x.endswith(".py"))
from langchain_community.document_loaders import GithubFileLoader
loader = GithubFileLoader(repo="user/repo", file_filter=lambda x: x.endswith(".py"))
CSV
CSV
from langchain_community.document_loaders import CSVLoader
loader = CSVLoader("data.csv")
undefinedfrom langchain_community.document_loaders import CSVLoader
loader = CSVLoader("data.csv")
undefinedText splitters
文本分割器
python
undefinedpython
undefinedRecursive (recommended for general text)
递归分割(推荐用于通用文本)
from langchain.text_splitter import RecursiveCharacterTextSplitter
splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200,
separators=["\n\n", "\n", " ", ""]
)
from langchain.text_splitter import RecursiveCharacterTextSplitter
splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200,
separators=["\n\n", "\n", " ", ""]
)
Code-aware
代码感知分割
from langchain.text_splitter import PythonCodeTextSplitter
splitter = PythonCodeTextSplitter(chunk_size=500)
from langchain.text_splitter import PythonCodeTextSplitter
splitter = PythonCodeTextSplitter(chunk_size=500)
Semantic (by meaning)
语义分割(按含义分割)
from langchain_experimental.text_splitter import SemanticChunker
splitter = SemanticChunker(OpenAIEmbeddings())
undefinedfrom langchain_experimental.text_splitter import SemanticChunker
splitter = SemanticChunker(OpenAIEmbeddings())
undefinedBest practices
最佳实践
- Start simple - Use for most cases
create_agent() - Enable streaming - Better UX for long responses
- Add error handling - Tools can fail, handle gracefully
- Use LangSmith - Essential for debugging agents
- Optimize chunk size - 500-1000 chars for RAG
- Version prompts - Track changes in production
- Cache embeddings - Expensive, cache when possible
- Monitor costs - Track token usage with LangSmith
- 从简开始 - 大多数场景使用即可
create_agent() - 启用流式输出 - 长响应时提升用户体验
- 添加错误处理 - 工具可能失败,需优雅处理
- 使用LangSmith - 调试智能体的必备工具
- 优化文本块大小 - RAG场景建议500-1000字符
- 版本化提示词 - 生产环境中追踪提示词变更
- 缓存嵌入结果 - 嵌入成本较高,尽可能缓存
- 监控成本 - 使用LangSmith追踪token消耗
Performance benchmarks
性能基准
| Operation | Latency | Notes |
|---|---|---|
| Simple LLM call | ~1-2s | Depends on provider |
| Agent with 1 tool | ~3-5s | ReAct reasoning overhead |
| RAG retrieval | ~0.5-1s | Vector search + LLM |
| Embedding 1000 docs | ~10-30s | Depends on model |
| 操作 | 延迟 | 说明 |
|---|---|---|
| 简单LLM调用 | ~1-2秒 | 取决于提供商 |
| 单工具智能体 | ~3-5秒 | ReAct推理带来的额外开销 |
| RAG检索 | ~0.5-1秒 | 向量搜索+LLM调用 |
| 1000份文档嵌入 | ~10-30秒 | 取决于模型 |
LangChain vs LangGraph
LangChain vs LangGraph
| Feature | LangChain | LangGraph |
|---|---|---|
| Best for | Quick agents, RAG | Complex workflows |
| Abstraction level | High | Low |
| Code to start | <10 lines | ~30 lines |
| Control | Simple | Full control |
| Stateful workflows | Limited | Native |
| Cyclic graphs | No | Yes |
| Human-in-loop | Basic | Advanced |
Use LangGraph when:
- Need stateful workflows with cycles
- Require fine-grained control
- Building multi-agent systems
- Production apps with complex logic
| 特性 | LangChain | LangGraph |
|---|---|---|
| 最佳适用场景 | 快速构建智能体、RAG | 复杂工作流 |
| 抽象层级 | 高 | 低 |
| 启动代码量 | <10行 | ~30行 |
| 控制权 | 简单易用 | 完全可控 |
| 有状态工作流 | 有限支持 | 原生支持 |
| 循环图 | 不支持 | 支持 |
| 人工介入 | 基础支持 | 高级支持 |
使用LangGraph的场景:
- 需要带循环的有状态工作流
- 需细粒度控制
- 构建多智能体系统
- 包含复杂逻辑的生产级应用
References
参考资料
- Agents Guide - ReAct, tool calling, streaming
- RAG Guide - Document loaders, retrievers, QA chains
- Integration Guide - Vector stores, LangSmith, deployment
- 智能体指南 - ReAct、工具调用、流式输出
- RAG指南 - 文档加载器、检索器、问答链
- 集成指南 - 向量存储、LangSmith、部署
Resources
资源
- GitHub: https://github.com/langchain-ai/langchain ⭐ 119,000+
- Docs: https://docs.langchain.com
- API Reference: https://reference.langchain.com/python
- LangSmith: https://smith.langchain.com (observability)
- Version: 0.3+ (stable)
- License: MIT
- GitHub: https://github.com/langchain-ai/langchain ⭐ 119,000+
- 官方文档: https://docs.langchain.com
- API参考: https://reference.langchain.com/python
- LangSmith: https://smith.langchain.com(可观测性平台)
- 版本: 0.3+(稳定版)
- 许可证: MIT