01 agent基础

Agent 是什么

一、定义

Agent = 大模型 + 工具 + 循环决策

二、Agent 流程

复制代码
用户提问 → 模型思考 → 决定要不要用工具 → 执行工具 → 看结果 → 再思考 → ... → 最终回答

三、Agent 的完整架构(五个组件)

#mermaid-svg-yLL8bgnyQguMxSlW{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-yLL8bgnyQguMxSlW .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-yLL8bgnyQguMxSlW .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-yLL8bgnyQguMxSlW .error-icon{fill:#552222;}#mermaid-svg-yLL8bgnyQguMxSlW .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-yLL8bgnyQguMxSlW .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-yLL8bgnyQguMxSlW .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-yLL8bgnyQguMxSlW .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-yLL8bgnyQguMxSlW .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-yLL8bgnyQguMxSlW .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-yLL8bgnyQguMxSlW .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-yLL8bgnyQguMxSlW .marker{fill:#333333;stroke:#333333;}#mermaid-svg-yLL8bgnyQguMxSlW .marker.cross{stroke:#333333;}#mermaid-svg-yLL8bgnyQguMxSlW svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-yLL8bgnyQguMxSlW p{margin:0;}#mermaid-svg-yLL8bgnyQguMxSlW .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-yLL8bgnyQguMxSlW .cluster-label text{fill:#333;}#mermaid-svg-yLL8bgnyQguMxSlW .cluster-label span{color:#333;}#mermaid-svg-yLL8bgnyQguMxSlW .cluster-label span p{background-color:transparent;}#mermaid-svg-yLL8bgnyQguMxSlW .label text,#mermaid-svg-yLL8bgnyQguMxSlW span{fill:#333;color:#333;}#mermaid-svg-yLL8bgnyQguMxSlW .node rect,#mermaid-svg-yLL8bgnyQguMxSlW .node circle,#mermaid-svg-yLL8bgnyQguMxSlW .node ellipse,#mermaid-svg-yLL8bgnyQguMxSlW .node polygon,#mermaid-svg-yLL8bgnyQguMxSlW .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-yLL8bgnyQguMxSlW .rough-node .label text,#mermaid-svg-yLL8bgnyQguMxSlW .node .label text,#mermaid-svg-yLL8bgnyQguMxSlW .image-shape .label,#mermaid-svg-yLL8bgnyQguMxSlW .icon-shape .label{text-anchor:middle;}#mermaid-svg-yLL8bgnyQguMxSlW .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-yLL8bgnyQguMxSlW .rough-node .label,#mermaid-svg-yLL8bgnyQguMxSlW .node .label,#mermaid-svg-yLL8bgnyQguMxSlW .image-shape .label,#mermaid-svg-yLL8bgnyQguMxSlW .icon-shape .label{text-align:center;}#mermaid-svg-yLL8bgnyQguMxSlW .node.clickable{cursor:pointer;}#mermaid-svg-yLL8bgnyQguMxSlW .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-yLL8bgnyQguMxSlW .arrowheadPath{fill:#333333;}#mermaid-svg-yLL8bgnyQguMxSlW .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-yLL8bgnyQguMxSlW .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-yLL8bgnyQguMxSlW .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-yLL8bgnyQguMxSlW .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-yLL8bgnyQguMxSlW .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-yLL8bgnyQguMxSlW .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-yLL8bgnyQguMxSlW .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-yLL8bgnyQguMxSlW .cluster text{fill:#333;}#mermaid-svg-yLL8bgnyQguMxSlW .cluster span{color:#333;}#mermaid-svg-yLL8bgnyQguMxSlW div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-yLL8bgnyQguMxSlW .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-yLL8bgnyQguMxSlW rect.text{fill:none;stroke-width:0;}#mermaid-svg-yLL8bgnyQguMxSlW .icon-shape,#mermaid-svg-yLL8bgnyQguMxSlW .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-yLL8bgnyQguMxSlW .icon-shape p,#mermaid-svg-yLL8bgnyQguMxSlW .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-yLL8bgnyQguMxSlW .icon-shape .label rect,#mermaid-svg-yLL8bgnyQguMxSlW .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-yLL8bgnyQguMxSlW .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-yLL8bgnyQguMxSlW .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-yLL8bgnyQguMxSlW :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 用户
Agent 主循环
LLM

大脑/决策者
Tools

手/能力
Memory

记忆
Prompt

规则/人设
外部世界

API/数据库/文件

组件 作用
LLM 理解任务、决定下一步
Tools 执行具体动作
Memory 记住对话历史、长期知识
Prompt 定义角色、规则、输出格式
Loop 串联以上组件,循环直到完成

四、ReAct 是什么?

ReAct = Reason(推理)+ Act(行动),目前最常见的 Agent 模式。

复制代码
Thought(思考): 用户问北京天气,我需要查天气工具
Action(行动):  调用 get_weather(city="北京")
Observation(观察): 工具返回 "晴,25℃"
Thought: 我已经有数据了,可以回答了
Action:  输出最终答案给用户

五、Tool Loop(工具循环)

#mermaid-svg-a5FoGvwHIRrd46wN{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-a5FoGvwHIRrd46wN .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-a5FoGvwHIRrd46wN .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-a5FoGvwHIRrd46wN .error-icon{fill:#552222;}#mermaid-svg-a5FoGvwHIRrd46wN .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-a5FoGvwHIRrd46wN .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-a5FoGvwHIRrd46wN .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-a5FoGvwHIRrd46wN .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-a5FoGvwHIRrd46wN .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-a5FoGvwHIRrd46wN .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-a5FoGvwHIRrd46wN .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-a5FoGvwHIRrd46wN .marker{fill:#333333;stroke:#333333;}#mermaid-svg-a5FoGvwHIRrd46wN .marker.cross{stroke:#333333;}#mermaid-svg-a5FoGvwHIRrd46wN svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-a5FoGvwHIRrd46wN p{margin:0;}#mermaid-svg-a5FoGvwHIRrd46wN .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-a5FoGvwHIRrd46wN .cluster-label text{fill:#333;}#mermaid-svg-a5FoGvwHIRrd46wN .cluster-label span{color:#333;}#mermaid-svg-a5FoGvwHIRrd46wN .cluster-label span p{background-color:transparent;}#mermaid-svg-a5FoGvwHIRrd46wN .label text,#mermaid-svg-a5FoGvwHIRrd46wN span{fill:#333;color:#333;}#mermaid-svg-a5FoGvwHIRrd46wN .node rect,#mermaid-svg-a5FoGvwHIRrd46wN .node circle,#mermaid-svg-a5FoGvwHIRrd46wN .node ellipse,#mermaid-svg-a5FoGvwHIRrd46wN .node polygon,#mermaid-svg-a5FoGvwHIRrd46wN .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-a5FoGvwHIRrd46wN .rough-node .label text,#mermaid-svg-a5FoGvwHIRrd46wN .node .label text,#mermaid-svg-a5FoGvwHIRrd46wN .image-shape .label,#mermaid-svg-a5FoGvwHIRrd46wN .icon-shape .label{text-anchor:middle;}#mermaid-svg-a5FoGvwHIRrd46wN .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-a5FoGvwHIRrd46wN .rough-node .label,#mermaid-svg-a5FoGvwHIRrd46wN .node .label,#mermaid-svg-a5FoGvwHIRrd46wN .image-shape .label,#mermaid-svg-a5FoGvwHIRrd46wN .icon-shape .label{text-align:center;}#mermaid-svg-a5FoGvwHIRrd46wN .node.clickable{cursor:pointer;}#mermaid-svg-a5FoGvwHIRrd46wN .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-a5FoGvwHIRrd46wN .arrowheadPath{fill:#333333;}#mermaid-svg-a5FoGvwHIRrd46wN .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-a5FoGvwHIRrd46wN .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-a5FoGvwHIRrd46wN .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-a5FoGvwHIRrd46wN .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-a5FoGvwHIRrd46wN .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-a5FoGvwHIRrd46wN .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-a5FoGvwHIRrd46wN .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-a5FoGvwHIRrd46wN .cluster text{fill:#333;}#mermaid-svg-a5FoGvwHIRrd46wN .cluster span{color:#333;}#mermaid-svg-a5FoGvwHIRrd46wN div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-a5FoGvwHIRrd46wN .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-a5FoGvwHIRrd46wN rect.text{fill:none;stroke-width:0;}#mermaid-svg-a5FoGvwHIRrd46wN .icon-shape,#mermaid-svg-a5FoGvwHIRrd46wN .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-a5FoGvwHIRrd46wN .icon-shape p,#mermaid-svg-a5FoGvwHIRrd46wN .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-a5FoGvwHIRrd46wN .icon-shape .label rect,#mermaid-svg-a5FoGvwHIRrd46wN .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-a5FoGvwHIRrd46wN .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-a5FoGvwHIRrd46wN .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-a5FoGvwHIRrd46wN :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} tool_calls
普通文字回复
超过最大轮数
用户输入任务
把 messages + tools 发给模型
模型返回什么?
执行对应工具函数
把结果以 role:tool 追加到 messages
任务完成,返回给用户
强制终止,提示失败

六、完整示例

py 复制代码
def run_agent(client, model, user_message) -> str:
    messages = [
        {
            "role": "system",
            "content": "你是数学助手。涉及计算时必须调用 calculate 工具,不要自己心算。",
        },
        {"role": "user", "content": user_message},
    ]
    for turn in range(MAX_TURNS):
        response = client.chat.completions.create(
            model=model, messages=messages, tools=TOOLS
        )
        message = response.choices[0].message
        if message.tool_calls:
            messages.append(message)
            for tool_call in message.tool_calls:
                tool_result = dispatch_tool(
                    tool_call,
                )
                messages.append(
                    {
                        "role": "tool",
                        "content": tool_result,
                        "tool_call_id": tool_call.id,
                    }
                )
        else:
            return message.content
    return "对话达到最大轮数,请重新开始。"
相关推荐
Setsuna_F_Seiei7 小时前
前端转型 Agent 开发 06 之 Agent Memory 记忆系统(让 Agent 更智能,更懂你)
前端·agent·ai编程
YYYing.10 小时前
【Agent系列 (二) 】大语言模型基础
人工智能·语言模型·自然语言处理·agent
大模型真好玩11 小时前
从 SDK 到成熟智能体:拆解 LangChain、Pi、DeepSeek Harness、Claude Code的本质区别
人工智能·agent·deepseek
梦在远山后12 小时前
通过 WSS 让 Server 安全调用 Desktop 本地工具(下01):Ticket、人工确认、幂等与断线对账
python·langchain·agent
阿里云云原生12 小时前
阿里云发布 AgenticOps 全栈能力,让 Agent 成为运维与研发的第一用户
agent
阿里云云原生12 小时前
阿里云刚发布的 AgentCore 有何不同?
agent
xiaozongt198913 小时前
AI代码学习-Function Calling + ReAct
agent·ai编程
阿里云云原生14 小时前
从个人生产力到企业生产力,阿里云发布企业级 Agent 平台 AgentCore
agent
Elcker14 小时前
Ynuo Agent(依诺) 一 Agentic 设计模式详解
agent·ai编程
武子康15 小时前
LingBot-VA 2.0 深度解析:为什么要同时预测未来世界与机器人动作
人工智能·后端·agent