AI Agent 完全拆解:从原理到架构到实战,一次讲透(3)

发布时间:2026/5/26 6:51:20

AI Agent 完全拆解:从原理到架构到实战,一次讲透(3) 第三部分:Agent 类型大全3.1 单 Agent 架构最简单的架构,但也最常用。用户 → 一个 Agent(LLM + Tools + Memory)→ 结果工作方式:一个 Agent 负责全部事情从理解需求到执行到出结果,都是它所有工具对它可用,它自己决定用什么、怎么用适用场景:任务复杂度不高(一个 Agent 能搞定)不需要分工协作典型的编码助手、个人助理优点:架构简单,维护成本低延迟最小(不需要 Agent 间通信)上下文一致(所有信息在一个上下文里)缺点:单个 Agent 能力有限(受限于一个上下文窗口)工具多容易混淆(太多工具时 LLM 选择困难)没有冗余(Agent 崩溃 = 全部崩溃)代码示例:class SingleAgent: def __init__(self, llm, tools: List[Tool], system_prompt: str): self.llm = llm self.tools = {t.name: t for t in tools} self.system_prompt = system_prompt self.memory = [{"role": "system", "content": system_prompt}] def run(self, user_input: str) - str: self.memory.append({"role": "user", "content": user_input}) while True: response = self.llm.chat( self.memory, tools=[t.to_openai_format() for t in self.tools.values()] ) if response.function_call: tool_name = response.function_call.name tool_args = json.loads(response.function_call.arguments) self.memory.append(response.message) result = self.tools[tool_name].fn(**tool_args)

相关新闻