|
| 1 | +--- |
| 2 | +sidebar: aiSidebar |
| 3 | +title: AI Agent(智能体) |
| 4 | +--- |
| 5 | + |
| 6 | +# AI Agent(智能体) |
| 7 | + |
| 8 | +## 概述 |
| 9 | + |
| 10 | +**AI Agent(人工智能智能体)** 是一个能够**自主感知环境、做出决策并执行行动**的 AI 系统。与传统的聊天式 AI 不同,Agent 可以: |
| 11 | + |
| 12 | +- 主动调用工具 |
| 13 | +- 规划多步骤任务 |
| 14 | +- 处理复杂的工作流 |
| 15 | +- 在失败时自动重试 |
| 16 | + |
| 17 | +> **核心特点**:自主性、交互性、反应性、主动性 |
| 18 | +
|
| 19 | +--- |
| 20 | + |
| 21 | +## 核心概念 |
| 22 | + |
| 23 | +### 1. Agent 的定义 |
| 24 | + |
| 25 | +**Agent** 是能够: |
| 26 | + |
| 27 | +1. **感知** (Perceive):获取环境信息 |
| 28 | +2. **推理** (Reason):分析情况并制定计划 |
| 29 | +3. **行动** (Act):执行具体操作 |
| 30 | +4. **学习** (Learn):从反馈中改进 |
| 31 | + |
| 32 | +### 2. Agent 与 Chatbot 的区别 |
| 33 | + |
| 34 | +| 特性 | Chatbot | AI Agent | |
| 35 | +| ---- | ------- | -------- | |
| 36 | +| **交互方式** | 问答式 | 任务导向 | |
| 37 | +| **主动性** | 被动响应 | 主动规划 | |
| 38 | +| **工具使用** | 有限 | 丰富 | |
| 39 | +| **任务复杂度** | 单步任务 | 多步骤任务 | |
| 40 | +| **记忆能力** | 会话级 | 长期记忆 | |
| 41 | +| **自主决策** | 无 | 有 | |
| 42 | + |
| 43 | +--- |
| 44 | + |
| 45 | +## Agent 的架构 |
| 46 | + |
| 47 | +### 基本架构 |
| 48 | + |
| 49 | +``` |
| 50 | +┌─────────────────────────────────────────────────────────┐ |
| 51 | +│ AI Agent 架构 │ |
| 52 | +├─────────────────────────────────────────────────────────┤ |
| 53 | +│ │ |
| 54 | +│ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ |
| 55 | +│ │ 输入 │ ──▶│ 推理 │ ──▶│ 输出 │ │ |
| 56 | +│ │ Input │ │ Reasoning│ │ Output │ │ |
| 57 | +│ └─────────┘ └────┬────┘ └─────────┘ │ |
| 58 | +│ │ │ |
| 59 | +│ ▼ │ |
| 60 | +│ ┌─────────────┐ │ |
| 61 | +│ │ 工具调用 │ │ |
| 62 | +│ │ Tools │ │ |
| 63 | +│ └─────────────┘ │ |
| 64 | +│ │ │ |
| 65 | +│ ▼ │ |
| 66 | +│ ┌─────────────┐ │ |
| 67 | +│ │ 环境反馈 │ │ |
| 68 | +│ │ Feedback │ │ |
| 69 | +│ └─────────────┘ │ |
| 70 | +│ │ |
| 71 | +└─────────────────────────────────────────────────────────┘ |
| 72 | +``` |
| 73 | + |
| 74 | +### 核心组件 |
| 75 | + |
| 76 | +#### 1. 规划模块 (Planning) |
| 77 | + |
| 78 | +- 任务分解 |
| 79 | +- 步骤排序 |
| 80 | +- 资源分配 |
| 81 | +- 时间估算 |
| 82 | + |
| 83 | +#### 2. 记忆模块 (Memory) |
| 84 | + |
| 85 | +- 短期记忆(当前会话) |
| 86 | +- 长期记忆(向量存储) |
| 87 | +- 上下文管理 |
| 88 | +- 知识检索 |
| 89 | + |
| 90 | +#### 3. 工具模块 (Tools) |
| 91 | + |
| 92 | +- 文件操作 |
| 93 | +- API 调用 |
| 94 | +- 命令执行 |
| 95 | +- 数据库查询 |
| 96 | + |
| 97 | +#### 4. 反思模块 (Reflection) |
| 98 | + |
| 99 | +- 结果验证 |
| 100 | +- 错误处理 |
| 101 | +- 策略调整 |
| 102 | +- 重试机制 |
| 103 | + |
| 104 | +--- |
| 105 | + |
| 106 | +## Agent 的类型 |
| 107 | + |
| 108 | +### 1. 单 Agent 系统 |
| 109 | + |
| 110 | +由一个 Agent 完成所有任务: |
| 111 | + |
| 112 | +``` |
| 113 | +┌─────────────────┐ |
| 114 | +│ Agent │ |
| 115 | +│ ┌───────────┐ │ |
| 116 | +│ │ Planning │ │ |
| 117 | +│ │ Memory │ │ |
| 118 | +│ │ Tools │ │ |
| 119 | +│ │ Action │ │ |
| 120 | +│ └───────────┘ │ |
| 121 | +└─────────────────┘ |
| 122 | +``` |
| 123 | + |
| 124 | +**特点**: |
| 125 | +- 实现简单 |
| 126 | +- 适合单领域任务 |
| 127 | +- 容易调试 |
| 128 | + |
| 129 | +### 2. 多 Agent 系统 |
| 130 | + |
| 131 | +多个 Agent 协同工作: |
| 132 | + |
| 133 | +``` |
| 134 | +┌─────────────────────────────────────────────────────────┐ |
| 135 | +│ Multi-Agent System │ |
| 136 | +├─────────────────────────────────────────────────────────┤ |
| 137 | +│ │ |
| 138 | +│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ |
| 139 | +│ │ Planner │ ──▶│ Coder │ ──▶│ Tester │ │ |
| 140 | +│ │ Agent │ │ Agent │ │ Agent │ │ |
| 141 | +│ └──────────┘ └──────────┘ └──────────┘ │ |
| 142 | +│ │ │ │ │ |
| 143 | +│ └───────────────┴───────────────┘ │ |
| 144 | +│ ▼ │ |
| 145 | +│ ┌──────────────┐ │ |
| 146 | +│ │ Coordinator │ │ |
| 147 | +│ │ Agent │ │ |
| 148 | +│ └──────────────┘ │ |
| 149 | +│ │ |
| 150 | +└─────────────────────────────────────────────────────────┘ |
| 151 | +``` |
| 152 | + |
| 153 | +**特点**: |
| 154 | +- 专业化分工 |
| 155 | +- 可并行处理 |
| 156 | +- 适合复杂任务 |
| 157 | + |
| 158 | +**常见角色**: |
| 159 | + |
| 160 | +| Agent 角色 | 职责 | |
| 161 | +| ---------- | ---- | |
| 162 | +| **Planner** | 任务规划、分解 | |
| 163 | +| **Coder** | 代码生成、修改 | |
| 164 | +| **Reviewer** | 代码审查、验证 | |
| 165 | +| **Tester** | 测试用例生成 | |
| 166 | +| **Debugger** | 问题定位、修复 | |
| 167 | +| **Documenter** | 文档生成 | |
| 168 | + |
| 169 | +### 3. 层级 Agent 系统 |
| 170 | + |
| 171 | +Agent 之间存在上下级关系: |
| 172 | + |
| 173 | +``` |
| 174 | +┌─────────────────────────────────────────────────────────┐ |
| 175 | +│ Hierarchical Agent System │ |
| 176 | +├─────────────────────────────────────────────────────────┤ |
| 177 | +│ │ |
| 178 | +│ ┌───────────────────────────────────────┐ │ |
| 179 | +│ │ Manager Agent (L1) │ │ |
| 180 | +│ │ 任务分配、进度监控、协调 │ │ |
| 181 | +│ └─────────────┬─────────────────────────┘ │ |
| 182 | +│ │ │ |
| 183 | +│ ┌──────────┼──────────┐ │ |
| 184 | +│ ▼ ▼ ▼ │ |
| 185 | +│ ┌──────┐ ┌──────┐ ┌──────┐ │ |
| 186 | +│ │Coder ││Tester││Doc │ (L2) │ |
| 187 | +│ │Agent ││Agent ││Agent │ │ |
| 188 | +│ └──────┘ └──────┘ └──────┘ │ |
| 189 | +│ │ |
| 190 | +└─────────────────────────────────────────────────────────┘ |
| 191 | +``` |
| 192 | + |
| 193 | +--- |
| 194 | + |
| 195 | +## 主流 Agent 框架 |
| 196 | + |
| 197 | +### 1. LangChain Agent |
| 198 | + |
| 199 | +```python |
| 200 | +from langchain.agents import create_openai_functions_agent |
| 201 | +from langchain.tools import Tool |
| 202 | + |
| 203 | +# 定义工具 |
| 204 | +tools = [ |
| 205 | + Tool( |
| 206 | + name="calculator", |
| 207 | + func=lambda x: eval(x), |
| 208 | + description="执行数学计算" |
| 209 | + ) |
| 210 | +] |
| 211 | + |
| 212 | +# 创建 Agent |
| 213 | +agent = create_openai_functions_agent( |
| 214 | + llm=chat_model, |
| 215 | + tools=tools, |
| 216 | + prompt=prompt |
| 217 | +) |
| 218 | +``` |
| 219 | + |
| 220 | +### 2. AutoGen |
| 221 | + |
| 222 | +```python |
| 223 | +from autogen import AssistantAgent, UserProxyAgent |
| 224 | + |
| 225 | +# 创建 Agent |
| 226 | +assistant = AssistantAgent( |
| 227 | + name="assistant", |
| 228 | + llm_config={"model": "gpt-4"} |
| 229 | +) |
| 230 | + |
| 231 | +user_proxy = UserProxyAgent( |
| 232 | + name="user_proxy", |
| 233 | + code_execution_config={"work_dir": "coding"} |
| 234 | +) |
| 235 | + |
| 236 | +# 启动对话 |
| 237 | +user_proxy.initiate_chat( |
| 238 | + assistant, |
| 239 | + message="计算斐波那契数列的第 10 项" |
| 240 | +) |
| 241 | +``` |
| 242 | + |
| 243 | +### 3. Claude Code Task Agent |
| 244 | + |
| 245 | +```typescript |
| 246 | +// 使用 Claude Code 的子代理 |
| 247 | +import { Task } from '@anthropic-ai/claude-code'; |
| 248 | + |
| 249 | +const result = await Task({ |
| 250 | + description: "分析项目代码结构", |
| 251 | + subagentType: "explore", |
| 252 | + model: "claude-opus-4-5" |
| 253 | +}); |
| 254 | +``` |
| 255 | + |
| 256 | +### 4. Cursor Composer |
| 257 | + |
| 258 | +Cursor 的 Agent 系统: |
| 259 | +- **Composer**:多步骤任务执行 |
| 260 | +- **Background Agent**:后台处理 |
| 261 | +- **Multi-Agent Interface**:并行多智能体 |
| 262 | + |
| 263 | +--- |
| 264 | + |
| 265 | +## Agent 的设计模式 |
| 266 | + |
| 267 | +### 1. ReAct (Reasoning + Acting) |
| 268 | + |
| 269 | +``` |
| 270 | +Thought (思考) → Action (行动) → Observation (观察) → Thought (思考) → ... |
| 271 | +``` |
| 272 | + |
| 273 | +**示例**: |
| 274 | +``` |
| 275 | +Thought: 我需要读取文件内容 |
| 276 | +Action: ReadFile(path="src/main.js") |
| 277 | +Observation: 读取到 100 行代码 |
| 278 | +Thought: 我看到了问题所在 |
| 279 | +Action: EditFile(...) |
| 280 | +``` |
| 281 | + |
| 282 | +### 2. Chain of Thought |
| 283 | + |
| 284 | +逐步推理,展示思考过程: |
| 285 | + |
| 286 | +``` |
| 287 | +问题:用户报告登录失败 |
| 288 | +
|
| 289 | +思考步骤: |
| 290 | +1. 检查认证逻辑 |
| 291 | +2. 查看日志文件 |
| 292 | +3. 验证 API 配置 |
| 293 | +4. 定位问题原因 |
| 294 | +``` |
| 295 | + |
| 296 | +### 3. Plan-and-Execute |
| 297 | + |
| 298 | +先规划再执行: |
| 299 | + |
| 300 | +``` |
| 301 | +1. 规划阶段: |
| 302 | + - 分析需求 |
| 303 | + - 制定计划 |
| 304 | + - 分解任务 |
| 305 | +
|
| 306 | +2. 执行阶段: |
| 307 | + - 按步骤执行 |
| 308 | + - 记录进度 |
| 309 | + - 处理异常 |
| 310 | +``` |
| 311 | + |
| 312 | +### 4. Self-Refine |
| 313 | + |
| 314 | +自我反思和改进: |
| 315 | + |
| 316 | +``` |
| 317 | +1. 生成初始方案 |
| 318 | +2. 自我审查 |
| 319 | +3. 识别问题 |
| 320 | +4. 改进方案 |
| 321 | +5. 重复 2-4 |
| 322 | +``` |
| 323 | + |
| 324 | +--- |
| 325 | + |
| 326 | +## Agent 的最佳实践 |
| 327 | + |
| 328 | +### 1. 明确目标 |
| 329 | + |
| 330 | +- 清晰定义任务边界 |
| 331 | +- 设定成功标准 |
| 332 | +- 明确输出格式 |
| 333 | + |
| 334 | +### 2. 工具设计 |
| 335 | + |
| 336 | +- 工具功能单一 |
| 337 | +- 输入输出明确 |
| 338 | +- 错误处理完善 |
| 339 | + |
| 340 | +### 3. 记忆管理 |
| 341 | + |
| 342 | +- 合理设置上下文窗口 |
| 343 | +- 使用向量存储长期记忆 |
| 344 | +- 定期清理无关信息 |
| 345 | + |
| 346 | +### 4. 安全考虑 |
| 347 | + |
| 348 | +- 限制可执行的操作 |
| 349 | +- 实施权限控制 |
| 350 | +- 记录所有行动 |
| 351 | + |
| 352 | +### 5. 可观测性 |
| 353 | + |
| 354 | +- 记录决策过程 |
| 355 | +- 监控执行状态 |
| 356 | +- 追踪资源使用 |
| 357 | + |
| 358 | +--- |
| 359 | + |
| 360 | +## Agent 的应用场景 |
| 361 | + |
| 362 | +| 场景 | Agent 类型 | 说明 | |
| 363 | +| ---- | ---------- | ---- | |
| 364 | +| **代码生成** | Coder Agent | 生成、修改代码 | |
| 365 | +| **代码审查** | Reviewer Agent | 审查代码质量 | |
| 366 | +| **自动化测试** | Tester Agent | 生成测试用例 | |
| 367 | +| **Bug 修复** | Debugger Agent | 定位和修复问题 | |
| 368 | +| **文档生成** | Documenter Agent | 生成技术文档 | |
| 369 | +| **数据分析** | Analyst Agent | 处理数据任务 | |
| 370 | +| **运维自动化** | Ops Agent | 自动化运维操作 | |
| 371 | + |
| 372 | +--- |
| 373 | + |
| 374 | +## 参考资源 |
| 375 | + |
| 376 | +### 论文 |
| 377 | + |
| 378 | +- [ReAct: Synergizing Reasoning and Acting in Language Models](https://arxiv.org/abs/2210.03629) |
| 379 | +- [AutoGen: Enabling Next-Gen LLM Applications](https://arxiv.org/abs/2308.08155) |
| 380 | + |
| 381 | +### 框架文档 |
| 382 | + |
| 383 | +- [LangChain Agents](https://python.langchain.com/docs/modules/agents/) |
| 384 | +- [AutoGen Documentation](https://microsoft.github.io/autogen/) |
| 385 | +- [Claude Code CLI](https://docs.anthropic.com/claude-code) |
| 386 | + |
| 387 | +--- |
| 388 | + |
| 389 | +**文档更新时间:2025 年 12 月** |
0 commit comments