langchain-agents

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

LangChain - LLM Applications with Agents & RAG

LangChain - 基于Agents与RAG的LLM应用开发

The most popular framework for building LLM-powered applications.
这是构建LLM驱动型应用最受欢迎的框架。

When to Use

适用场景

  • 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

  • 构建具备工具调用与推理能力的agents(采用ReAct模式)
  • 实现RAG(检索增强生成)流水线
  • 需要轻松切换LLM提供商(OpenAI、Anthropic、Google等)
  • 创建带有对话记忆的聊天机器人
  • LLM应用的快速原型开发

Core Components

核心组件

ComponentPurposeKey Concept
Chat ModelsLLM interfaceUnified API across providers
AgentsTool use + reasoningReAct pattern
ChainsSequential operationsComposable pipelines
MemoryConversation stateBuffer, summary, vector
RetrieversDocument lookupVector search, hybrid
ToolsExternal capabilitiesFunctions agents can call

组件用途核心概念
Chat ModelsLLM 接口跨提供商的统一API
Agents工具使用 + 推理ReAct模式
Chains序列操作可组合的流水线
Memory对话状态缓冲区、摘要、向量
Retrievers文档检索向量搜索、混合搜索
Tools外部能力扩展Agents可调用的函数

Agent Patterns

Agent模式

PatternDescriptionUse Case
ReActReason-Act-Observe loopGeneral tool use
Plan-and-ExecutePlan first, then executeComplex multi-step
Self-AskGenerate sub-questionsResearch tasks
Structured ChatJSON tool callingAPI integration
模式描述适用场景
ReAct推理-执行-观察循环通用工具调用场景
Plan-and-Execute先规划,再执行复杂多步骤任务
Self-Ask生成子问题研究类任务
Structured ChatJSON格式工具调用API集成场景

Tool Definition

工具定义

ElementPurpose
NameHow agent refers to tool
DescriptionWhen to use (critical for selection)
ParametersInput schema
Return typeWhat agent receives back
Key concept: Tool descriptions are critical—the LLM uses them to decide which tool to call. Be specific about when and why to use each tool.

元素用途
NameAgent指代工具的名称
Description工具的适用场景(对工具选择至关重要)
Parameters输入 schema
Return typeAgent接收的返回类型
核心概念:工具描述至关重要——LLM会依据描述来决定调用哪个工具。需明确说明每个工具的使用时机与原因。

RAG Pipeline Stages

RAG流水线阶段

StagePurposeOptions
LoadIngest documentsWeb, PDF, GitHub, DBs
SplitChunk into piecesRecursive, semantic
EmbedConvert to vectorsOpenAI, Cohere, local
StoreIndex vectorsChroma, FAISS, Pinecone
RetrieveFind relevant chunksSimilarity, MMR, hybrid
GenerateCreate responseLLM with context
阶段用途可选方案
Load文档导入网页、PDF、GitHub、数据库
Split文档分块递归分块、语义分块
Embed转换为向量OpenAI、Cohere、本地模型
Store向量索引存储Chroma、FAISS、Pinecone
Retrieve查找相关分块相似度检索、MMR、混合检索
Generate生成响应结合上下文的LLM

Chunking Strategies

分块策略

StrategyBest ForTypical Size
RecursiveGeneral text500-1000 chars
SemanticCoherent passagesVariable
Token-basedLLM context limits256-512 tokens
策略最佳适用场景典型尺寸
Recursive通用文本500-1000字符
Semantic连贯段落可变长度
Token-based适配LLM上下文限制256-512 tokens

Retrieval Strategies

检索策略

StrategyHow It Works
SimilarityNearest neighbors by embedding
MMRDiversity + relevance balance
HybridKeyword + semantic combined
Self-queryLLM generates metadata filters

策略工作原理
Similarity基于嵌入向量的最近邻检索
MMR平衡多样性与相关性
Hybrid关键词检索 + 语义检索结合
Self-queryLLM生成元数据过滤器

Memory Types

记忆类型

TypeStoresBest For
BufferFull conversationShort conversations
WindowLast N messagesMedium conversations
SummaryLLM-generated summaryLong conversations
VectorEmbedded messagesSemantic recall
EntityExtracted entitiesTrack facts about people/things
Key concept: Buffer memory grows unbounded. Use summary or vector for long conversations to stay within context limits.

类型存储内容最佳适用场景
Buffer完整对话内容短对话场景
Window最近N条消息中等长度对话场景
SummaryLLM生成的对话摘要长对话场景
Vector消息嵌入向量语义召回场景
Entity提取的实体信息追踪人物/事物相关事实
核心概念:Buffer记忆会无限制增长。对于长对话,使用摘要或向量记忆以控制在上下文限制内。

Document Loaders

文档加载器

SourceLoader Type
Web pagesWebBaseLoader, AsyncChromium
PDFsPyPDFLoader, UnstructuredPDF
CodeGitHubLoader, DirectoryLoader
DatabasesSQLDatabase, Postgres
APIsCustom loaders

数据源加载器类型
网页WebBaseLoader、AsyncChromium
PDFPyPDFLoader、UnstructuredPDF
代码GitHubLoader、DirectoryLoader
数据库SQLDatabase、Postgres
APIs自定义加载器

Vector Stores

向量存储

StoreTypeBest For
ChromaLocalDevelopment, small datasets
FAISSLocalLarge local datasets
PineconeCloudProduction, scale
WeaviateSelf-hosted/CloudHybrid search
QdrantSelf-hosted/CloudFiltering, metadata

存储类型部署方式最佳适用场景
Chroma本地开发环境、小型数据集
FAISS本地大型本地数据集
Pinecone云端生产环境、规模化场景
Weaviate自托管/云端混合搜索场景
Qdrant自托管/云端过滤、元数据检索场景

LangSmith Observability

LangSmith可观测性

FeatureBenefit
TracingSee every LLM call, tool use
EvaluationTest prompts systematically
DatasetsStore test cases
MonitoringTrack production performance
Key concept: Enable LangSmith tracing early—debugging agents without observability is extremely difficult.

功能优势
Tracing查看每一次LLM调用、工具使用记录
Evaluation系统化测试提示词
Datasets存储测试用例
Monitoring追踪生产环境性能
核心概念:尽早启用LangSmith追踪——没有可观测性的话,调试agents会极其困难。

Best Practices

最佳实践

PracticeWhy
Start simple
create_agent()
covers most cases
Enable streamingBetter UX for long responses
Use LangSmithEssential for debugging
Optimize chunk size500-1000 chars typically works
Cache embeddingsThey're expensive to compute
Test retrieval separatelyRAG quality depends on retrieval

实践原因
从简单开始
create_agent()
可覆盖大多数场景
启用流式输出长响应场景下提升用户体验
使用LangSmith调试必备工具
优化分块大小通常500-1000字符效果最佳
缓存嵌入向量嵌入向量计算成本高昂
单独测试检索环节RAG的质量依赖于检索效果

LangChain vs LangGraph

LangChain vs LangGraph

AspectLangChainLangGraph
Best forQuick agents, RAGComplex workflows
Code to start<10 lines~30 lines
State managementLimitedNative
Branching logicBasicAdvanced
Human-in-loopManualBuilt-in
Key concept: Use LangChain for straightforward agents and RAG. Use LangGraph when you need complex state machines, branching, or human checkpoints.
维度LangChainLangGraph
最佳适用场景快速构建agents、RAG复杂工作流
启动代码量<10行~30行
状态管理有限原生支持
分支逻辑基础高级
人工介入循环手动实现内置支持
核心概念:使用LangChain构建简单的agents与RAG。当需要复杂状态机、分支逻辑或人工检查点时,选择LangGraph。

Resources

资源