Hello Agents进阶篇学习笔记Agent核心目标Agent主要问题经典Agent范式ReAct系统结构总结ReAct代码结构总结ReAct Agent 核心实现Agent核心目标把LLM升级成Agent 从静态只能回答问题到动态理解任务调用工具动态决策所以Agent能力可以总结为Agent LLM推理能力Tools外部能力控制逻辑循环决策Agent主要问题1.幻觉-LLM生成无关内容2.死循环-无限思考3.工具误用-胡乱调用所以需要设计架构经典Agent范式ReactReasonActIdea边想边做循环结构(Thought-Action-Observation)-~Plan and SolveIdea先规划再执行结构Plan-Step1-Step2-Step3ReflectionIdea:做完之后反思结构Answer-Critique-ImproveReAct系统结构总结用户问题-ReactAgent-LLM(决策)-ToolExecutor工具调度-外部工具search API-结果返回-~(循环/结束)ReAct代码结构总结LLM封装流程调用OpenAI API使用stream流式输出defthink(self,messages):forchunkinresponse:contentchunk.choices[0].delta.contentTool工具设计一个工具包括NameDescriuptionFunctiondefsearch(query):流程调 SerpApi获取 JSON 结果智能解析ToolExecutor工具管理器作用是管理多个工具self.tools{name:{description:~,func:~}}方法作用registerTool注册工具getTool获取函数getAvailableTools给 LLM 的说明description是给LLM的prompt。ReAct Agent 核心实现Prompt 模板Thought:~Action:~Action格式如下即协议ProtocolSearch[query]Finish[answer]主循环核心逻辑主循环whilestepmax_steps:构造promptpromptTEMPLATE.format(...)调用LLMresponsellm.think()解析输出thought,actionparse()执行动作ifFinish:returnanswerelse:call tool记录Historyself.history.append(...)注意LLM每一步都基于历史推理即history~memory输出解析Parser