Transcript · English → Chinese

Building Your First AI Agent

在本视频中, 你将了解什么是 AI 智能体,

不仅是从

理论层面, 更会从实践层面了解。我们将探讨现 代

智能体背后的理念, 然后构建你的第一个 AI

智能体,一步

步使用 Google 的 ADK。到最后, 你将了解

智能体如何推理、行动和适应, 并且你会拥有可以

亲自运行的代码。那么, AI 智能体是什么?

最简单地说,智能体 是一种不只是回答问题的软件,

它还能做决定并采取行动。 它不会像传统聊天机器人那样只生 成单一回复,

而是会查看你的请求, 弄清楚

需要采取哪些步骤, 可能会调用 API、运行

代码、查看结果, 然后决定下一步该做什么。

最清晰的解释之一来自 一篇研究论文

ReAct:协同推理, 以及语言模型中的行动。

那篇论文的理念很简单, 却很强大。

语言模型不应只是一次性生成 文本。

它们实际上可以逐步推理, 采取行动,例如调用工具

或 API,观察结果, 再决定下一步做什么。

推理、行动、观察, 以及

调整的循环,是现代 AI

智能体的工作基础, 也符合其定义。

具备推理、规划, 和记忆能力,并有足够

自主性去适应, 并代表用户做决策的系统。

现在,并非所有智能体的行为都一样 。

理解它们的一种有用方式 是将其分为三种大致模式。

首先是顺序型智能体。

它们像流水线一样逐步运行。

第一步,然后第二步,再第三步。 它们可预测,但很死板。

第二种是反应型智能体。 它们当下做决定。

它们查看当前状态,然后问: “我下一步该做什么?”也许这次用工 具 A

下次用工具 B。它们很灵活, 但不会提前规划。

最后是审慎型 或规划型智能体。

它们会停下来拟定计划, 然后执行。

想想订旅行。 你不会随便买张机票。

你会选日期、酒店, 然后按这个步骤顺序

一路执行到底。

那么,哪一种最适合 你?这取决于问题。

对于简单、可预测的流程, 顺序型就足够了。

对于动态情况,反应型效果更好。

对于有依赖关系的多步骤目标, 你需要规划型

智能体。好了,这就是整体概览。

现在我们来真正构建一个智能体, 这样你就能看到这些理念如何实际 运作。

我们将构建一个博客写作智能体。 你给它一个主题,它就会

生成结构化大纲,再将 该大纲扩展成完整的博客文章,并且

然后建议备选标题和开篇钩子。

这正是 ADK 的分类派上用场的地方。

在我们讲解代码的过程中, 请注意规划和写作

智能体都是由模型驱动的 LLM 智能体。

它们被封装在循环智能体中, 后者是 ADK 中的工作流智能体。

如果验证失败,它们会自动处理重试 。

如果我们需要超出 这一范围的功能,ADK 也能让我们

通过继承基础 智能体来扩展自定义智能体。

那么我们先设置环境, 然后逐行讲解

代码。首先,你需要安装 UV。

你可以通过运行该命令来做到这一 点 就在这里。

其次, 你需要安装 Google ADK,因为我们

显然需要使用

其中的 ADK。 然后我们在你的 IDE 中创建一个名为

agent.py 的文件。我们需要

导入这些关键库, 然后就可以开始构建我们的

第一个

智能体了。所以我们正在构建的第一 个智能体 叫作

博客规划器。它唯一的工作 是接收一个主题并将其转换

为结构化大纲。 我们给它取名为博客规划器,这样

就容易识别, 并告诉它要使用哪个模型,该模型

来自我们的环境文件。 描述只是一个便于人类阅读的

说明。 介绍部分的重要之处

在于,我们实际上告诉

它要生成什么,也就是 一个带有标题的 Markdown 大纲,

简短的引言, 四到六个带要点的章节,

以及一个

结论。明确写出这些要求, 能让输出结果

更可靠。最后, 看看输出键。

我们将其设为“博客大纲”。 这意味着无论这个

智能体写什么,都会以该名称存储在 共享 状态中,

这样下一个能够接手的 智能体就能

自动处理。

接下来,我们添加第二个智能体, 大纲验证检查器。

它不会写任何新内容, 只会检查大纲。

它的指令很严格。 如果状态中的大纲包含

标题、引言、四到六个章节, 以及结论,

它必须回答“okay”这个词。 如果缺少任何内容,它必须

回答“retry”这个词, 并说明哪里有问题。

这样我们就能自动决定 是接受大纲还是重试

再次。为了连接规划器 和检查器,我们把它们封装进一个

名为“稳健博客规划器”的循环代理 中。

循环会先运行规划器, 再运行检查器。

如果检查器说没问题,就完成了。

如果检查器说重试, 循环就会再次运行规划器,

使用相同的指令。 它最多可以这样做三次。

这给了我们一道安全网,所以 如果模型遗漏了什么,

它还有一次

机会把它做好。 现在我们创建另一个名为博客

撰写器的代理。 它会获取我们刚刚保存的提纲

在

blog_outline 中,并将其转化为完整的 Markdown 博客

文章。 该指令会告诉它如何写作。

它应假定受众是软件工程师, 跳过

基础内容,并专注于实用见解。

同时解释怎么做以及为什么, 并加入简短的

在有帮助的地方加入代码示例。 它还应遵循以下结构

大纲

并使用恰当的标题。 完成的草稿会保存到

共享状态中的 blog_post, 可供下一步使用。

接下来,我们要添加另一个检查器, 这次是用于文章的。

它叫作博客文章验证检查器。

它查看 blog_post,并检查 文章是否包含

引言、与大纲相符的章节、 结论以及

清晰的解释。如果通过, 检查器会显示“通过”。

如果没有通过,它会显示“重试”, 并列出所需的修改。

就像规划阶段一样, 我们将写作器及其检查器

组合成一个名为“稳健博客 写作器”的循环代理。

这样系统会自动重试 写作步骤,直到

验证通过,最多三次。

现在,我们将每个循环封装起来, 稳健博客规划器,

以及功能强大的博客写作工具。

这让根代理可以直接调用它们 无需担心

内部发生了什么。最后, 我们将这一切整合到名为 blogger 的根

代理中。 blogger 的指令很简单。

当用户给它一个主题时, 它首先调用规划工具来

制定大纲。 然后调用写作工具起草

完整

的文章,最后, 它会添加三个备选

标题和两个推文长度的钩子文案。

根代理只能访问我们 暴露的两个

工具,即规划器和写作器, 因此它的工作流程

清晰且受控。 你给它一个主题,它会规划,

写作、检查, 然后为你提供一篇润色完善的博客

文章。 至此,你已成功编写了第一个

代理脚本。

下一步是实际运行它。

打开终端, 然后用这条命令通过

ADK Web 启动它,这样你就能在简洁的 界面中查看一切。

运行后, 在你的

浏览器中打开本地 ADK Web 链接,

然后直接开始与该智能体互动。

首先, 我会让它生成一篇关于

最热门的

十个 AI 智能体应用案例的博客。 来看看它如何规划、

撰写并交付结果。

你的子智能体随后会协同合作, 撰写一篇博客文章。

就是这样。你已经构建并 运行了你的第一个 AI 智能体

使用 Google 的 ADK。 我们先从智能体的概念讲起,

然后逐步讲解代码,

最后看着它

在终端中变为现实。 感谢观看。

请查看说明栏,获取所有 本视频中提到的资源和代码。

视频。 观看下一个视频,了解如何

连接你的 AI

智能体到 MC-

End of transcript · 1,442 words · machine translated

Original transcript (English)

In this video, you're going to learn what AI agents are, not just in theory, but in practice. We'll look at the ideas behind modern agents, and then we'll build your very first AI agent step by

step using Google's ADK. By the end, you'll understand how agents reason, act, and adapt, and you'll have code that you can actually run yourself. So what are AI agents? At the simplest level, an agent is software that doesn't just answer, it can decide and take action. Instead of generating a single response

like a traditional chatbot, it looks at your request, figures out what steps need to be taken, maybe calls an API, runs code, looks at the results, and then decides what to do next. One of the clearest explanations comes from the research paper ReAct, synergizing, reasoning, and acting in language models.

The idea in that paper was simple but powerful. Language models shouldn't just generate text in one go. They can actually reason step by step, take an action like calling a tool or API, observe the results, and then decide what to do next. That cycle of reasoning, acting, observing, and

adjusting is the foundation of how modern AI agents work, and it lines up with how it's defined. Systems with reasoning, planning, and memory, with enough autonomy to adapt and make decisions on behalf of the user. Now, not all agents behave the same way.

A useful way to think about them is in three broad patterns. First, we have sequential agents. These run step by step like an assembly line. Step one, then step two, then step three. They're predictable but rigid. Second, we have reactive agents. These decide in the moment.

They look at the current state and ask, "What should I do next?" Maybe tool A one time, tool B the next. They're flexible, but they don't plan ahead. Lastly, we have deliberate or planning agents. These pause to sketch a plan, then execute. Think about booking travel. You don't just buy a flight randomly.

You pick dates, hotels, and then that's the order of steps that you follow through. So which of these is the right one for you? It depends on the problem. For simple, predictable flows, sequential is fine. For dynamic, reactive works better.

And for multi-step goals with dependencies, you want planning agents. Okay, that's the high level view. Now let's actually build an agent, so you can see these ideas in action. We'll build a blog writing agent. You give it a topic, it will generate a structured outline, turn that outline into a full blog post, and

then suggest alternative titles and hooks. This is where ADK's categories comes in. As we walk through the code, notice how the planner and writer agent are LLM agents, which are powered by a model. They're wrapped in loop agents, which are workflow agents in ADK.

These handle retries automatically if validation fails. And if we ever wanted something beyond that, ADK would also let us extend a custom agent by subclassing the base agent. So let's set up the environment and then go line by line through the code. First off, you need to install UV.

You can do that by running the command right here. Secondly, you need to install Google ADK because we need to obviously use ADK in this. And then let's create a file called agent.py in your IDE. We need to

import these key libraries, and then we can start building out our very first agent. So the first agent that we're building out is called the blog planner. Its only job is to take a topic and turn it into a structured outline. We give it the name blog planner, so

it's easy to identify, and we tell it which model to use, which comes from our environment file. The description is just a human readable note. The important part of the introduction is here we actually tell it what to produce, which is a markdown outline with a title, a

short introduction, four to six sections with bullet points, and a conclusion. By spelling that out, we make the output much more reliable. Finally, look at the output key. We set it to blog outline. This means whatever this

agent writes will be stored in shared state under that name, so that the next agent that can pick it up can do it automatically. Next, we add a second agent, the outline validation checker. This one doesn't write anything new, it just checks the outline.

Its instruction is strict. If the outline in state has a title, an intro, four to six sections, and a conclusion, it must answer with the word okay. If something is missing, it must answer with the word retry and explain what's wrong. That way we can automatically decide whether to accept the outline or try

again. To connect the planner and the checker, we wrap them inside a loop agent called robust blog planner. The loop first runs the planner, then the checker. If the checker says okay, we're done. If the checker says retry, the loop runs the planner again

with the same instructions. It can do this up to three times. This gives us a safety net, so if the model forgets something, it has another chance to get it right. Now we create another agent called blog writer. This one takes the outline we just saved

in blog_outline and turns it into a full markdown blog post. The instruction tells it how to write. It should assume the audience is software engineers, skip the basics, and focus on practical insights.

Explain both the how and the why and include short code snippets where helpful. It should also follow the structure of the outline with proper headings. The finished draft is saved under blog_post in shared state, ready for the next step.

Next up, we're gonna add another checker, this time for the post. It's called blog post validation checker. It looks at blog_post and checks if the article has an introduction, a, sections that match the outline, a conclusion, and clear explanations. If it passes, the checker says okay.

If not, it says retry and lists the fixes which are needed. Just like with planning, we combine the writer and its checker into a loop agent called robust blog writer. This makes the system automatically retry the writing step until the validation passes, up to three times.

Now, we wrap each loop, the robust blog planner, and the robust blog writer as tools. This is what lets the root agent call them directly without worrying about what's happening inside. Finally, we put it all together with the root agent called blogger. The instructions for blogger are simple.

When a user gives it a topic, first it calls the planner tool to make an outline. Then it calls the writer tool to draft the full post, and finally, it adds three alternative titles and two tweet-length hooks.

The root agent only has access to the two tools we exposed, the planner and the writer, so its workflow is clear and controlled. You give it a topic, it plans, it writes, it checks, and then it gives you a polished blog post. You've now successfully written your first

agent script. The next step is to actually run it. Head into your terminal and launch it using this command with ADK Web so you can view everything inside a simple UI. Once that's running, open the local ADK web link in your

browser and start interacting with the agent directly. To kick things off, I'm going to ask it to generate a blog on the top 10 use cases for AI agents. Let's see how it plans,

writes, and delivers the results. Your subagents will then work together to create a blog post. And that's it. You've built and run your first AI agent with Google's ADK. We started from the idea of what agents are, then walked through the code step by step,

and finally saw it come to life in terminal. Thank you for watching. Check out the description box for all the resources and code mentioned in this video. Check out this next video to learn how to connect your AI

agent to an MC-

iPhone app Translate videos on your iPhone Paste a TikTok, Instagram or X link, add subtitles in your language and save the video to Photos.

Read any video in your language

Paste a link from X, TikTok, YouTube or Instagram. You get a page like this one.