当前位置: 首页 > news >正文

Agent 结构(LLM + Tool + Executor)

day29:理解Agent 结构(LLM + Tool + Executor)

一、Agent定义 简单介绍

  • Agent = 能“思考 → 决策 → 调用工具 → 再思考”的 LLM 程序

公式化一点就是:

Agent = LLM + Tools + Executor

它和「问 → 答」最大的区别是:

LLM 不再只是生成文本,而是在“做事”

二、Agent 结构总览

┌─────────────────────┐ │ User │ └─────────┬───────────┘ │ ┌─────────▼───────────┐ │ Agent │ │ │ │ ┌──────────────┐ │ │ │ LLM │ │ ← 决策中枢(大脑) │ └──────┬───────┘ │ │ │ Thought │ │ ┌──────▼───────┐ │ │ │ Tools │ │ ← 外部能力 │ └──────┬───────┘ │ │ │ Action │ │ ┌──────▼───────┐ │ │ │ Executor │ │ ← 执行与控制循环 │ └──────────────┘ │ └─────────────────────┘

三、Agent 的三个核心组件(重点)

1️⃣ LLM(大脑)

在 Agent 中,LLM 不只是“回答问题”

它负责:

  • 🤔 思考(Thought)

  • 🧭 决策(是否用工具)

  • 🧠 规划(先做什么,再做什么)

典型提示词结构(你不写,LangChain 会自动帮你写):

Thought: 我需要查询时间 Action: get_current_time Action Input: {} Observation: 2025-12-13 Thought: 我可以回答了 Final Answer: 现在是 2025-12-13
  • 👉 Agent 的本质是让 LLM 输出“结构化思考过程”

2️⃣ Tool(工具)

Tool 是什么?

Tool = Agent 能调用的 Python 函数

例如:

- 搜索 - 计算 - 查数据库 - 调 API - 查文件 - 调你写的业务函数

在 LangChain 中,一个 Tool 至少包含:

name descriptioncallablefunction

LLM 通过 description 来判断“该不该用这个工具”


3️⃣ Executor(执行器)

  • Executor 是 Agent 的“循环控制器”

它负责:

  • 把 LLM 输出解析成:

    • Thought

    • Action

    • Action Input

  • 调用 Tool

  • 把结果塞回给 LLM

  • 再让 LLM 思考

  • 直到得到 Final Answer

可以理解为:

Executor = Agent 的 runtime

四、LangChain 中的 Agent 类型(认识不分)

✅ ReAct Agent(最重要)

ReAct = Reason + Act

Thought → Action → Observation → Thought → Final

👉 这是 LangChain 默认 & 最稳定 & 最好理解 的 Agent 结构
后面你学的基本都是它的变体。


五、从 0 到 1:一个最小 LangChain Agent Demo(Qwen-Plus)

Step 0:安装依赖

langchain>=0.1.0langchain-openai langchain_classic pip install-r requirements.txt
importdatetimefromlangchain_classic.agentsimportinitialize_agent,AgentTypefromlangchain_core.toolsimporttoolfromlangchain_openaiimportChatOpenAI llm=ChatOpenAI(model="qwen-plus-latest",temperature=0,api_key="sk-YOUR-API-KEY",base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",)@tooldefget_current_time(query:str)->str:"""获取当前时间"""returnstr(datetime.datetime.now())tools=[get_current_time]agent=initialize_agent(tools,llm,agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,verbose=True,handle_parsing_errors=True)defmain():res=agent.invoke({"现在是什么时间?"})print(res["output"])if__name__=="__main__":main()# (day29venv) PS E:\code\xsun_ai_study\week5\day29> python main.py# E:\code\xsun_ai_study\week5\day29\main.py:23: LangChainDeprecationWarning: LangChain agents will continue to be supported, but it is recommended for new use cases to be built with LangGraph. LangGraph offers a more flexible and full-featured framework for building agents, including support for tool-calling, persistence of state, and human-in-the-loop workflows. For details, refer to the [LangGraph documentation](https://langchain-ai.github.io/langgraph/) as well as guides for [Migrating from AgentExecutor](https://python.langchain.com/docs/how_to/migrate_agent/) and LangGraph's [Pre-built ReAct agent](https://langchain-ai.github.io/langgraph/how-tos/create-react-agent/).# agent = initialize_agent(tools,### > Entering new AgentExecutor chain...# 需要获取当前时间# Action: get_current_time# Action Input: {"query": "current time"}# Observation: 2025-12-14 14:52:24.744312# Thought:Final Answer: 现在是2025年12月14日14点52分24秒。## > Finished chain.# 现在是2025年12月14日14点52分24秒。

你会看到完整的 Agent 思考链:

Thought: 我需要知道当前时间 Action: get_current_time Action Input: {} Observation: 2025-12-13 10:32:11 Thought: 我已经知道时间了 Final Answer: 现在是 2025-12-13 10:32:11

六、这个 Demo 背后发生了什么(非常重要)

组件做了什么
LLMQwen-Plus
ToolPython 函数
AgentReAct 推理模板
Executor控制调用循环

👉 这是 90% LangChain Agent 的通用骨架

七、Agent 和你之前 RAG 的关系

RAGAgent
查资料 → 回答思考 → 决策 → 行动
单次生成多轮内部循环
被动主动
无状态或弱状态可引入 Memory

👉 Agent 可以“用 RAG 作为工具”

http://www.gsyq.cn/news/98648.html

相关文章:

  • 红米10x将一键清理和锁屏加到桌面步骤
  • 台达DVPEH3系列PLC与欧姆龙E5CC温控器通讯及控制实现
  • 192KHz 双声道输入 24 位 AD 转换器国产品牌DP8340兼容CS5340
  • Cameralink采集卡软件EspeedGrab使用讲解:3 保存采集参数
  • XPM与IP模式下FIFO的比较
  • MySQL数据处理(增删改)
  • 电科毕设 stm32 wifi远程可视化与农业灌溉系统(源码+硬件+论文)
  • 55、Ubuntu 系统软件管理全攻略
  • 34、Bash脚本中的循环控制与故障排查
  • 一文搞懂CNN - LSTM - Attention回归预测:新手友好实战
  • Kubernetes Node 管理完全指南:从入门到生产实践
  • const引用
  • 36、脚本编程中的参数、循环与数据处理
  • Cameralink采集卡软件EspeedGrab使用讲解:4图像处理
  • 【思维模型】第一性原理 ③ ( 5 Why 分析法 | 明确问题 | 层层深入 | 验证原因 | 改进措施 )
  • 台达DVPES2系列PLC与欧姆龙E5CC温控器通讯实现温控
  • 光伏MPPT仿真-固定电压法+扰动观察法+电导增量法 模型版本:有两个版本2015a和2022...
  • vue基于Spring Boot框架的心理咨询预约管理平台的设计与实现_2uab9a6h
  • 2025 年 AI 资讯公众号平台推荐榜单,精准筛选,助你高效获取 AI 行业核心信息 - 品牌鉴赏师
  • C++类和对象--访问限定符与封装-类的实例化与对象模型-this指针(二)
  • Flink SQL Time Travel用 FOR SYSTEM_TIME AS OF 查询历史快照
  • 31、脚本编程进阶:Here文档、自上而下设计与流程控制
  • vue基于Spring Boot框架的数字乡村旅游景点预约平台的设计与实现_ax346a6i
  • AI:深度学习中反向传播中的链式法则和梯度
  • 英语_阅读_2019 Young Scientist Challenge_待读
  • 计算机毕业设计springboot高考志愿智能推荐系统 基于SpringBoot的考后择校智慧匹配平台 面向新高考的SpringBoot个性化志愿辅助决策系统
  • 29、《pkg-config与GNU Autotools使用指南》
  • 计算机毕业设计springboot汽车智慧检修系统 基于SpringBoot的智能汽车故障预测与维修管理平台 融合IoT的SpringBoot车辆健康监测与维修决策系统
  • 题目集 4~5 总结性 Blog
  • 你写的不是代码,是生存的底气|从“制造思维”到“生长思维”的范式革命