外观
LLM Agent 架构设计与实现指南
内容整理自学习笔记,仅供面试备考参考;不构成录用、培训或考试承诺。
1. 什么是 LLM Agent?
LLM Agent 是一种超越简单文本生成的人工智能系统,使用大型语言模型(LLM)作为核心计算引擎,使其能够进行对话、执行任务、推理并展现一定程度的自主性。
简而言之,Agent 是一个具有复杂推理能力、记忆机制和执行任务手段的系统。
2. LLM Agent 的核心组成
在 LLM 赋能的自主 Agent 系统中,LLM 充当"大脑"角色,并与以下关键组件协作:
┌─────────────────────────────────────────┐
│ LLM Agent │
│ ┌─────────┐ ┌─────────┐ ┌───────────┐ │
│ │ Planning│ │ Memory │ │ Tool Use │ │
│ │ (规划) │ │ (记忆) │ │ (工具使用) │ │
│ └─────────┘ └─────────┘ └───────────┘ │
└─────────────────────────────────────────┘2.1 规划(Planning)
复杂任务通常包含多个步骤,Agent 需要提前规划。
2.1.1 任务分解方法
| 方法 | 核心思想 | 特点 |
|---|---|---|
| Chain of Thought (CoT) | "think step by step",逐步推理 | 将大任务转化为可管理的子任务 |
| Tree of Thoughts (ToT) | 每步探索多种推理可能性 | 树形结构,支持 BFS/DFS 搜索 |
| Graph of Thoughts (GoT) | 支持多链、树形及任意图形结构 | 支持聚合、回溯、循环等操作 |
| LLM+P | LLM + 经典 Planner(PDDL) | 规划步骤外包给外部工具 |
提示: CoT 和 ToT 本质上都是通过精心设计的 Prompt 激发模型的 Metacognition 能力。
任务分解的常用指令方式:
python
# 方式一:简单提示
"Steps for XYZ.\n1."
# 方式二:针对具体任务的指令
"Write a story outline."
# 方式三:用户直接指定子目标2.1.2 模型自我反省(Self-Reflection)
Agent 能够对过去的 actions 进行自我批评和反思,从错误中学习并改进。
| 方法 | 说明 |
|---|---|
| ReAct | Reasoning + Acting,将推理和行动在 LLM 内部整合 |
| Reflexion | 让 Agent 具备动态记忆和自我反思能力的框架,采用标准 RL 设置 |
ReAct 的提示词模板格式:
Thought: ...
Action: ...
Observation: ...实验结论: 在知识密集型任务(HotpotQA)和决策型任务(AlfWorld Env)中,ReAct 的表现优于去掉 Thought 的纯 Action 方式。
2.2 记忆(Memory)
| 记忆类型 | 说明 | 实现方式 |
|---|---|---|
| 短期记忆 | 上下文学习,利用模型的短期记忆学习 | Prompt 上下文窗口 |
| 长期记忆 | 保留和召回长期信息 | 外部向量存储 + 检索 |
2.3 工具使用(Tool Use)
Agent 调用外部 API 获取模型权重中缺失的信息,包括:
- 当前实时信息
- 代码执行能力
- 专有信息源的访问
3. LLM Agent 利用的大模型能力
LLM Agent 主要利用大模型的以下能力:
- 推理(Reasoning)
- 模仿(Few-shot Learning)
- 规划(Planning)
- 结合**函数调用(Function Calling)**实现工具使用
4. 代码实战:LLM Agent 实现
4.1 实例一:多选判断
让大模型从多个选项中选出正确的工具:
python
multi_choice_prompt = """请针对 >>> 和 <<< 中间的用户问题,选择一个适合的工具。
>>> {question} <<<
你能使用的工具如下:
A. 一个能够查询商品信息为用户进行商品导购的工具
B. 一个能够查询最近下单的订单信息的工具
C. 一个能够查询商家的退换货政策的工具
D. 都不适合
请按以下格式进行回答`A`、`B`、`C`、`D`。
"""4.2 实例二:函数工具选择与格式化输出
将 A/B/C/D 替换为函数名,要求固定 JSON 格式输出:
python
tool_prompt = """请按以下格式进行回答:
{
"recommend_product": "一个为用户进行商品推荐导购的工具"
}
"""
# 解析时通过正则提取 JSON
pattern = re.compile(r"^.*?`{3}(?:json)?\n(.*?)`{3}.*?$", re.DOTALL)4.3 实例三:Chat Zero-Shot ReAct Agent
标准 Agent 模板使用经典的 Thought-Action-Observation 三段式:
python
agent_prompt = """Answer the following questions as best you can.
You have access to the following tools:
Search Order: 查询订单信息的工具,参数是输入订单id
Recommend product: 基于用户信息进行商品推荐的工具
The way you use the tools is by specifying a json blob:{ "action": $TOOL_NAME, "action_input": $INPUT }
ALWAYS use the following format:
Question: the input question you must answer
Thought: you should always think about what to do
Action:$JSON_BLOB
Observation: the result of the action
... (this Thought/Action/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question
"""4.4 实例四:接入 LangChain 测试 Agent
python
from langchain.agents import initialize_agent, AgentType
from langchain.agents import Tool
# 定义工具函数
def search_order(input: str) -> str:
return "{order},订单状态:已发货".format(order=input)
def recommend_product(input: str) -> str:
return "黑色连衣裙"
tools = [
Tool(name="Search Order", func=search_order,
description="查询订单信息的工具"),
Tool(name="Recommend product", func=recommend_product,
description="基于用户信息进行商品推荐的工具")
]
# 初始化 Agent
agent = initialize_agent(
tools=tools,
llm=chat,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True
)
result = agent.run("我想买一件衣服,有什么好推荐吗")
# 输出:黑色连衣裙5. 如何给 LLM 注入领域知识?
| 方法 | 说明 |
|---|---|
| 检索 + LLM(RAG) | 先在领域数据库中检索候选答案,再用 LLM 加工 |
| 领域知识微调(SFT) | 将领域知识构建为问答数据集,通过 SFT 让 LLM 学习 |
6. 常见 LLM Agent 框架
| 框架 | 地址 |
|---|---|
| AutoGPT | https://github.com/Significant-Gravitas/AutoGPT |
| AutoGen (Microsoft) | https://github.com/microsoft/autogen |
| ChatDev | https://github.com/OpenBMB/ChatDev |
| XAgent | https://github.com/OpenBMB/XAgent |
| GPT-engineer | https://github.com/gpt-engineer-org/gpt-engineer |