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 "对话达到最大轮数,请重新开始。"
相关推荐
云烟成雨TD2 小时前
LlamaIndex 系列【31】检索增强策略:命名实体识别(NER)
ai·agent·rag·llamaindex
slacker-kian2 小时前
本地大模型 + 自建 MCP Server + SAP OData:让 LLM 代理 SAP 业务操作
大模型·llm·sap·agent·mcp·odata
Flynt2 小时前
Shopify 弃 React Native 上了 HN 1272 分,我复现了它给 Agent 用的那套无头架构
react native·前端框架·agent
leoZ2312 小时前
第 10 篇 数据飞轮与冷启动:AI 产品的数据从哪来
人工智能·大模型·agent
坐吃山猪4 小时前
Harness Engineering知识总结
llm·agent·harness
李燚4 小时前
Agent 要用 API key,但明文一次都不能进模型——Secret Runtime 落地实录(第110篇)
ai·agent·credential·secret·eino·deepflux·secret runtime
invicinble4 小时前
agent操作Windows的原理,以及操作其他的操作系统里面的数据
agent
wangruofeng5 小时前
把AI判断做成「智能if语句」!Jev实测:一次调用3个判断,置信度直接路由
aigc·agent·ai编程
coderMax6 小时前
MCP 架构概览
agent