AI Agent 入门指南:从原理到实践

什么是 AI Agent

AI Agent(人工智能代理)是一种能够自主感知环境、做出决策并执行动作的智能系统。与传统的规则脚本不同,Agent 核心的决策能力由大语言模型(LLM)驱动,使其能够理解复杂指令、规划多步任务、甚至自我反思。

简单来说:Agent = LLM + 工具 + 规划 + 记忆

Agent 与普通 AI 助手的区别

维度 普通 AI 助手 AI Agent
交互方式 问答式,一问一答 自主决策,持续执行
工具使用 需要手动调用 自动调用工具
任务分解 用户手动拆解 自动规划执行步骤
错误处理 返回错误后等待 自我反思,自动修正
记忆能力 无状态 有长期/短期记忆
适用场景 简单问答 复杂多步骤任务

Agent 的核心组件

一个典型的 Agent 系统包含以下组件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
┌─────────────────────────────────────────────┐
│ Agent │
│ ┌─────────┐ ┌─────────┐ ┌─────────────┐ │
│ │ LLM │ │ 工具 │ │ 记忆 │ │
│ │ (大脑) │ │(四肢) │ │ (存储) │ │
│ └────┬────┘ └────┬────┘ └──────┬──────┘ │
│ │ │ │ │
│ └────────────┼──────────────┘ │
│ ▼ │
│ ┌─────────┐ │
│ │ 规划器 │ │
│ │ (决策) │ │
│ └────┬────┘ │
│ ▼ │
│ ┌─────────┐ │
│ │ 执行器 │ │
│ │ (行动) │ │
│ └─────────┘ │
└─────────────────────────────────────────────┘

1. 大语言模型(LLM)

LLM 是 Agent 的”大脑”,负责:

  • 理解用户意图
  • 制定执行计划
  • 生成工具调用指令
  • 总结执行结果
  • 自我反思和改进

2. 工具(Tools)

工具让 Agent 能够与外部世界交互:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 常见工具类型
tools = [
# 搜索工具
{"type": "function", "name": "web_search",
"description": "搜索互联网获取最新信息"},

# 代码执行
{"type": "function", "name": "execute_code",
"description": "执行 Python/JS 代码"},

# 文件操作
{"type": "function", "name": "read_file",
"description": "读取文件内容"},

# API 调用
{"type": "function", "name": "http_request",
"description": "发送 HTTP 请求"},

# 数据库查询
{"type": "function", "name": "query_database",
"description": "执行 SQL 查询"},
]

3. 记忆(Memory)

Agent 的记忆系统分为两类:

短期记忆(Working Memory):

1
2
3
4
5
6
7
# 当前任务的上下文信息
context = {
"task": "分析销售数据",
"current_step": 2,
"completed_steps": ["数据加载", "数据清洗"],
"pending_steps": ["统计分析", "可视化"],
}

长期记忆(Long-term Memory):

1
2
3
4
5
6
# 持久化存储的经验和知识
memory_store = {
"user_preferences": {"format": "markdown"},
"past_tasks": [...],
"learned_patterns": [...],
}

4. 规划器(Planner)

规划器负责将复杂任务分解为可执行的步骤:

1
2
3
4
5
6
# ReAct 模式(思考 + 行动 + 观察)
def react_agent(task):
thought = llm.think(task, history)
action = llm.decide_action(thought)
observation = execute(action)
return react_agent(task, observation) # 递归直到完成

Agent 的工作模式

单 Agent 模式

一个 Agent 独立完成整个任务:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class SimpleAgent:
def __init__(self, llm, tools, memory):
self.llm = llm
self.tools = tools
self.memory = memory

def run(self, task):
# 理解任务
context = self.memory.get_context()

# 规划步骤
plan = self.llm.plan(task, context)

# 执行每一步
for step in plan:
result = self.execute_step(step)
context = self.update_context(context, result)

# 返回结果
return self.summarize(context)

多 Agent 协作模式

多个 Agent 分工协作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
用户请求


┌─────────┐
│ 调度员 │ ─── 分析任务,分发给专业 Agent
│ Agent │
└────┬────┘
│ 分配

┌─────────┐ ┌─────────┐ ┌─────────┐
│ 研究员 │ │ 程序员 │ │ 审查员 │
│ Agent │ │ Agent │ │ Agent │
└────┬────┘ └────┬────┘ └────┬────┘
│ │ │
▼ ▼ ▼
搜索资料 编写代码 检查质量
│ │ │
└─────────────┼─────────────┘


整合结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 多 Agent 协作示例
class MultiAgent:
def __init__(self):
self.coordinator = Agent(role="调度员")
self.researcher = Agent(role="研究员", tools=[web_search])
self.coder = Agent(role="程序员", tools=[execute_code])
self.reviewer = Agent(role="审查员")

def run(self, task):
# 1. 调度员分析任务
subtasks = self.coordinator.decompose(task)

# 2. 并行执行子任务
results = parallel(
self.researcher.run(subtasks["research"]),
self.coder.run(subtasks["code"]),
)

# 3. 审查员检查
review = self.reviewer.run(results)

# 4. 调度员整合输出
return self.coordinator.finalize(results, review)

ReAct 模式详解

ReAct(Reason + Act)是目前最流行的 Agent 推理模式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def react_loop(query, agent):
history = []

while not agent.finished():
# 1. Thought - 思考应该做什么
thought = agent.think(query, history)

# 2. Action - 执行动作
if thought.action == "search":
result = web_search(thought.query)
elif thought.action == "code":
result = execute_code(thought.code)
elif thought.action == "finish":
return thought.response

# 3. Observation - 观察结果
history.append({
"thought": thought,
"result": result
})

return "任务未能完成"

ReAct 的优势:

  • 可追溯每一步决策
  • 容易调试和修正
  • 适合复杂推理任务

工具定义规范

Agent 通过 Function Calling 与工具交互:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# OpenAI 风格的工具定义
tools = [
{
"type": "function",
"function": {
"name": "calculate",
"description": "执行数学计算",
"parameters": {
"type": "object",
"properties": {
"expression": {
"type": "string",
"description": "数学表达式,如 2+3*4"
}
},
"required": ["expression"]
}
}
},
{
"type": "function",
"function": {
"name": "get_weather",
"description": "查询城市天气",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名称"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "温度单位"
}
},
"required": ["city"]
}
}
}
]

# LLM 返回的工具调用
response = openai.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "北京的天气如何?"}],
tools=tools
)

# 解析返回的函数调用
tool_call = response.choices[0].message.tool_calls[0]
function_name = tool_call.function.name
arguments = json.loads(tool_call.function.arguments)

主流 Agent 框架

1. LangChain Agents

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from langchain.agents import AgentExecutor, create_openai_functions_agent
from langchain.tools import Tool
from langchain import hub

# 定义工具
tools = [
Tool(
name="Search",
func=search.run,
description="用于搜索最新信息"
),
Tool(
name="Calculator",
func=calculate.run,
description="用于数学计算"
)
]

# 创建 Agent
prompt = hub.pull("hwchase17/openai-functions-agent")
agent = create_openai_functions_agent(llm, tools, prompt)

# 执行
agent_executor = AgentExecutor(agent=agent, tools=tools)
result = agent_executor.invoke({"input": "查找 AI 领域的最新进展"})

2. LlamaIndex Agent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from llama_index.agent import OpenAIAgent
from llama_index.tools import QueryEngineTool

# 定义工具
tools = [
QueryEngineTool(
query_engine=query_engine,
metadata={
"name": "knowledge_base",
"description": "公司知识库,包含产品文档和技术规范"
}
)
]

# 创建 Agent
agent = OpenAIAgent.from_tools(tools)

# 对话
response = agent.chat("我的产品支持哪些特性?")

3. AutoGPT / GPT-Engineer

面向非技术用户的 Agent 平台,通过自然语言描述即可完成代码开发:

1
2
# AutoGPT 使用示例
python -m autogpt "帮我写一个待办事项 App"

4. CrewAI

专注多 Agent 协作的框架:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from crewai import Agent, Task, Crew

# 定义 Agent
researcher = Agent(
role="研究员",
goal="获取最准确的信息",
backstory="你是一名专业研究员,擅长信息检索和分析"
)

writer = Agent(
role="作家",
goal="用通俗易懂的语言撰写报告",
backstory="你是一名科普作家,擅长把复杂概念讲得简单"
)

# 定义任务
task1 = Task(description="研究 AI Agent 的最新发展", agent=researcher)
task2 = Task(description="写一篇关于 AI Agent 的科普文章", agent=writer)

# 启动协作
crew = Crew(agents=[researcher, writer], tasks=[task1, task2])
result = crew.kickoff()

Agent 的应用场景

1. 自动化办公

1
2
3
4
5
6
7
8
9
10
11
12
# 邮件处理 Agent
email_agent = Agent(
role="邮件助手",
tools=[read_email, send_email, calendar],
prompt="""
你是一个邮件助手,负责:
1. 读取重要邮件
2. 识别需要回复的邮件
3. 起草回复草稿
4. 根据用户确认发送邮件
"""
)

2. 代码开发助手

1
2
3
4
5
6
7
8
9
10
11
12
13
# 代码开发 Agent
dev_agent = Agent(
role="开发助手",
tools=[read_file, write_file, execute_code, git],
prompt="""
你是一个全栈开发助手,负责:
1. 理解需求
2. 编写代码
3. 编写测试
4. 调试和修复 bug
5. 提交代码到仓库
"""
)

3. 数据分析助手

1
2
3
4
5
6
7
8
9
10
11
12
13
# 数据分析 Agent
data_agent = Agent(
role="数据分析师",
tools=[query_db, execute_python, visualize],
prompt="""
你是一个数据分析专家,负责:
1. 理解业务问题
2. 编写 SQL 查询数据
3. 进行统计分析
4. 生成可视化图表
5. 撰写分析报告
"""
)

4. 客服机器人

1
2
3
4
5
6
7
8
9
10
11
12
13
# 智能客服 Agent
support_agent = Agent(
role="客服代表",
tools=[knowledge_base, order_system, refund_api],
prompt="""
你是一个耐心的客服代表,负责:
1. 理解用户问题
2. 在知识库中查找解决方案
3. 指导用户操作
4. 处理退款、换货等请求
5. 记录无法解决的问题以便人工跟进
"""
)

Agent 开发的最佳实践

1. 清晰的角色定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# ❌ 模糊的定义
agent = Agent(prompt="帮我做事")

# ✅ 清晰的定义
agent = Agent(
role="资深 Python 后端工程师",
goal="编写高质量、可维护的 RESTful API",
backstory="""
你有 10 年 Python 开发经验,
精通 FastAPI、PostgreSQL、Docker,
注重代码规范和性能优化,
习惯写详细的 docstring 和注释。
""",
tools=[read_code, write_code, run_test, docker_build]
)

2. 有限的工具数量

1
2
3
4
5
6
7
8
9
10
# ❌ 工具过多导致选择困难
tools = [100个工具...]

# ✅ 按功能分组,按需加载
class Agent:
def __init__(self, mode="development"):
if mode == "development":
self.tools = self.dev_tools # 10 个开发相关工具
elif mode == "analysis":
self.tools = self.analysis_tools # 8 个分析工具

3. 错误处理和重试

1
2
3
4
5
6
7
8
9
10
11
12
def execute_with_retry(agent, task, max_retries=3):
for attempt in range(max_retries):
try:
result = agent.run(task)
return result
except ToolExecutionError as e:
# 分析错误原因,尝试修正
correction = agent.reflect(f"执行失败: {e}")
task = agent.modify_task(task, correction)
except MaxStepsExceeded:
return "任务复杂度过高,请拆分为多个子任务"
return "执行多次仍未成功"

4. 安全的工具设计

1
2
3
4
5
6
7
8
9
10
11
12
13
# ❌ 危险:直接执行任意代码
def execute_code(code):
os.system(code) # 可能执行恶意命令

# ✅ 安全:使用沙箱环境
def execute_code(code):
# 只允许执行安全的代码片段
allowed_patterns = ["print(", "len(", "str(", "int("]
if not all(p in code for p in allowed_patterns):
raise SecurityError("禁止执行未授权代码")

# 在沙箱中执行
return sandbox.execute(code)

Agent 面临的挑战

1. 幻觉问题

Agent 可能自信地执行错误的操作:

1
2
3
4
5
6
7
8
9
# 风险场景
user: "帮我删除所有旧日志"
# Agent 可能误解为删除所有文件

# 解决方案:关键操作需要确认
def dangerous_action_guard(agent, action):
if action.is_dangerous:
return agent.request_confirmation(f"确认执行: {action}?")
return action.execute()

2. 任务分解的准确性

复杂任务需要正确的分解:

1
2
3
4
5
6
7
8
9
# 解决方案:让 Agent 自己验证分解是否完整
task = "分析本季度销售情况"
steps = agent.decompose(task)

# 验证步骤
validation = agent.validate_decomposition(task, steps)
if not validation.is_complete:
# 补充遗漏步骤
steps = agent.refine_decomposition(steps, validation.gaps)

3. 工具调用的可靠性

1
2
3
4
5
6
7
8
9
10
# 解决方案:超时处理和备用方案
def reliable_tool_call(tool, args, timeout=5):
try:
return asyncio.wait_for(tool(**args), timeout)
except asyncio.TimeoutError:
logger.warning(f"{tool.name} 执行超时,尝试备用方案")
return fallback_solution(tool.name)
except Exception as e:
logger.error(f"{tool.name} 执行失败: {e}")
return None

4. 长期记忆的管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 解决方案:定期总结和压缩记忆
class MemoryManager:
def __init__(self, max_size=1000):
self.short_term = []
self.long_term = []
self.max_size = max_size

def add(self, experience):
self.short_term.append(experience)

# 超过容量时压缩
if len(self.short_term) > self.max_size:
summary = self.summarize(self.short_term)
self.long_term.append(summary)
self.short_term = []

未来展望

AI Agent 正在快速发展,以下是几个值得关注的方向:

  1. 多模态 Agent:能够处理文本、图像、音频、视频等多种信息
  2. 自主学习:Agent 能够从交互中持续学习和改进
  3. 多 Agent 社会:多个 Agent 组成协作网络,各司其职
  4. 具身智能:Agent 与物理世界交互(机器人)
  5. 安全对齐:确保 Agent 行为符合人类意图和价值观

总结

AI Agent 代表了人工智能从「回答问题」到「解决问题」的重大跨越。通过结合 LLM 的理解能力、工具的执行能力、规划器的推理能力和记忆系统的经验积累,Agent 能够自主完成越来越复杂的任务。

掌握 Agent 的核心概念、熟悉主流框架、了解最佳实践和潜在挑战,将帮助我们更好地构建和应用这些智能系统。


欢迎留言讨论!