Files

55 lines
1.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Step 02: 理解 State - 状态管理
## 🎯 学习目标
- 理解什么是 Agent State(代理状态)
- 理解为什么 Agent 需要状态
- 学会设计合理的状态结构
- 理解状态在多步骤任务中的作用
---
## 📖 概念讲解
### 什么是 State
State(状态)是 Agent 的"记忆"——它记录了:
1. **当前任务进展**:完成了多少,还剩多少
2. **历史数据**:用户说过什么,生成过什么
3. **中间结果**:每个步骤的输出是什么
4. **工具调用结果**:工具返回了什么
```
没有 State 的 Agent
用户: "生成报表"
Agent: 生成报表
用户: "把标题改成黑色"
Agent: ??? 我不记得你刚才生成的是什么报表
有 State 的 Agent
用户: "生成报表"
Agent: 生成报表,记录到 state
state = {current_jrxml: "..."}
用户: "把标题改成黑色"
Agent: 从 state 读取 current_jrxml
修改标题
更新 state = {current_jrxml: "新报表"}
```
### 为什么需要精心设计 State?
一个好的 State 设计应该:
1. **包含所有必要信息**:不遗漏关键数据
2. **避免信息冗余**:不要重复存储相同数据
3. **结构清晰**:易于读取和更新
4. **类型安全**:有类型提示,减少 bug
---
## 💻 代码实现
请打开 `concept.py` 查看详细代码注释。