5分钟内,我将向你展示如何从零开始构建 AI 代理

发布: (2025年12月7日 GMT+8 22:46)
5 min read
原文: Dev.to

Source: Dev.to

什么是 AI 代理?(简明解释)

普通的 AI 模型(比如 ChatGPT 或 Gemini)只能给你文字答案。

AI 代理 可以:

  • 执行任务,不仅仅是回答问题
  • 使用工具,例如 Google 搜索、计算器、API 或数据库
  • 做出决策并规划步骤
  • 在最少的人类帮助下自主工作

把 AI 代理想象成一个小型的 “AI 工作员”,可以为你完成任务。

开始之前需要准备的东西

  • 已在电脑上 安装 Python
  • 任意 代码编辑器(如 VS Code 或 Google Antigravity IDE
  • Google ADK —— 我们将要安装的开源框架(参见 Google ADK 概览
  • 一个 Gemini API 密钥(免费生成)

从零构建 AI 代理

步骤 1:创建项目文件夹并建立虚拟环境

mkdir my_first_agent
cd my_first_agent

创建虚拟环境:

python -m venv .venv

激活它:

# macOS / Linux
source .venv/bin/activate  

# Windows
.\venv\Scripts\activate

虚拟环境可以让你的项目保持干净,避免版本冲突。

步骤 2:安装 Google ADK

pip install google-adk

步骤 3:创建你的代理项目

adk create my_agent

选择模型(例如 Gemini 2.5 或 Gemini Flash)。

创建完成后会看到如下文件结构:

my_agent/
 ├── agent.py
 ├── .env
 └── __init__.py
  • agent.py – 代码的主文件
  • .env – 用来存放你的 API 密钥

在 IDE 中打开该文件夹。

Create Your Agent Project

步骤 4:获取免费 Gemini API 密钥

  1. 前往并使用 Google 账户登录。
  2. 在左下角侧边栏点击 Get API key
  3. 点击 Create API Key,为其命名,选择(或创建)一个项目,然后复制密钥。

Get Your Free Gemini API Key

将密钥写入 .env

GOOGLE_API_KEY="your-key-here"

步骤 5:测试默认代理

打开 agent.py,你会看到一些模板代码。
将占位的模型名称替换为 Gemini 模型的内部名称(例如 gemini-2.0-flashgemini-3-pro-preview)。

运行代理:

adk run my_agent

提一个简单的问题。如果一切正常,你会收到答案。如果模型的免费额度已用完,换成更轻量的免费层模型即可。

步骤 6:创建多个代理(研究 + 摘要 + 协调)

agent.py(或新建模块)中加入以下代码,构建一个多代理流水线:

from google.adk.agents.llm_agent import Agent
from google.adk.tools import google_search, AgentTool

# Research Agent – searches the web
research_agent = Agent(
    name="Researcher",
    model="gemini-2.5-flash-lite",
    instruction="""
You are a specialized research agent. Your only job is to use the
google_search tool to find the top 5 AI news items for a given topic.
Do not answer any user questions directly.
""",
    tools=[google_search],
    output_key="research_result",
)

print("Research Agent created successfully.")

# Summarizer Agent – creates summaries
summarizert = Agent(
    name="Summarizert",
    model="gemini-2.5-flash-lite",
    instruction="""
Read the research findings {research_result} and create a summary for each topic,
including a link to read more.
""",
    output_key="summary_result",
)

print("Summarizert Agent created successfully.")

# Root Coordinator – orchestrates the workflow
root_agent = Agent(
    model="gemini-2.5-flash-lite",
    name="root_agent",
    description="A helpful assistant for user questions.",
    instruction="""
You are the coordinator. First, delegate user questions to the 'research_agent' to gather information.
Second, pass the findings to the 'summarizert' agent to create a summary.
Finally, compile the summaries into a final response for the user.
""",
    tools=[
        AgentTool(research_agent),
        AgentTool(summarizert),
    ],
)

这段代码定义了一个三步流水线:研究 → 摘要 → 协调。

步骤 7:运行多代理系统

adk run my_agent

提示输入时,键入你想要研究的主题。你会看到:

  • Research Agent 正在收集信息
  • Summarizer Agent 正在生成摘要
  • Coordinator 正在整合最终回复

所有代理协同工作,交付结果。

步骤 8:使用网页界面(更简便)

Google ADK 自带的网页 UI 让你可以通过浏览器与代理交互,测试和迭代更加轻松。使用同样的 adk run 命令启动后,打开终端给出的本地 URL 即可在浏览器中使用。

Back to Blog

相关文章

阅读更多 »

AI驱动编程的崛起

在软件开发领域,根本性的变化正在发生。这种变化不是逐步进行的,而是剧烈的。到2026年,AI-Driven Coding 将……