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 "对话达到最大轮数,请重新开始。"
相关推荐
粥里有勺糖1 小时前
视野修炼-技术周刊第131期 | Bun 与 pnpm Rust 化
前端·github·agent
像云~3 小时前
DeepSeek Harness Cordis理解
agent·harness·crodis
魔术师Grace4 小时前
调用一次大模型,就算 Agent 吗?
aigc·agent·ai编程
桃西西呀5 小时前
GPT-5.6 的 ultra 模式凭什么开 4 个 Agent 并行跑?——多智能体协作,是把"一个聪明人"换成"一个团队"
人工智能·llm·agent
怕浪猫5 小时前
用好这 5 把钥匙,你就能拦截 AI Agent 的一切行为
数据分析·agent·产品
澄怀5 小时前
Agent 说「我已经改好文件了」,这句话到底能不能信?
llm·agent
luckystar513~6 小时前
Hermes 工程化实战专栏:安装部署实战——从装好到跑通第一个任务
人工智能·agent·智能体·hermes·实战专栏·hermes安装配置
新知图书7 小时前
14.5 AI试驾预约系统的完整实现
人工智能·agent·ai agent·智能体
水管在开花.9 小时前
Agent范式与LangGraph-②零基础保姆级教程
人工智能·面试·langchain·agent
ShallWeL9 小时前
RAG 文档变更后的检索回归清单
人工智能·agent·知识库·rag·检索