跳转至

02 - 智能体系统(Agent Systems)

⚠️ 时效性说明:本章涉及前沿模型/价格/榜单等信息,可能随版本快速变化;请以论文原文、官方发布页和 API 文档为准。

学习目标:深入理解智能体的架构、能力和应用,掌握工具使用、规划推理和多智能体协作的核心技术。

📌 定位说明:本章侧重智能体系统的研究前沿与理论分析。 - 动手实践(手写Agent框架、MCP开发、项目实战)→ AI Agent开发实战/ - 框架应用(LangChain/LangGraph Agent开发)→ LLM应用/07-Agent开发基础 · LLM应用/17-多Agent框架


1. 什么是智能体?

1.1 智能体的定义

智能体(Agent) 是一个能够感知环境进行决策执行动作的自主系统。

Text Only
传统大模型:
输入 → [大模型] → 输出(文本)
      静态知识(训练数据)

智能体系统:
环境感知 → [智能体] → 决策 → 执行动作 → 影响环境
            可以:
            - 使用工具(Tool Use)
            - 访问外部知识
            - 进行多步规划
            - 与其他智能体协作

1.2 智能体的核心能力

能力 描述 示例
工具使用 调用外部API和函数 查天气、计算、搜索
规划推理 分解任务,制定计划 多步骤问题解决
记忆管理 短期和长期记忆 上下文保持、知识积累
自主决策 根据环境反馈调整 错误恢复、策略优化
多智能体协作 与其他智能体配合 分工合作、协商沟通

1.3 为什么需要智能体?

Text Only
大模型的局限:
1. 知识静态 - 无法获取实时信息
2. 无法行动 - 不能直接操作外部系统
3. 计算有限 - 复杂计算容易出错
4. 没有记忆 - 无法保持长期上下文

智能体的解决方案:
1. 工具使用 → 获取实时信息
2. 环境交互 → 执行实际操作
3. 外部计算 → 精确计算和验证
4. 记忆系统 → 持久化存储和检索

2. 智能体架构

2.1 基础架构:ReAct

ReAct(Reasoning + Acting) 是智能体的经典架构。

Text Only
循环:
观察(Observation)
思考(Thought)
行动(Action)
执行 → 环境反馈 → 回到观察

示例流程:
用户:"北京今天天气怎么样?适合穿什么?"

Thought 1: 用户询问北京今天的天气和穿衣建议。
          我需要先获取北京的天气信息。
Action 1: get_weather(location="北京", date="今天")
Observation 1: {"temperature": "15°C", "weather": "晴", "wind": "微风"}

Thought 2: 天气是晴天,15°C,微风。
          这是一个温暖的春日天气。
          建议穿轻薄外套或长袖。
Action 2: finish(answer="北京今天晴天,15°C,微风。建议穿轻薄外套或长袖衬衫。")

2.2 架构组件详解

组件1:感知模块(Perception)

Python
class PerceptionModule:
    """感知模块:处理环境输入"""

    def process(self, raw_input):
        """
        处理原始输入
        - 用户消息
        - 工具返回结果
        - 系统状态
        """
        if isinstance(raw_input, dict):  # isinstance检查类型
            # 结构化数据(工具返回)
            return self._format_observation(raw_input)
        else:
            # 文本输入
            return raw_input

    def _format_observation(self, data):
        """将结构化数据格式化为文本"""
        return json.dumps(data, ensure_ascii=False, indent=2)  # json.dumps将Python对象→JSON字符串

组件2:推理引擎(Reasoning)

Python
class ReasoningEngine:
    """推理引擎:基于LLM进行思考"""

    def __init__(self, llm):
        self.llm = llm
        self.system_prompt = """你是一个智能助手。你可以使用以下工具:

可用工具:
{tools_description}

请按以下格式思考:
Thought: 你的思考过程
Action: 工具名称
Action Input: 工具参数(JSON格式)

或者完成任务:
Thought: 任务完成
Final Answer: 最终答案
"""

    def think(self, context):
        """基于上下文进行推理"""
        response = self.llm.generate(
            system_prompt=self.system_prompt,
            context=context
        )
        return self._parse_response(response)

    def _parse_response(self, response):
        """解析LLM输出,提取Thought和Action"""
        # 解析逻辑...
        pass

组件3:工具执行器(Tool Executor)

Python
class ToolExecutor:
    """工具执行器:管理和执行工具"""

    def __init__(self):
        self.tools = {}

    def register_tool(self, name, func, description):
        """注册工具"""
        self.tools[name] = {
            'function': func,
            'description': description
        }

    def execute(self, tool_name, params):
        """执行工具"""
        if tool_name not in self.tools:
            return f"Error: Tool '{tool_name}' not found"

        try:  # try/except捕获异常,防止程序崩溃
            result = self.tools[tool_name]['function'](**params)
            return result
        except Exception as e:
            return f"Error: {str(e)}"

组件4:记忆系统(Memory)

Python
class MemorySystem:
    """记忆系统:管理短期和长期记忆"""

    def __init__(self):
        # 短期记忆(当前会话)
        self.short_term = []

        # 长期记忆(向量数据库)
        self.long_term = VectorDB()

    def add_short_term(self, item):
        """添加到短期记忆"""
        self.short_term.append(item)

        # 保持最近N条
        if len(self.short_term) > 10:
            self.short_term.pop(0)

    def add_long_term(self, item, embedding):
        """添加到长期记忆"""
        self.long_term.store(item, embedding)

    def retrieve(self, query_embedding, k=5):
        """检索相关记忆"""
        # 从长期记忆中检索
        relevant = self.long_term.search(query_embedding, k)

        # 结合短期记忆
        return self.short_term + relevant

2.3 高级架构模式

模式1:计划-执行(Plan-and-Execute)

Text Only
第一阶段:规划
  任务 → [规划器] → 步骤列表

第二阶段:执行
  对每个步骤:
    步骤 → [执行器] → 结果

第三阶段:验证
  结果 → [验证器] → 是否完成/重试

示例:
任务:"帮我订一张明天北京到上海的高铁票"

Plan:
1. 查询明天北京到上海的高铁班次
2. 获取用户偏好的时间和座位类型
3. 选择合适的车次
4. 填写乘客信息
5. 确认订单并支付

Execute:
  Step 1 → 查询API → 返回10个班次
  Step 2 → 询问用户 → 用户选择G1,二等座
  Step 3 → 选择G1 → 确认有票
  Step 4 → 填写信息 → 成功
  Step 5 → 支付 → 完成

模式2:多智能体协作

Text Only
协调者(Orchestrator)
    ↓ 分配任务
┌────┴────┬────────┬────────┐
↓         ↓        ↓        ↓
研究Agent  代码Agent  测试Agent  文档Agent
    ↓         ↓        ↓        ↓
    └────────┴────────┴────────┘
          协调者整合
           最终结果

示例:软件开发
- 研究Agent:调研技术方案
- 代码Agent:编写代码
- 测试Agent:编写测试用例
- 文档Agent:编写文档

3. 工具使用(Tool Use)

3.1 工具的定义和分类

Python
# 工具的标准定义
tool_definition = {
    "name": "get_weather",
    "description": "获取指定城市的天气信息",
    "parameters": {
        "type": "object",
        "properties": {
            "location": {
                "type": "string",
                "description": "城市名称,如'北京'"
            },
            "date": {
                "type": "string",
                "description": "日期,如'今天'、'明天'或'2024-01-01'"
            }
        },
        "required": ["location"]
    }
}

工具分类

类别 功能 示例
信息检索 获取实时信息 搜索、天气、新闻
计算工具 精确计算 计算器、代码执行
外部API 调用服务 订票、购物、支付
文件操作 读写文件 读取文档、保存结果
数据库 数据查询 SQL查询、知识库检索

3.2 Function Calling机制

Python
# OpenAI Function Calling示例
import openai

functions = [
    {
        "name": "get_weather",
        "description": "获取天气信息",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {"type": "string"},
                "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
            },
            "required": ["location"]
        }
    }
]

from openai import OpenAI
client = OpenAI()

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "user", "content": "北京今天天气怎么样?"}
    ],
    tools=[{"type": "function", "function": f} for f in functions],
    tool_choice="auto"
)

# 模型决定调用函数
tool_calls = response.choices[0].message.tool_calls
if tool_calls:
    tool_call = tool_calls[0]
    function_name = tool_call.function.name
    arguments = json.loads(tool_call.function.arguments)  # json.loads将JSON字符串→Python对象

    # 执行函数
    result = get_weather(**arguments)

    # 将结果返回给模型
    second_response = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {"role": "user", "content": "北京今天天气怎么样?"},
            response.choices[0].message,
            {"role": "tool", "tool_call_id": tool_call.id, "content": result}
        ]
    )

3.3 工具学习(Tool Learning)

阶段1:工具理解

Text Only
目标:让模型理解工具的用途和参数

方法:
1. 工具描述微调
   - 输入:工具定义
   - 输出:工具用途说明

2. 使用示例学习
   - 输入:任务描述
   - 输出:正确的工具调用

示例数据:
{
  "instruction": "查询北京的天气",
  "thought": "用户想查询天气,我应该使用get_weather工具",
  "action": "get_weather",
  "action_input": {"location": "北京", "date": "今天"}
}

阶段2:工具选择

Text Only
挑战:面对多个工具,如何选择正确的?

解决方案:
1. 工具检索
   - 将工具描述编码为向量
   - 根据任务检索相关工具

2. 工具排序
   - 训练模型评估工具相关性
   - 选择Top-K个候选工具

3. 工具组合
   - 复杂任务需要多个工具
   - 学习工具之间的依赖关系

阶段3:工具创造

Text Only
高级能力:根据需求创建新工具

示例:
用户:"我需要计算一个列表的标准差"

模型发现没有直接工具,于是:
1. 创建临时工具:
   ```python
   def calculate_std(numbers):
       import statistics
       return statistics.stdev(numbers)
```text

2. 执行计算

3. 返回结果

这要求模型具备:
- 代码生成能力
- 理解任务需求
- 验证工具正确性

4. 规划与推理

4.1 任务分解(Task Decomposition)

方法1:链式思考(Chain-of-Thought)

Text Only
问题:"一个农场有鸡和兔,头共35个,脚共94只。鸡和兔各有多少只?"

CoT推理:
让我一步步解决这个问题。

设鸡有x只,兔有y只。
根据题意:
1. x + y = 35 (头的总数)
2. 2x + 4y = 94 (脚的总数)

从方程1:x = 35 - y
代入方程2:
2(35 - y) + 4y = 94
70 - 2y + 4y = 94
2y = 24
y = 12

所以兔有12只,鸡有35-12=23只。

验证:
头:23 + 12 = 35 ✓
脚:23×2 + 12×4 = 46 + 48 = 94 ✓

方法2:思维树(Tree of Thoughts)

Text Only
问题:"24点游戏,数字是4, 5, 6, 10"

ToT推理:

Level 1 - 可能的第一次操作:
├── 4 + 5 = 9
├── 4 × 5 = 20
├── 6 + 10 = 16
├── 6 × 10 = 60
├── 10 - 6 = 4
└── ...

Level 2 - 评估每个分支:
├── 4 + 5 = 9 → 剩余[6, 9, 10] → 评估:可能
├── 4 × 5 = 20 → 剩余[6, 10, 20] → 评估:可能
├── 6 × 10 = 60 → 剩余[4, 5, 60] → 评估:不太可能
└── ...

Level 3 - 深入探索有潜力的分支:
分支:4 × 5 = 20,剩余[6, 10, 20]
  ├── 20 + 6 = 26 → 剩余[10, 26] → 无法得到24
  ├── 20 - 6 = 14 → 剩余[10, 14] → 无法得到24
  ├── 20 + 10 = 30 → 剩余[6, 30] → 无法得到24
  └── 10 - 6 = 4 → 剩余[4, 20] → 20 + 4 = 24 ✓

找到解:(10 - 6) + 4 × 5 = 24

4.2 反思与自我修正(Reflection)

Python
class ReflectiveAgent:
    """具备反思能力的智能体"""

    def execute_with_reflection(self, task, max_retries=3):
        """执行任务,支持反思和重试"""

        for attempt in range(max_retries):
            # 执行
            result = self.execute(task)

            # 反思
            reflection = self.reflect(task, result)

            if reflection.is_success:
                return result

            if reflection.should_retry:
                # 根据反馈调整策略
                task = self.adjust_strategy(task, reflection.feedback)
            else:
                break

        return result

    def reflect(self, task, result):
        """反思执行结果"""
        reflection_prompt = f"""
        任务:{task}
        执行结果:{result}

        请反思:
        1. 结果是否正确?
        2. 过程中是否有错误?
        3. 如何改进?
        4. 是否需要重试?
        """

        reflection = self.llm.generate(reflection_prompt)
        return self.parse_reflection(reflection)

4.3 多步规划(Multi-step Planning)

Python
class Planner:
    """任务规划器"""

    def create_plan(self, goal):
        """创建实现目标的计划"""

        planning_prompt = f"""
        目标:{goal}

        请制定一个详细的执行计划:
        1. 将目标分解为具体步骤
        2. 每个步骤应该是可执行的
        3. 明确步骤之间的依赖关系
        4. 考虑可能的错误和备选方案

        格式:
        Step 1: [描述] (依赖: 无)
        Step 2: [描述] (依赖: Step 1)
        ...
        """

        plan_text = self.llm.generate(planning_prompt)
        return self.parse_plan(plan_text)

    def execute_plan(self, plan):
        """执行计划"""
        results = {}

        for step in plan.steps:
            # 检查依赖是否满足
            if not all(dep in results for dep in step.dependencies):  # all()全部为True才返回True
                continue

            # 执行步骤
            context = {dep: results[dep] for dep in step.dependencies}
            result = self.execute_step(step, context)
            results[step.id] = result

            # 检查是否需要调整计划
            if self.should_replan(result):
                plan = self.replan(plan, results)

        return results

5. 多智能体系统

5.1 多智能体架构

架构1:层级式(Hierarchical)

Text Only
                [管理者Agent]
                     |
        ┌────────────┼────────────┐
        ↓            ↓            ↓
   [子Agent 1]  [子Agent 2]  [子Agent 3]
        |            |            |
   [工具1,2]     [工具3,4]     [工具5,6]

特点:
- 管理者分配任务
- 子Agent专注特定领域
- 适合复杂任务分解

架构2:对等式(Peer-to-Peer)

Text Only
[Agent A] ←→ [Agent B]
    ↑    ↘   ↑
    └────→ [Agent C] ←→ [Agent D]

特点:
- 智能体平等协作
- 通过协商达成共识
- 适合分布式问题求解

架构3:市场式(Market-based)

Text Only
任务发布 → [拍卖机制] → Agent竞标
                    [Agent A] 中标
                        执行任务
                        返回结果

特点:
- 基于竞价分配任务
- 资源优化配置
- 适合动态环境

5.2 智能体间通信

通信协议

Python
class Message:
    """智能体间消息"""

    def __init__(self, sender, receiver, msg_type, content):
        self.sender = sender
        self.receiver = receiver
        self.type = msg_type  # REQUEST, INFORM, PROPOSE, ACCEPT, REJECT
        self.content = content
        self.timestamp = time.now()

class CommunicationProtocol:
    """通信协议"""

    def request(self, sender, receiver, task):
        """请求协助"""
        return Message(sender, receiver, "REQUEST", task)

    def inform(self, sender, receiver, information):
        """通知信息"""
        return Message(sender, receiver, "INFORM", information)

    def propose(self, sender, receiver, proposal):
        """提出建议"""
        return Message(sender, receiver, "PROPOSE", proposal)

协商机制

Python
class Negotiation:
    """协商机制"""

    def negotiate(self, agent_a, agent_b, issue):
        """
        双方就某个问题进行协商
        """
        # Agent A提出建议
        proposal_a = agent_a.make_proposal(issue)

        # Agent B评估
        evaluation = agent_b.evaluate(proposal_a)

        if evaluation.acceptable:
            return self.agreement(proposal_a)

        # Agent B提出反建议
        counter_proposal = agent_b.make_counter_proposal(issue, proposal_a)

        # 多轮协商...
        return self.iterative_negotiation(agent_a, agent_b, issue)

5.3 实际应用案例

案例:AutoGPT

Text Only
AutoGPT架构:

[目标设定]
[任务生成Agent] → 分解为子任务
[执行Agent] → 使用工具执行
[评估Agent] → 检查结果
  ┌─┴─┐
  ↓   ↓
成功  失败 → [反思] → 重试
[记忆存储]

案例:MetaGPT(软件开发)

Text Only
角色定义:
- 产品经理:写PRD
- 架构师:设计技术方案
- 项目经理:分配任务
- 工程师:写代码
- 测试工程师:写测试用例

工作流程:
1. 产品经理分析需求 → PRD文档
2. 架构师设计系统 → 技术设计文档
3. 项目经理分解任务 → 任务列表
4. 工程师并行开发 → 代码
5. 测试工程师验证 → 测试报告
6. 迭代优化

6. 智能体应用

6.1 个人助手

Text Only
能力:
- 日程管理
- 邮件处理
- 信息检索
- 任务提醒
- 旅行规划

示例对话:
用户:"帮我安排下周去上海的出差"

Agent:
1. 查询用户日历,找出空闲时间
2. 搜索北京到上海的航班
3. 推荐合适的酒店
4. 预订机票和酒店(经用户确认)
5. 添加到日历
6. 发送确认邮件

6.2 代码助手

Text Only
能力:
- 代码生成
- Bug修复
- 代码审查
- 文档生成
- 测试用例生成

示例:
用户:"帮我写一个Python函数,计算斐波那契数列"

Agent:
1. 生成代码
2. 提供测试用例
3. 解释算法复杂度
4. 提供优化版本(如使用缓存)

6.3 研究助手

Text Only
能力:
- 文献检索
- 信息整合
- 数据分析
- 报告生成

示例:
用户:"调研一下Transformer架构的发展历程"

Agent:
1. 搜索相关论文
2. 提取关键信息
3. 按时间线整理
4. 生成综述报告
5. 提供参考文献

6.4 客户服务

Text Only
能力:
- 问题理解
- 知识库检索
- 多轮对话
- 工单创建
- 情绪识别

示例:
用户:"我的订单还没收到,已经一周了"

Agent:
1. 查询订单状态
2. 解释延迟原因
3. 提供解决方案(加急/退款/补偿)
4. 执行用户选择的方案
5. 跟进直到问题解决

7. 挑战与未来方向

7.1 当前挑战

挑战 描述 可能的解决方案
可靠性 智能体可能犯错,且难以预测 增加验证层、人在回路
安全性 工具使用可能带来风险 权限控制、沙箱环境
效率 多步推理成本高 模型优化、缓存机制
可解释性 决策过程不透明 思维链、日志记录
泛化性 对新工具适应慢 元学习、快速适应

7.2 未来方向

Text Only
方向1:具身智能(Embodied AI)
- 智能体与物理世界交互
- 机器人控制
- 自动驾驶

方向2:终身学习(Lifelong Learning)
- 持续学习新技能
- 知识积累不遗忘
- 迁移学习

方向3:群体智能(Swarm Intelligence)
- 大规模智能体协作
- 自组织系统
- 涌现行为

方向4:人机协作(Human-AI Collaboration)
- 智能体辅助人类决策
- 人在回路
- 信任建立

8. 动手实践

实践1:构建简单的ReAct智能体

Python
class SimpleReActAgent:
    """简单的ReAct智能体实现"""

    def __init__(self, llm, tools):
        self.llm = llm
        self.tools = tools
        self.memory = []

    def run(self, query, max_steps=10):
        """运行智能体"""
        self.memory.append(f"User: {query}")

        for step in range(max_steps):
            # 构建prompt
            prompt = self._build_prompt()

            # 获取模型输出
            response = self.llm.generate(prompt)

            # 解析Thought和Action
            thought, action, action_input = self._parse(response)

            print(f"Step {step + 1}:")
            print(f"Thought: {thought}")
            print(f"Action: {action}")

            if action == "finish":
                return action_input

            # 执行工具
            observation = self.tools[action](**action_input)
            print(f"Observation: {observation}\n")

            # 添加到记忆
            self.memory.append(f"Thought: {thought}")
            self.memory.append(f"Action: {action}({action_input})")
            self.memory.append(f"Observation: {observation}")

        return "Max steps reached"

实践2:使用LangChain构建智能体

Python
from langchain.agents import Tool, AgentExecutor, create_react_agent
from langchain_community.tools import DuckDuckGoSearchRun
from langchain_openai import ChatOpenAI
from langchain import hub

# 定义工具
search = DuckDuckGoSearchRun()
tools = [
    Tool(
        name="Search",
        func=search.run,
        description="用于搜索实时信息"
    )
]

# 加载预定义的ReAct prompt
prompt = hub.pull("hwchase17/react")

# 创建LLM
llm = ChatOpenAI(model="gpt-4o")

# 创建Agent
agent = create_react_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

# 运行
response = agent_executor.invoke({
    "input": "2024年诺贝尔物理学奖得主是谁?"
})
print(response)

实践3:构建多智能体系统

Python
class MultiAgentSystem:
    """多智能体系统示例"""

    def __init__(self):
        self.agents = {
            'researcher': ResearchAgent(),
            'writer': WriterAgent(),
            'reviewer': ReviewerAgent()
        }

    def collaborative_writing(self, topic):
        """协作写作"""
        # 研究员收集资料
        research = self.agents['researcher'].research(topic)

        # 作者撰写初稿
        draft = self.agents['writer'].write(topic, research)

        # 审阅者评审
        feedback = self.agents['reviewer'].review(draft)

        # 根据反馈修改(迭代)
        while not feedback.approved:
            draft = self.agents['writer'].revise(draft, feedback)
            feedback = self.agents['reviewer'].review(draft)

        return draft

9. 下一步

完成本节后,你应该: - [ ] 理解智能体的核心概念和架构 - [ ] 掌握ReAct等基础架构 - [ ] 理解工具使用和Function Calling机制 - [ ] 了解任务分解和规划方法 - [ ] 理解多智能体系统的协作机制 - [ ] 能够构建简单的智能体应用

下一步03-RAG与长文本 - 学习如何让大模型利用外部知识库处理长文本


最后更新日期:2026-02-12 适用版本:LLM学习教程 v2026