langchain

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

LangChain - 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
undefined
bash
undefined

Core 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
undefined
pip install langchain-community # 500+种集成 pip install langchain-chroma # 向量存储
undefined

Basic LLM usage

基础LLM使用

python
from langchain_anthropic import ChatAnthropic
python
from langchain_anthropic import ChatAnthropic

Initialize 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)
undefined
response = llm.invoke("用2句话解释量子计算") print(response.content)
undefined

Create an agent (ReAct pattern)

创建智能体(ReAct模式)

python
from langchain.agents import create_agent
from langchain_anthropic import ChatAnthropic
python
from langchain.agents import create_agent
from langchain_anthropic import ChatAnthropic

Define 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)
undefined
result = agent.invoke({"messages": [{"role": "user", "content": "巴黎的天气如何?"}]}) print(result["messages"][-1].content)
undefined

Core concepts

核心概念

1. Models - LLM abstraction

1. 模型 - LLM抽象层

python
from langchain_openai import ChatOpenAI
from langchain_anthropic import ChatAnthropic
from langchain_google_genai import ChatGoogleGenerativeAI
python
from langchain_openai import ChatOpenAI
from langchain_anthropic import ChatAnthropic
from langchain_google_genai import ChatGoogleGenerativeAI

Swap 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)
undefined
for chunk in llm.stream("写一首诗"): print(chunk.content, end="", flush=True)
undefined

2. Chains - Sequential operations

2. 链 - 顺序操作流

python
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
python
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate

Define 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")
undefined
result = chain.run(topic="机器学习")
undefined

3. Agents - Tool-using reasoning

3. 智能体 - 工具调用与推理

ReAct (Reasoning + Acting) pattern:
python
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain.tools import Tool
ReAct(推理+行动)模式:
python
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain.tools import Tool

Define 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?"})
undefined
result = agent_executor.invoke({"input": "25 * 17 + 142等于多少?"})
undefined

4. Memory - Conversation history

4. 内存 - 对话历史管理

python
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationChain
python
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationChain

Add 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"
undefined
conversation.predict(input="你好,我是Alice") conversation.predict(input="我叫什么名字?") # 会记住"Alice"
undefined

RAG (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 RetrievalQA
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 RetrievalQA

1. 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']}")
undefined
result = qa_chain({"query": "什么是Python装饰器?"}) print(result["result"]) print(f"来源:{result['source_documents']}")
undefined

Conversational RAG with memory

带内存的对话式RAG

python
from langchain.chains import ConversationalRetrievalChain
python
from langchain.chains import ConversationalRetrievalChain

RAG 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
undefined
qa({"question": "Python有什么用途?"}) qa({"question": "能否详细说明其在Web开发中的应用?"}) # 会记住上下文
undefined

Advanced agent patterns

高级智能体模式

Structured output

结构化输出

python
from langchain_core.pydantic_v1 import BaseModel, Field
python
from langchain_core.pydantic_v1 import BaseModel, Field

Define 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)
undefined
structured_llm = llm.with_structured_output(WeatherReport) result = structured_llm.invoke("旧金山的天气如何?气温65华氏度,晴朗") print(result.city, result.temperature, result.condition)
undefined

Parallel tool execution

并行工具执行

python
from langchain.agents import create_tool_calling_agent
python
from langchain.agents import create_tool_calling_agent

Agent 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"}] })
undefined
result = agent.invoke({ "messages": [{"role": "user", "content": "对比巴黎和伦敦的天气"}] })
undefined

Streaming agent execution

流式智能体执行

python
undefined
python
undefined

Stream 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']}")
undefined
for 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']}")
undefined

Common patterns

常见模式

Multi-document QA

多文档问答

python
from langchain.chains.qa_with_sources import load_qa_with_sources_chain
python
from langchain.chains.qa_with_sources import load_qa_with_sources_chain

Load 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
undefined
chain = load_qa_with_sources_chain(llm, chain_type="stuff") result = chain({"input_documents": docs, "question": "如何使用numpy数组?"}) print(result["output_text"]) # 包含来源引用
undefined

Custom 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])
undefined
agent = create_agent(model=llm, tools=[risky_operation])
undefined

LangSmith observability

LangSmith可观测性

python
import os
python
import os

Enable 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查看追踪记录

undefined
undefined

Vector 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())
undefined
vectorstore = FAISS.load_local("faiss_index", OpenAIEmbeddings())
undefined

Document loaders

文档加载器

python
undefined
python
undefined

Web 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

PDF

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")
undefined
from langchain_community.document_loaders import CSVLoader loader = CSVLoader("data.csv")
undefined

Text splitters

文本分割器

python
undefined
python
undefined

Recursive (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())
undefined
from langchain_experimental.text_splitter import SemanticChunker splitter = SemanticChunker(OpenAIEmbeddings())
undefined

Best practices

最佳实践

  1. Start simple - Use
    create_agent()
    for most cases
  2. Enable streaming - Better UX for long responses
  3. Add error handling - Tools can fail, handle gracefully
  4. Use LangSmith - Essential for debugging agents
  5. Optimize chunk size - 500-1000 chars for RAG
  6. Version prompts - Track changes in production
  7. Cache embeddings - Expensive, cache when possible
  8. Monitor costs - Track token usage with LangSmith
  1. 从简开始 - 大多数场景使用
    create_agent()
    即可
  2. 启用流式输出 - 长响应时提升用户体验
  3. 添加错误处理 - 工具可能失败,需优雅处理
  4. 使用LangSmith - 调试智能体的必备工具
  5. 优化文本块大小 - RAG场景建议500-1000字符
  6. 版本化提示词 - 生产环境中追踪提示词变更
  7. 缓存嵌入结果 - 嵌入成本较高,尽可能缓存
  8. 监控成本 - 使用LangSmith追踪token消耗

Performance benchmarks

性能基准

OperationLatencyNotes
Simple LLM call~1-2sDepends on provider
Agent with 1 tool~3-5sReAct reasoning overhead
RAG retrieval~0.5-1sVector search + LLM
Embedding 1000 docs~10-30sDepends on model
操作延迟说明
简单LLM调用~1-2秒取决于提供商
单工具智能体~3-5秒ReAct推理带来的额外开销
RAG检索~0.5-1秒向量搜索+LLM调用
1000份文档嵌入~10-30秒取决于模型

LangChain vs LangGraph

LangChain vs LangGraph

FeatureLangChainLangGraph
Best forQuick agents, RAGComplex workflows
Abstraction levelHighLow
Code to start<10 lines~30 lines
ControlSimpleFull control
Stateful workflowsLimitedNative
Cyclic graphsNoYes
Human-in-loopBasicAdvanced
Use LangGraph when:
  • Need stateful workflows with cycles
  • Require fine-grained control
  • Building multi-agent systems
  • Production apps with complex logic
特性LangChainLangGraph
最佳适用场景快速构建智能体、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

资源