LLM 作为智能体大脑
约 1470 字大约 5 分钟
Agent虾学智能体入门
2026-03-08
LLM 作为智能体大脑
本系列第三篇,深入讲解大语言模型(LLM)如何成为智能体的核心推理引擎。
为什么 LLM 适合做智能体大脑?
传统智能体需要人工设计复杂的规则和状态机,而大语言模型(LLM)凭借其强大的理解、推理和生成能力,成为现代智能体的理想"大脑"。
LLM 的三大核心能力
| 能力 | 描述 | 在 Agent 中的应用 |
|---|---|---|
| 理解 | 理解自然语言指令 | 解析用户意图 |
| 推理 | 逻辑推理和规划 | 任务分解、决策 |
| 生成 | 生成文本和代码 | 回复生成、工具调用 |
LLM vs 传统方法
传统智能体:
规则 + 状态机 + 知识库 → 人工设计 → 有限场景
LLM Agent:
LLM + 提示词 + 工具 → 自然交互 → 无限场景LLM Agent 的核心架构
┌─────────────────────────────────────────────┐
│ LLM Agent │
│ │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ Prompt │ → │ LLM │ → │ Output │ │
│ │ System │ │ Model │ │ Parser │ │
│ └─────────┘ └─────────┘ └─────────┘ │
│ ↑ ↑ │ │
│ │ │ │ │
│ ┌────┴────┐ ┌────┴────┐ ┌────┴────┐ │
│ │ Context │ │ Memory │ │ Tools │ │
│ │ 上下文 │ │ 记忆 │ │ 工具 │ │
│ └─────────┘ └─────────┘ └─────────┘ │
│ │
└─────────────────────────────────────────────┘核心组件
- System Prompt - 定义智能体的角色和行为规范
- Context - 当前对话和任务的上下文信息
- Memory - 短期和长期记忆存储
- Tools - 可调用的外部工具和 API
- Output Parser - 解析 LLM 输出为结构化数据
Prompt Engineering for Agents
System Prompt 模板
SYSTEM_PROMPT = """
你是一个智能助手,能够帮助用户完成各种任务。
## 角色定义
- 你是一个专业、友好的助手
- 你可以使用工具来完成任务
- 当不确定时,主动询问澄清
## 可用工具
{tools_description}
## 工作流程
1. 理解用户意图
2. 判断是否需要使用工具
3. 如果需要,调用相应工具
4. 整合信息,给出回复
## 输出格式
当需要调用工具时,使用以下格式:
```json
{
"thought": "你的思考过程",
"action": "工具名称",
"action_input": {"参数名": "参数值"}
}当直接回复时:
{
"thought": "你的思考过程",
"answer": "你的回复内容"
}注意事项
- 一次只调用一个工具
- 等待工具返回结果后再决定下一步
- 如果工具调用失败,尝试其他方案 """
### Few-Shot 示例
```python
FEW_SHOT_EXAMPLES = """
## 示例 1:查询天气
用户:北京今天天气怎么样?
助手思考:
```json
{
"thought": "用户想查询北京天气,我需要使用天气工具",
"action": "get_weather",
"action_input": {"city": "北京"}
}工具返回:
助手回复:
{
"thought": "已获取天气信息,可以回答用户",
"answer": "北京今天天气晴朗,温度25°C,适合外出活动。"
}示例 2:数学计算
用户:帮我算一下 123 * 456
助手思考:
{
"thought": "用户需要数学计算,使用计算器工具",
"action": "calculator",
"action_input": {"expression": "123 * 456"}
}工具返回:56088
助手回复:
{
"thought": "计算完成",
"answer": "123 × 456 = 56088"
}"""
---
## ReAct 模式
**ReAct**(Reasoning + Acting)是目前最流行的 LLM Agent 模式,它将**推理**和**行动**交织在一起。
### ReAct 流程Thought → Action → Observation → Thought → ...
### 代码实现
```python
from typing import List, Optional
import json
class ReActAgent:
"""基于 ReAct 模式的智能体"""
def __init__(self, llm, tools: dict):
self.llm = llm
self.tools = tools
self.history = []
def run(self, question: str, max_iterations: int = 5) -> str:
"""运行 ReAct 循环"""
for i in range(max_iterations):
# 构建提示词
prompt = self._build_prompt(question)
# 调用 LLM
response = self.llm.generate(prompt)
# 解析响应
thought, action, action_input = self._parse_response(response)
print(f"[Thought] {thought}")
if action is None:
# 直接回答
return thought
print(f"[Action] {action}({action_input})")
# 执行工具
observation = self._execute_tool(action, action_input)
print(f"[Observation] {observation}")
# 记录历史
self.history.append({
"thought": thought,
"action": action,
"action_input": action_input,
"observation": observation
})
return "达到最大迭代次数,未能完成任务"
def _build_prompt(self, question: str) -> str:
"""构建 ReAct 提示词"""
history_str = "\n".join([
f"Thought: {h['thought']}\n"
f"Action: {h['action']}\n"
f"Action Input: {h['action_input']}\n"
f"Observation: {h['observation']}"
for h in self.history
])
return f"""
Answer the following question using the ReAct format:
Question: {question}
{history_str}
Available tools: {list(self.tools.keys())}
Use this format:
Thought: [your reasoning]
Action: [tool name or "Finish"]
Action Input: [tool input or final answer]
"""
def _parse_response(self, response: str):
"""解析 LLM 响应"""
thought = None
action = None
action_input = None
for line in response.split("\n"):
if line.startswith("Thought:"):
thought = line.replace("Thought:", "").strip()
elif line.startswith("Action:"):
action = line.replace("Action:", "").strip()
elif line.startswith("Action Input:"):
action_input = line.replace("Action Input:", "").strip()
# 如果 Action 是 Finish,说明是最终答案
if action == "Finish":
return action_input, None, None
try:
action_input = json.loads(action_input)
except:
pass
return thought, action, action_input
def _execute_tool(self, action: str, action_input: dict) -> str:
"""执行工具"""
tool = self.tools.get(action)
if tool:
return str(tool(action_input))
return f"Unknown tool: {action}"
# 使用示例
def weather_tool(args):
return f"Weather in {args.get('city')}: Sunny, 25°C"
# agent = ReActAgent(
# llm=OpenAI(),
# tools={"get_weather": weather_tool}
# )
# result = agent.run("北京今天天气怎么样?")主流 LLM Agent 框架
LangChain
from langchain.agents import initialize_agent, AgentType
from langchain.tools import Tool
from langchain_openai import ChatOpenAI
# 定义工具
tools = [
Tool(
name="Calculator",
func=lambda x: eval(x),
description="用于数学计算"
)
]
# 创建 Agent
llm = ChatOpenAI(model="gpt-4")
agent = initialize_agent(
tools=tools,
llm=llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION
)
# 运行
result = agent.run("123 * 456 是多少?")AutoGPT 模式
class AutoGPTStyleAgent:
"""AutoGPT 风格的自主 Agent"""
def __init__(self, llm, goal: str):
self.llm = llm
self.goal = goal
self.memory = []
self.tasks = []
def run(self):
"""自主运行直到达成目标"""
while not self._is_goal_achieved():
# 1. 思考下一步
next_action = self._think()
# 2. 执行行动
result = self._execute(next_action)
# 3. 更新记忆
self.memory.append({
"action": next_action,
"result": result
})
# 4. 评估进度
self._evaluate_progress()
def _think(self) -> dict:
"""思考下一步行动"""
prompt = f"""
Goal: {self.goal}
Memory: {self.memory[-5:] if self.memory else []}
What should I do next? Return a JSON with:
- thought: your reasoning
- action: the action to take
- action_input: parameters for the action
"""
return self.llm.generate_json(prompt)
def _is_goal_achieved(self) -> bool:
"""检查目标是否达成"""
prompt = f"Has the goal '{self.goal}' been achieved? Answer yes or no."
return "yes" in self.llm.generate(prompt).lower()模型选择指南
| 模型 | 推理能力 | 工具调用 | 成本 | 适用场景 |
|---|---|---|---|---|
| GPT-4 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ | 💰💰💰 | 复杂任务 |
| Claude 3.5 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ | 💰💰💰 | 长文本、编码 |
| GPT-3.5 | ⭐⭐⭐ | ⭐⭐⭐⭐ | 💰 | 简单任务 |
| Gemini Flash | ⭐⭐⭐⭐ | ⭐⭐⭐⭐ | 💰 | 平衡选择 |
| 本地模型 | ⭐⭐ | ⭐⭐ | 免费 | 隐私敏感 |
小结
- LLM 凭借理解、推理、生成能力,成为智能体的理想"大脑"
- ReAct 模式将推理和行动交织,是目前主流的 Agent 模式
- 选择合适的模型和框架,根据任务复杂度和成本权衡
下一篇
工具调用机制 - 深入理解 Function Calling