AI Agent入门:从大语言模型到智能体
AI Agent实战:从工具调用到自主规划的架构设计
第一章:AI Agent入门:从大语言模型到智能体
你有没有想过,为什么ChatGPT能回答问题,却不能帮你订机票?为什么Claude能写文章,却不能替你发邮件?这背后隐藏着一个关键问题:单纯的大语言模型(Large Language Model, LLM)虽然强大,但它们本质上还是"纸上谈兵"的选手——说得头头是道,却迈不出房门一步。
这就是我们今天要探讨的核心主题:AI Agent。不是那些科幻电影里会走路的机器人,而是能够真正感知环境、做出决策、执行行动的智能系统。简单来说,Agent就是给大语言模型装上了"手"和"脚",让它从"能说会道"进化到"能说会做"。
Have you ever wondered why ChatGPT can answer questions but can't book flights for you? Why Claude can write essays but can't send emails on your behalf? There's a fundamental limitation hiding behind these capabilities: standalone Large Language Models, despite their power, are essentially "armchair strategists"—they talk a good game but can't take real action.
This is precisely what we'll explore in this chapter: AI Agents. Not the walking, talking robots from sci-fi movies, but intelligent systems that can genuinely perceive their environment, make decisions, and execute actions. In simple terms, an Agent is a Large Language Model equipped with "hands" and "feet"—evolving from "eloquent speaker" to "capable doer."
---
从问答机器到智能助手:大语言模型的进化之路
要理解AI Agent为何如此重要,我们先得回顾一下大语言模型短短几年间的惊人进化。
The Evolution: From Q&A Machines to Intelligent Assistants
Let's first trace the remarkable evolution of Large Language Models over the past few years to understand why AI Agents matter so much.
2020年:GPT-3的惊艳登场
2020年6月,OpenAI发布了GPT-3,这个拥有1750亿参数的模型震惊了整个AI界。在它之前,训练一个能理解上下文的语言模型需要大量特定任务的标注数据。但GPT-3展示了惊人的few-shot learning(少样本学习)能力——你只需要在提示中给出几个例子,它就能理解你要做什么。
GPT-3 was released in June 2020, and this 175-billion-parameter model shocked the entire AI community. Before GPT-3, training a context-aware language model required massive amounts of task-specific labeled data. But GPT-3 demonstrated astonishing few-shot learning capabilities—give it a few examples in the prompt, and it understands what you want to do.
这在当时已经是革命性的进步。但问题也很明显:GPT-3能写文章、做翻译、编代码,但它永远停留在"输入→输出"的单一交互模式中。你问,它答;答完就结束。它不知道你是谁,也不记得你们的对话历史(每次都需要重新提供上下文)。
This was revolutionary at the time. But the limitations were equally obvious: GPT-3 could write articles, translate languages, and generate code, but it was forever trapped in a single "input→output" interaction pattern. You ask, it answers; answer ends. It didn't know who you were, nor did it remember your conversation history.
2022-2023年:ChatGPT引领的对话革命
2022年11月,ChatGPT横空出世。虽然底层仍是GPT-3.5的变体,但通过Instruction Tuning(指令微调)和RLHF(基于人类反馈的强化学习),它学会了更好地理解和遵循人类意图。
In November 2022, ChatGPT burst onto the scene. Though built on a variant of GPT-3.5, through Instruction Tuning and RLHF (Reinforcement Learning from Human Feedback), it learned to better understand and follow human intentions.
更重要的是,ChatGPT引入了会话记忆机制。它能记住对话历史,这意味着你可以进行多轮深入交流,而不是每次都从头开始。更长的上下文窗口(Context Window)让模型能够"看到"更多前后信息。
More importantly, ChatGPT introduced conversational memory. It could remember dialogue history, enabling multi-turn deep conversations instead of starting fresh each time. Larger context windows allowed models to "see" more surrounding information.
但即便如此,ChatGPT本质上还是一个超级搜索引擎的升级版——它擅长处理信息,却无法直接与现实世界互动。你可以问它"帮我写一封请假邮件",它会给你一封完美的邮件内容,但你还得自己打开邮箱、粘贴、发送。
Still, ChatGPT at its core remained an upgraded super search engine—great at processing information but unable to interact directly with the real world. You could ask it to "write a leave request email," and it would give you a perfect email, but you'd still have to open your email client, paste it, and send it yourself.
2023-2024年:Claude与GPT-4的工具觉醒
真正让大语言模型开始"觉醒"的是两大里程碑:Claude的发布和GPT-4的登场。
What truly awakened Large Language Models were two major milestones: Claude's debut and GPT-4's arrival.
2023年3月,Anthropic推出了Claude,它在长文本处理和安全性方面展现了卓越能力。紧接着,OpenAI发布了GPT-4,这是首个支持多模态输入(图像+文本)的大语言模型,同时也是首个原生支持Function Calling(函数调用)功能的模型。
In March 2023, Anthropic launched Claude, demonstrating exceptional capabilities in long-form text processing and safety. Shortly after, OpenAI released GPT-4—the first LLM supporting multimodal input (images + text) and the first with native Function Calling capabilities.
Function Calling是一个关键突破。它允许模型在生成回复时,明确指示需要调用的外部函数或工具。比如,当用户问"今天北京天气怎么样"时,模型可以输出一个结构化的函数调用请求,指定要调用`get_weather(location="北京")`。这就像是给模型装上了一个"插口",让它能够请求执行外部操作。
Function Calling was a crucial breakthrough. It allows models to specify external functions or tools to call when generating responses. For instance, when a user asks "What's the weather like in Beijing today?", the model can output a structured function call request, specifying `get_weather(location="Beijing")`. This was like installing an "outlet" on the model, enabling it to request external operations.
这就是从LLM到Agent的转折点。大语言模型不再只是一个"文本生成器",而是开始成为能够编排工具、执行任务的调度中心。
This was the turning point from LLM to Agent. Large Language Models no longer functioned merely as "text generators" but began serving as orchestration centers capable of calling tools and executing tasks.
2024年及以后:Agent的寒武纪大爆发
2024年,AI Agent迎来了爆发式增长。OpenAI发布了GPT-4o和最新的Agent开发框架,Anthropic强化了Claude的工具调用能力,各种开源Agent框架如AutoGPT、LangChain、LlamaIndex、CrewAI等如雨后春笋般涌现。
In 2024, AI Agents experienced explosive growth. OpenAI released GPT-4o and updated Agent development frameworks, Anthropic enhanced Claude's tool-calling capabilities, and various open-source Agent frameworks like AutoGPT, LangChain, LlamaIndex, and CrewAI mushroomed across the landscape.
我们见证了从"LLM能做什么"到"Agent能做什么"的范式转移。这不仅仅是技术进步,更是一种全新的计算范式——从被动响应请求,到主动规划并执行复杂任务。
We witnessed a paradigm shift from "what can LLMs do" to "what can Agents do." This isn't merely technological progress—it's an entirely new computing paradigm, transitioning from passively responding to requests, to proactively planning and executing complex tasks.
---
什么是AI Agent?重新定义智能系统
Now that we understand the evolution, let's define what an AI Agent truly is.
Agent的核心定义
简单来说,AI Agent是一个能够自主感知环境、做出决策并执行行动以实现特定目标的智能系统。它包含以下几个核心组件:
An AI Agent is an intelligent system capable of autonomously perceiving its environment, making decisions, and executing actions to achieve specific goals. It comprises several core components:
感知(Perception):Agent通过各种"感官"获取环境信息。对于LLM-based Agent,这通常是通过Tool(工具)来实现的——比如搜索API、数据库查询、文件读取等。Agent不是闭门造车,而是能够主动获取外部信息。
感知(Perception):The Agent acquires environmental information through various "sensory" mechanisms. For LLM-based Agents, this is typically achieved through Tools—such as search APIs, database queries, file reading, etc. Agents don't work in isolation; they actively gather external information.
推理(Reasoning):这是大语言模型的核心优势。Agent利用LLM强大的推理能力,理解当前状况、分析问题、制定策略。与传统的if-else规则引擎不同,LLM-based推理能够处理模糊的、开放式的、复杂的问题。
推理(Reasoning):This is the LLM's core strength. Agents leverage powerful LLM reasoning capabilities to understand situations, analyze problems, and formulate strategies. Unlike traditional if-else rule engines, LLM-based reasoning handles ambiguous, open-ended, and complex problems.
规划(Planning):这是Agent区别于简单工具调用的关键能力。当面对复杂任务时,Agent能够将大目标分解为小步骤,动态调整执行计划,必要时回溯或尝试替代方案。
规划(Planning):This distinguishes Agents from simple tool calls. When facing complex tasks, Agents can break down larger goals into smaller steps, dynamically adjust execution plans, and backtrack or try alternatives when necessary.
行动(Action):Agent通过调用工具来影响环境或获取信息。这可能包括调用API、写文件、执行代码、发送消息等各种操作。行动的结果又会被Agent感知,形成闭环。
行动(Action):Agents influence the environment or acquire information through tool calls. This may include API calls, file writing, code execution, sending messages, and various other operations. The results of actions are perceived by the Agent, forming a closed loop.
记忆(Memory):Agent通常具备短期记忆和长期记忆两种机制。短期记忆保存当前会话的上下文信息,长期记忆则存储从历史经验中提取的知识。这使得Agent能够从过去的行动中学习和改进。
记忆(Memory):Agents typically have both short-term and long-term memory mechanisms. Short-term memory preserves current session context, while long-term memory stores knowledge extracted