Back to Blog
MediumImported

[ Google ADK ] 來用 yaml 格式來開發 AI Agent 吧!

💡 本文同步發表於 Medium:原文連結

https://medium.com/@simon3458/google-adk-yaml-config-ai-agent-intro-2025-fc6ec5230a55

點擊查看連結詳情...

https://medium.com/@simon3458/google-adk-yaml-config-ai-agent-intro-2025-fc6ec5230a55

[ Google ADK ] 來用 yaml 格式來開發 AI Agent 吧!

備註:用 yaml 格式設定 Agent 的方法目前仍在持續開發中,少部分原本 ADK 擁有功能可能無法使用,如有需要完整功能,可先以 Python 做開發。

過往,我們在使用 Google ADK 工具進行開發時,我們會選擇使用 Python 或者 Java 來開發 AI Agent ,但是,你現在有一個另外的新選擇,就是使用 YAML 格式來進行開發。

本篇文章,將帶你使用 YAML 檔案來開發 Google ADK AI Agent。

I. Google ADK 介紹

你可以先閱讀此篇文章,來了解 Google ADK:

https://medium.com/@simon3458/google-adk-tools-intro-202504-3181fd6ab567

點擊查看連結詳情...

https://medium.com/@simon3458/google-adk-tools-intro-202504-3181fd6ab567

II. 基礎 YAML 設定格式

ADK Agent YAML 設定功能主要是讓您無需編寫程式碼,或者專注於編寫客製化工具程式碼,即可建置 ADK AI Agent。以下是 ADK AI Agent 配置定義的最小範例:

CODE
name: assistant_agent  
model: gemini-2.5-flash

您可以基於此格式,使用 Agent YAML 檔案建立更複雜的 Agent,這些 Agent 可以包含函數、工具、Sub-Agent 等。本頁介紹如何使用 Agent 設定功能建置和執行 ADK 工作流程。

III. 透過 SDK 建構 ADK AI Agent

您可以使用adk 的 SDK 指令,來創建基礎 ADK Agent 專案設定檔案,然後透過編輯root_agent.yaml來獲得正確的 instruction 設定,步驟如下:

步驟一:在終端機視窗中,執行以下命令來建立 Agent:

CODE
adk create --type=config my_agent
  • 這個指令產生一個my_agent/資料夾,裡麵包含一個 root_agent.yaml檔案和一個.env檔案。

步驟二:在該my_agent/.env文件中,為您的 Agent 設定環境變數以存取生成式 AI 模型和其他服務

CODE
GOOGLE_GENAI_USE_VERTEXAI=0  
GOOGLE_API_KEY=<your-Google-Gemini-API-key>
  • 若要透過 Google Cloud 存取 Gemini 模型,請在文件中新增以下環境變數:
CODE
GOOGLE_GENAI_USE_VERTEXAI=1  
GOOGLE_CLOUD_PROJECT=<your_gcp_project>  
GOOGLE_CLOUD_LOCATION=global

步驟三:編輯 Agent Config 文件 my_agent/root_agent.yaml,如下所示:

CODE
# yaml-language-server: $schema=https://raw.githubusercontent.com/google/adk-python/refs/heads/main/src/google/adk/agents/config_schemas/AgentConfig.json  
name: assistant_agent  
model: gemini-2.5-flash  
description: A helper agent that can answer users' questions.  
instruction: You are an agent to help answer users' various questions.

步驟四:透過 adk web 或者 adk api-server 即可啟動服務並且進行測試。

IV. 加入 Tools 進入 ADK Agent

1. 內建工具範例

你可以使用 Google ADK 專案內建的工具來進行工具導入,以下範例,是使用 Google 搜尋來作為 ADK Tools 的範例。該 Agent 會自動使用搜尋工具回覆用戶請求。

CODE
# Internal Tool  
name: search_agent  
model: gemini-2.0-flash  
description: 'an agent whose job it is to perform Google search queries and answer questions about the results.'  
instruction: You are an agent whose job is to perform Google search queries and answer questions about the results.  
tools:  
  - name: google_search

目前支援的內建工具如下,會陸續更新後持續加入更多內建工具:

  • google_search
  • load_artifacts
  • url_context
  • exit_loop
  • preload_memory
  • get_user_choice
  • enterprise_web_search
  • load_web_page:需要完全限定的路徑才能存取網頁。

2. 自訂工具範例

你也可以自己撰寫自己的工具,並且透過引用的方式進行工具導入,以下範例使用 Python 程式碼建構的自訂工具,該工具列在tools:設定檔的相應部分。

CODE
# Custom Tool  
agent_class: LlmAgent  
model: gemini-2.5-flash  
name: prime_agent  
description: Handles checking if numbers are prime.  
instruction: |  
  You are responsible for checking whether numbers are prime.  
  When asked to check primes, you must call the check_prime tool with a list of integers.  
  Never attempt to determine prime numbers manually.  
  Return the prime number results to the root agent.  
tools:  
  - name: ma_llm.check_prime

3. Sub-Agent 範例

除了工具的導入,若你已經有其他的 Agent,希望能夠作為此專案的 Sub-Agent,那你也可以透過 sub_agents的 tag 進行導入,以下範例展示了一個在 sub_agents:部分中定義了兩個 Sub-Agents ,該 Agent 會根據使用者的需求,並委託其中的 Sub-Agent 程式來解析請求。

CODE
# SubAgent  
agent_class: LlmAgent  
model: gemini-2.5-flash  
name: root_agent  
description: Learning assistant that provides tutoring in code and math.  
instruction: |  
  You are a learning assistant that helps students with coding and math questions.You delegate coding questions to the code_tutor_agent and math questions to the math_tutor_agent.  
  Follow these steps:  
  1. If the user asks about programming or coding, delegate to the code_tutor_agent.  
  2. If the user asks about math concepts or problems, delegate to the math_tutor_agent.  
  3. Always provide clear explanations and encourage learning.  
sub_agents:  
  - config_path: code_tutor_agent.yaml  
  - config_path: math_tutor_agent.yaml

V. 已知限制

Agent 配置功能尚處於實驗階段,有以下限制:

  • **模型支援:**目前僅支援 Gemini 模型。第三方模型的整合正在進行中。
  • **程式語言:**Agent 配置功能目前僅支援需要程式碼的工具和其他功能的 Python 程式碼。
  • Agent **類型支援:**和LangGraphAgent類型A2aAgent尚不受支援。

VI. 結論

透過 YAML 格式開發 Google ADK Agent,代表著 AI 工程開發方式正逐步邁向更高層次的抽象化與模組化。這不僅讓開發者能以宣告式語言快速構建具備功能性的 AI Agent,也大幅降低了技術門檻,讓團隊能更專注於邏輯設計與整體架構思維,而非陷入繁瑣的程式碼細節中。

目前 YAML 配置仍處於實驗階段,功能尚未完全成熟,但這項技術方向極具潛力。未來隨著 Google ADK 工具鏈的完善與生態系擴展,YAML 型 Agent 開發將有機會成為企業級 AI 解決方案設計的標準流程之一。

相關 YAML Agent Config ADK Agent 文件

Agent Development Kit (ADK)

Build powerful multi-agent systems with Agent Development Kit (ADK)

https://google.github.io/adk-docs/agents/config/#build-an-agent

I am Simon

大家好,我是 Simon 劉育維,是一位 AI 領域解決方案專家,目前也擔任 Google GenAI 領域開發者專家 (GDE),期待能夠幫助企業導入人工智慧相關技術解決問題。如果這篇文章對您有幫助,請在 Medium 上按一下鼓勵,並追蹤我的個人帳號,這樣您就可以隨時閱讀我所撰寫的文章。歡迎在我的 Linkedin 上留言提供意見,並與我一起討論有關人工智慧的主題,期待能夠對大家有所幫助!

My Personal Website: https://simonliuyuwei.my.canva.site/link-in-bio

這篇文章有幫上忙嗎?

留言

載入留言中…