手把手演示用 Google 开源 Agent Development Kit 在 Cloud 上构建 Trend Spotter Agent:组织为正规 Python 包、编写 Prompt 定义角色、串联 Web 搜索与情报整理,最终输出结构化报告。
AI Agent 的世界正以闪电般的速度发展。紧跟最新趋势、新开源工具以及重要的开发者讨论,可能感觉像一份全职工作。在我们准备推出面向 Agent 开发者的新播客(敬请期待!)时,我们正面临着这个挑战。为了确保每一期节目都能做好准备,我们希望创建一种自动化方式来获取最重要新闻的最新动态。
在本指南中,我们将逐步引导你在 Google Cloud 上使用开源的 Agent Development Kit(ADK)构建你的第一个 AI Agent。我们将设计一个"趋势发现者"Agent,它的使命是充当你的个人 AI 分析师,教它扫描网络、筛选噪音,找到真正重要的东西。
在本文结束时,你将拥有一个实用的、可运行的工具,它能自动生成简明的情报报告,让你随时了解最新动态,节省数小时的手动研究时间。更重要的是,你将学会使用 ADK 构建自己的 Agent 的基本技能。你将学会如何:
此设置使用标准包结构,允许 ADK 工具发现并运行我们的 Agent,无需 main.py 文件。
打开终端。创建以下文件夹结构和虚拟环境。
# Create the main project folder
mkdir trend-spotter && cd trend-spotter
# Create the Python package folder that will hold our code
mkdir trend_spotter
touch trend_spotter/__init__.py
touch trend_spotter/agent.py
touch trend_spotter/prompt.py
# Create the top-level configuration files
touch pyproject.toml requirements.txt
# Finally, create and activate a virtual environment
python3 -m venv venv && source venv/bin/activate
(On Windows, use python -m venv venv && .\venv\Scripts\activate)
打开 requirements.txt 并添加我们的唯一依赖:
google-adk
从终端安装:
pip install -r requirements.txt
这些设置告诉 ADK 如何安全地连接到你的 Google Cloud 账户,以使用 Vertex AI 和 Google Search 等服务。
设置环境变量:在终端中运行以下 export 命令。这些命令告诉 ADK 在你特定的 Google Cloud 项目和区域中使用 Vertex AI 平台。
export GOOGLE_GENAI_USE_VERTEXAI=true
export GOOGLE_CLOUD_PROJECT=<your-gcp-project-id>
export GOOGLE_CLOUD_LOCATION=<your-gcp-project-location>
登录你的账户:运行此一次性命令。它将为你打开一个浏览器进行登录,允许 ADK 代表你发出授权请求。
gcloud auth application-default login
现在我们将编写代码并将其放入 trend_spotter 包目录中。
Prompt 包含 Agent 的所有指令。
注意,我们正在引导 LLM 在调用 GoogleSearch 工具时指定日期范围,以确保我们专注于过去一周的趋势。
打开 trend_spotter/prompt.py 并添加以下指令:
# trend_spotter/prompt.py
TREND_SPOTTER_PROMPT = """
You are a helpful AI assistant and expert tech analyst for a new podcast called "The Agent Factory". Your goal is to generate a highly relevant and verifiable report about the latest developments in AI agents that specifically impact developers.
**Your multi-step plan is as follows:**
**Step 1: Discover the Current Date.**
Your very first action must be to find the current date.
- **Action**: Use the `Google Search` tool with a query like "what is today's date".
- From the search result, identify the current year, month, and day.
**Step 2: Formulate and Execute Search Queries with Date Operators.**
Now, you must formulate your search queries by embedding the date range directly into the query string using Google's `after:YYYY-MM-DD` and `before:YYYY-MM-DD` operators. Calculate these dates to cover the last 7 days.
- You must perform at least three initial searches to cover trends, releases, and questions.
- **Example Query Format**: `"AI agent trends after:2025-06-01 before:2025-06-08"`
- After the initial searches, you may perform 1-2 additional, more targeted searches if a category is missing information. **Do not perform more than 5 searches in total.**
**Step 3: Analyze the Results and Create the Report.**
Read through all the text and links from your searches. Your primary filter is to **only select topics, tools, and questions that have a direct and significant impact on developers building AI agents.**
**Critical Rule for Sourcing:** For every trend, release, or question you identify, you must first pinpoint the **single best search result** that provides the evidence. You will then use the URL from that **exact search result** as the source link for that item. **If you cannot find a specific source link for an item, do not include that item in the report.**
Based on these rules, create a report:
1. The report **must begin with a header** specifying the date range used.
2. The body of the report must have exactly three sections.
3. For each item, you **must provide three pieces of information**: a 1-2 sentence explanation, the "Developer Impact" analysis, and the **verifiable source URL**.
The report format must be:
**🔥 Top 5 Trends for Agent Developers**
1. **[Trend 1 Name]**: [A 1-2 sentence explanation of this trend.] (Source: [URL])
* **Developer Impact**: [A 1-sentence explanation of why this matters to developers.]
2. ... (up to 5 total)
**🚀 Top 5 Releases for Agent Developers**
1. **[Release 1 Name]**: [A 1-2 sentence explanation of the tool, framework, or model.] (Source: [URL])
* **Developer Impact**: [A 1-sentence explanation of why this matters to developers.]
2. ... (up to 5 total)
**🤔 Top 5 Questions from Agent Developers**
1. **[Question 1 Topic]**: [A 1-2 sentence explanation of what developers are asking.] (Source: [URL])
* **Developer Impact**: [A 1-sentence explanation of why this matters to developers.]
2. ... (up to 5 total)
Begin your work now by executing your plan.
"""
agent.py 文件将我们的 prompt 和搜索工具连接到一个新的 ADK Agent。
打开 trend_spotter/agent.py 并添加以下代码:
# trend_spotter/agent.py
from google.adk.agents import Agent
from google.adk.tools import google_search
from . import prompt
# Use the "latest" tag to always get the most recent stable version of the model.
MODEL = "gemini-2.5-pro-preview-05–06"
# This single agent will perform all the work.
trend_spotter_agent = Agent(
model=MODEL,
name="trend_spotter_agent",
description="An agent that finds and reports on AI agent trends.",
# The agent's entire logic comes from our detailed prompt.
instruction=prompt.TREND_SPOTTER_PROMPT,
# We give the agent a single tool: the ability to search Google.
tools=[google_search],
)
# We assign it to `root_agent` by convention for ADK to discover.
root_agent = trend_spotter_agent
要使用 adk web 命令,我们需要告诉 ADK 在哪里找到我们的 Agent。我们需要在 pyproject.toml 文件中完成此配置。
在根目录打开 pyproject.toml 并添加以下配置:
[project]
name = "trend_spotter"
version = "0.1.0"
# This section tells the ADK how to find our agent.
[tool.adk.agents]
trend_spotter = "trend_spotter.agent:root_agent"
现在到了激动人心的部分!
从项目根目录运行此命令。-e . 命令以"可编辑"模式安装你的项目,以便 adk 工具能够找到它。
pip install -e .
adk web
打开终端中显示的 URL。在 Web 界面中,从下拉菜单中选择"trend_spotter"。你现在可以与你的 Agent 聊天了!问它:"生成一份最新的 AI Agent 新闻报告。"根据你在 prompt 中指示 Agent 执行的搜索数量,这可能需要几分钟时间。
响应将类似于:
Okay, I will begin by finding the current date.
Okay, I have identified the current date as June 7, 2025.
Now, I will formulate and execute search queries to find information from the last 7 days (June 1, 2025, to June 7, 2025) about AI agent trends, releases, and questions relevant to developers.
Okay, I have analyzed the search results and will now generate the report on AI agent developments impacting developers for the period of June 1, 2025, to June 7, 2025.
Report: AI Agent Developments for Developers (June 1, 2025 - June 7, 2025)
🔥 Top 5 Trends for Agent Developers
Maturation of Agentic AI: Agentic AI, which involves AI systems that can autonomously handle complex tasks, is moving beyond hype and into practical application, with companies beginning to launch pilot projects. This trend signifies a shift towards more sophisticated AI that can streamline various stages of software development, testing, and maintenance.
Developer Impact: Developers will increasingly need to build, integrate, and manage these more autonomous AI agents, requiring skills in designing for complex, goal-oriented behaviors.
Rise of Multi-Agent Systems: There is a growing interest in systems where multiple AI agents collaborate to solve larger, more complex problems than a single agent could handle. This involves individual agents with specific goals and tools working together within a broader workflow.
Developer Impact: This trend requires developers to think about inter-agent communication protocols, task delegation, and overall system orchestration.
Emphasis on AI Agent Evaluation and Benchmarking: As AI agents become more capable and undertake more complex tasks, there's a corresponding growth in the need for robust evaluation methods and standardized benchmarks. These benchmarks aim to assess core competencies like planning, reasoning, and tool usage.
Developer Impact: Developers will need to leverage these evolving benchmarks to test, validate, and improve the reliability and performance of the agents they build.
Enhanced NLP and Personalization in Conversational AI: Conversational AI agents are advancing to provide more human-like interactions, including better understanding of natural language, emotional cues, and offering highly personalized user experiences.
Developer Impact: Developers creating conversational interfaces must focus on incorporating these advanced NLP and personalization techniques to build more engaging and effective AI agents.
Shift Towards AI-Native Development Infrastructure: The rapid generation of code by AI agents is prompting a re-evaluation of existing development platforms like GitHub and GitLab, which were primarily designed for human development speeds. This points towards a need for infrastructure that can better support AI-driven development rhythms.
Developer Impact: Developers should anticipate changes in their toolchains and workflows, potentially including new platforms designed to handle the increased volume and velocity of AI-generated code and the unique review processes required.
🚀 Top 5 Releases for Agent Developers
Embabel Agent Framework: Launched by Spring founder Rod Johnson, Embabel is a new JVM-based, type-safe framework for building AI-powered applications, particularly for integrating Large Language Models (LLMs) with Java applications using Goal-Oriented Action Planning (GOAP).
Developer Impact: Java developers now have a dedicated, high-level framework that emphasizes production-readiness, type safety, and advanced planning capabilities for creating sophisticated AI agents within the Spring ecosystem.
Digitate's ignio™ Agentic AI Platform Update: Digitate announced a significant update to its ignio™ platform, introducing a suite of purpose-built AI agents aimed at IT operations, SREs, and CIOs to accelerate the move towards autonomous enterprise and ticketless IT operations.
Developer Impact: Developers working on enterprise IT and business
adk web 界面是你的最佳调试工具。在"Events"标签页中,你可以看到 Agent 执行的每个步骤,包括它调用了哪些工具以及 LLM 在思考什么。如果输出不正确,你的第一步应该始终是调整 prompt.py 中的指令。
adk deploy cloud_run 命令将你的 Agent 代码部署到 Google Cloud Run。
确保你已通过 Google Cloud 身份验证(gcloud auth login 和 gcloud config set project),并设置好环境变量,然后使用一行命令将 Agent 部署到 cloud run。
可选但推荐:设置环境变量可以使部署命令更简洁。
# Set your Google Cloud Project ID
export GOOGLE_CLOUD_PROJECT="your-gcp-project-id"
# Set your desired Google Cloud Location
export GOOGLE_CLOUD_LOCATION="us-central1" # Example location
# Set the path to your agent code directory
export AGENT_PATH="./trend_spotter" # Assuming capital_agent is in the current directory
# Set a name for your Cloud Run service (optional)
export SERVICE_NAME="trend-spotter-service"
# Set an application name (optional)
export APP_NAME="trend-spotter-app"
adk deploy cloud_run \
- project=$GOOGLE_CLOUD_PROJECT \
- region=$GOOGLE_CLOUD_LOCATION \
- service_name=$SERVICE_NAME \
- app_name=$APP_NAME \
- with_ui \
$AGENT_PATH
(更多 Cloud Run 部署选项可在此处找到)
你可以通过在 Web 浏览器中导航到部署后提供的 Cloud Run 服务 URL 来测试你的 Agent。(URL 应该类似于:https://your-service-name-abc123xyz.a.run.app)
恭喜!你已成功使用 Agent Development Kit 设计、构建、测试和部署了你的第一个 AI Agent。
你学会了如何构建规范的 Agent 包、如何编写详细的 prompt 来控制 Agent 的逻辑,以及如何使用 adk web 界面运行和交互你的 Agent。我们现在为 AI Agent 播客拥有了一个可用的"研究员",而你也有了一个可以扩展的工作基础。尝试修改 prompt 来研究不同的主题,或探索添加新的自定义工具来为你的 Agent 赋予更多能力。
在我们的下一篇文章中,我们将继续在此基础上进行构建,通过添加更丰富、更专业的工具来使我们的 Agent 更加强大。
当你准备好深入探索框架的所有强大功能时,最好的去处是官方 Google Cloud ADK 文档。