langchain-agents
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseLangChain - 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
核心组件
| Component | Purpose | Key Concept |
|---|---|---|
| Chat Models | LLM interface | Unified API across providers |
| Agents | Tool use + reasoning | ReAct pattern |
| Chains | Sequential operations | Composable pipelines |
| Memory | Conversation state | Buffer, summary, vector |
| Retrievers | Document lookup | Vector search, hybrid |
| Tools | External capabilities | Functions agents can call |
| 组件 | 用途 | 核心概念 |
|---|---|---|
| Chat Models | LLM 接口 | 跨提供商的统一API |
| Agents | 工具使用 + 推理 | ReAct模式 |
| Chains | 序列操作 | 可组合的流水线 |
| Memory | 对话状态 | 缓冲区、摘要、向量 |
| Retrievers | 文档检索 | 向量搜索、混合搜索 |
| Tools | 外部能力扩展 | Agents可调用的函数 |
Agent Patterns
Agent模式
| Pattern | Description | Use Case |
|---|---|---|
| ReAct | Reason-Act-Observe loop | General tool use |
| Plan-and-Execute | Plan first, then execute | Complex multi-step |
| Self-Ask | Generate sub-questions | Research tasks |
| Structured Chat | JSON tool calling | API integration |
| 模式 | 描述 | 适用场景 |
|---|---|---|
| ReAct | 推理-执行-观察循环 | 通用工具调用场景 |
| Plan-and-Execute | 先规划,再执行 | 复杂多步骤任务 |
| Self-Ask | 生成子问题 | 研究类任务 |
| Structured Chat | JSON格式工具调用 | API集成场景 |
Tool Definition
工具定义
| Element | Purpose |
|---|---|
| Name | How agent refers to tool |
| Description | When to use (critical for selection) |
| Parameters | Input schema |
| Return type | What 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.
| 元素 | 用途 |
|---|---|
| Name | Agent指代工具的名称 |
| Description | 工具的适用场景(对工具选择至关重要) |
| Parameters | 输入 schema |
| Return type | Agent接收的返回类型 |
核心概念:工具描述至关重要——LLM会依据描述来决定调用哪个工具。需明确说明每个工具的使用时机与原因。
RAG Pipeline Stages
RAG流水线阶段
| Stage | Purpose | Options |
|---|---|---|
| Load | Ingest documents | Web, PDF, GitHub, DBs |
| Split | Chunk into pieces | Recursive, semantic |
| Embed | Convert to vectors | OpenAI, Cohere, local |
| Store | Index vectors | Chroma, FAISS, Pinecone |
| Retrieve | Find relevant chunks | Similarity, MMR, hybrid |
| Generate | Create response | LLM with context |
| 阶段 | 用途 | 可选方案 |
|---|---|---|
| Load | 文档导入 | 网页、PDF、GitHub、数据库 |
| Split | 文档分块 | 递归分块、语义分块 |
| Embed | 转换为向量 | OpenAI、Cohere、本地模型 |
| Store | 向量索引存储 | Chroma、FAISS、Pinecone |
| Retrieve | 查找相关分块 | 相似度检索、MMR、混合检索 |
| Generate | 生成响应 | 结合上下文的LLM |
Chunking Strategies
分块策略
| Strategy | Best For | Typical Size |
|---|---|---|
| Recursive | General text | 500-1000 chars |
| Semantic | Coherent passages | Variable |
| Token-based | LLM context limits | 256-512 tokens |
| 策略 | 最佳适用场景 | 典型尺寸 |
|---|---|---|
| Recursive | 通用文本 | 500-1000字符 |
| Semantic | 连贯段落 | 可变长度 |
| Token-based | 适配LLM上下文限制 | 256-512 tokens |
Retrieval Strategies
检索策略
| Strategy | How It Works |
|---|---|
| Similarity | Nearest neighbors by embedding |
| MMR | Diversity + relevance balance |
| Hybrid | Keyword + semantic combined |
| Self-query | LLM generates metadata filters |
| 策略 | 工作原理 |
|---|---|
| Similarity | 基于嵌入向量的最近邻检索 |
| MMR | 平衡多样性与相关性 |
| Hybrid | 关键词检索 + 语义检索结合 |
| Self-query | LLM生成元数据过滤器 |
Memory Types
记忆类型
| Type | Stores | Best For |
|---|---|---|
| Buffer | Full conversation | Short conversations |
| Window | Last N messages | Medium conversations |
| Summary | LLM-generated summary | Long conversations |
| Vector | Embedded messages | Semantic recall |
| Entity | Extracted entities | Track 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条消息 | 中等长度对话场景 |
| Summary | LLM生成的对话摘要 | 长对话场景 |
| Vector | 消息嵌入向量 | 语义召回场景 |
| Entity | 提取的实体信息 | 追踪人物/事物相关事实 |
核心概念:Buffer记忆会无限制增长。对于长对话,使用摘要或向量记忆以控制在上下文限制内。
Document Loaders
文档加载器
| Source | Loader Type |
|---|---|
| Web pages | WebBaseLoader, AsyncChromium |
| PDFs | PyPDFLoader, UnstructuredPDF |
| Code | GitHubLoader, DirectoryLoader |
| Databases | SQLDatabase, Postgres |
| APIs | Custom loaders |
| 数据源 | 加载器类型 |
|---|---|
| 网页 | WebBaseLoader、AsyncChromium |
| PyPDFLoader、UnstructuredPDF | |
| 代码 | GitHubLoader、DirectoryLoader |
| 数据库 | SQLDatabase、Postgres |
| APIs | 自定义加载器 |
Vector Stores
向量存储
| Store | Type | Best For |
|---|---|---|
| Chroma | Local | Development, small datasets |
| FAISS | Local | Large local datasets |
| Pinecone | Cloud | Production, scale |
| Weaviate | Self-hosted/Cloud | Hybrid search |
| Qdrant | Self-hosted/Cloud | Filtering, metadata |
| 存储类型 | 部署方式 | 最佳适用场景 |
|---|---|---|
| Chroma | 本地 | 开发环境、小型数据集 |
| FAISS | 本地 | 大型本地数据集 |
| Pinecone | 云端 | 生产环境、规模化场景 |
| Weaviate | 自托管/云端 | 混合搜索场景 |
| Qdrant | 自托管/云端 | 过滤、元数据检索场景 |
LangSmith Observability
LangSmith可观测性
| Feature | Benefit |
|---|---|
| Tracing | See every LLM call, tool use |
| Evaluation | Test prompts systematically |
| Datasets | Store test cases |
| Monitoring | Track production performance |
Key concept: Enable LangSmith tracing early—debugging agents without observability is extremely difficult.
| 功能 | 优势 |
|---|---|
| Tracing | 查看每一次LLM调用、工具使用记录 |
| Evaluation | 系统化测试提示词 |
| Datasets | 存储测试用例 |
| Monitoring | 追踪生产环境性能 |
核心概念:尽早启用LangSmith追踪——没有可观测性的话,调试agents会极其困难。
Best Practices
最佳实践
| Practice | Why |
|---|---|
| Start simple | |
| Enable streaming | Better UX for long responses |
| Use LangSmith | Essential for debugging |
| Optimize chunk size | 500-1000 chars typically works |
| Cache embeddings | They're expensive to compute |
| Test retrieval separately | RAG quality depends on retrieval |
| 实践 | 原因 |
|---|---|
| 从简单开始 | |
| 启用流式输出 | 长响应场景下提升用户体验 |
| 使用LangSmith | 调试必备工具 |
| 优化分块大小 | 通常500-1000字符效果最佳 |
| 缓存嵌入向量 | 嵌入向量计算成本高昂 |
| 单独测试检索环节 | RAG的质量依赖于检索效果 |
LangChain vs LangGraph
LangChain vs LangGraph
| Aspect | LangChain | LangGraph |
|---|---|---|
| Best for | Quick agents, RAG | Complex workflows |
| Code to start | <10 lines | ~30 lines |
| State management | Limited | Native |
| Branching logic | Basic | Advanced |
| Human-in-loop | Manual | Built-in |
Key concept: Use LangChain for straightforward agents and RAG. Use LangGraph when you need complex state machines, branching, or human checkpoints.
| 维度 | LangChain | LangGraph |
|---|---|---|
| 最佳适用场景 | 快速构建agents、RAG | 复杂工作流 |
| 启动代码量 | <10行 | ~30行 |
| 状态管理 | 有限 | 原生支持 |
| 分支逻辑 | 基础 | 高级 |
| 人工介入循环 | 手动实现 | 内置支持 |
核心概念:使用LangChain构建简单的agents与RAG。当需要复杂状态机、分支逻辑或人工检查点时,选择LangGraph。