news 2026/4/16 11:05:28

Agent 结构(LLM + Tool + Executor)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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 作为工具”

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 11:01:21

Tarjan全家桶系列--割点

割点定义 在无向图G(V,E)中,如果一个节点u满足:删除u以及与u相关联的所有边后,图的连通分量数量增加,则称u为割点。 核心思想 Tarjan算法仍然基于深度优先搜索(DFS),利用两个关键数组&#xff1…

作者头像 李华
网站建设 2026/4/14 20:49:20

台达DVPES2系列PLC与欧姆龙E5CC温控器通讯实现温控

台达DVPES2系列PLC与3台欧姆龙E5CC温控器通讯程序(TDES-7) 功能:采用台达DVPES2型号PLC,对3台欧姆龙E5CC温控器通过485方式,modbus协议,进行温度的设定,实际温度读取硬件:台达DVP24ES2系列PLC,欧…

作者头像 李华
网站建设 2026/4/15 13:58:56

Flink SQL Time Travel用 FOR SYSTEM_TIME AS OF 查询历史快照

1. Time Travel 是什么,能解决什么问题 Time Travel(时间旅行)用于查询表在某个历史时间点的“数据与表结构状态”。你可以指定一个时间点,让 Flink 返回该时间点对应的表数据,适合做: 历史对账、回溯分析…

作者头像 李华
网站建设 2026/4/16 9:15:36

36、脚本编程中的参数、循环与数据处理

脚本编程中的参数、循环与数据处理 1. 位置参数 位置参数在脚本编程中是非常重要的概念,它们允许我们在执行脚本时传递参数。以下是不同形式的位置参数及其结果: | 形式 | 结果 | | ---- | ---- | | $1 = word $2 = words $3 = with $4 = spaces | 原始位置参…

作者头像 李华
网站建设 2026/4/7 9:16:53

Arduino UNO Q 烘托圣诞节气氛

本示例将传统LED控制升级为沉浸式节日体验,基于Arduino UNO Q开发。系统包含交互式圣诞树、音乐播放器和实时视觉反馈。通过简单的网络用户界面来切换板载 LED 的状态。应用程序通过网络浏览器监听用户输入并相应地更新 LED 状态。它展示了如何在 Linux 环境中与硬件…

作者头像 李华
网站建设 2026/4/14 20:16:50

【思维模型】第一性原理 ③ ( 5 Why 分析法 | 明确问题 | 层层深入 | 验证原因 | 改进措施 )

文章目录一、5 Why 分析法1、概念简介2、核心原则3、实施步骤4、关键技巧5、常见误区6、案例分析在 【思维模型】第一性原理 ② ( 利用 “ 第一性原理 “ 进行创新 : 归零 -> 解构 -> 重构 | 跨学科学习 ) 博客中 , 屡次提到了 5 Why 分析法 , 本篇博客简…

作者头像 李华