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 "对话达到最大轮数,请重新开始。"
相关推荐
zzz_23682 小时前
TencentDB-Agent-Memory 深度解析:让多个 Agent 共享项目经验的记忆中枢
java·开发语言·jvm·人工智能·agent·memory·tencent db
武子康3 小时前
WebRTC 已经双向,语音 Agent 为什么还会抢话?
人工智能·llm·agent
用户469368483203 小时前
kimi-code 深度掌握系列文章-Plan Mode 与 Goal Mode:结构化的自主执行(九)
agent
leeyi3 小时前
Memory 三层设计:为什么 [“session“,id] 最后写成了四元组(第78篇-E64)
aigc·agent·ai编程
为你学会写情书4 小时前
Agent Skills 完全指南:从目录规范到渐进式加载的工程实践
agent
Ai拆代码的曹操4 小时前
Agent 做错了怎么办?Self-Critique 机制拆解
后端·agent·ai编程
ckjoker4 小时前
我把Java多模态链路从0跑通了,结果先被4个坑狠狠干了一顿
后端·agent
小当家.1054 小时前
Agent 如何触发 Skill?Skill 和 Tool 的区别详解
agent·skill·tool
提笔了无痕5 小时前
Agent 上下文管理详解、Context设计与构建
数据库·oracle·agent·context