iT邦幫忙

2025 iThome 鐵人賽

DAY 20
0
生成式 AI

我的 AI 學習之路:30天 Gemma 與 Gemini系列 第 20

我的 AI 學習之路:第20天 Gemma 與 Gemini - Agent Development Kit

  • 分享至 

  • xImage
  •  

什麼是ADK (Agent Development Kit)

Agent Development Kit(ADK)是 Google 開發的一個開源框架,旨在簡化和加速 AI 代理(Agent)的開發、測試與部署。

簡單來說,ADK 讓 AI 代理的開發更像傳統的軟體開發,提供了一套結構化的工具,幫助開發者:

 - 建立多代理系統: ADK 的主要特點之一是專為多代理協作而設計。開發者可以建立一個由多個專業化代理組成的團隊,讓它們互相溝通、委派任務,以解決更複雜的問題,例如一個旅遊規劃代理團隊,裡面可能包含負責查詢航班、預訂飯店、提供景點建議等不同功能的代理。

 - 賦予代理工具: ADK 讓開發者可以輕鬆地為代理配備各種工具,例如網路搜尋、程式碼執行、與外部 API 互動等,讓代理能執行實際行動,而不只是產生文字回應。

 - 簡化開發流程: 它提供了彈性的編排方式,無論是定義固定的任務流程(例如:依序執行 A、B、C)還是讓大型語言模型 (LLM) 動態決定下一步行動,ADK 都能支援。

 - 內建評估工具: 開發者可以透過 ADK 內建的工具,系統性地評估代理的效能,確保它們在不同情境下的表現符合預期。

設定環境並安裝ADK

創建並啟動虛擬環境

    python -m venv .venv

安裝 ADK

    pip install google-adk

建立代理項目

建立以下專案結構:

parent_folder/
    multi_tool_agent/
        __init__.py
        agent.py
        .env

建立資料夾multi_tool_agent

    mkdir multi_tool_agent/

init.py

 echo "from . import agent" > multi_tool_agent/__init__.py

multi_tool_agent/init.py 的內容

    from . import agent

agent.py

    touch multi_tool_agent/agent.py

agent.py 的內容

import datetime
from zoneinfo import ZoneInfo
from google.adk.agents import Agent

def get_weather(city: str) -> dict:
    """Retrieves the current weather report for a specified city.

    Args:
        city (str): The name of the city for which to retrieve the weather report.

    Returns:
        dict: status and result or error msg.
    """
    if city.lower() == "new york":
        return {
            "status": "success",
            "report": (
                "The weather in New York is sunny with a temperature of 25 degrees"
                " Celsius (77 degrees Fahrenheit)."
            ),
        }
    else:
        return {
            "status": "error",
            "error_message": f"Weather information for '{city}' is not available.",
        }


def get_current_time(city: str) -> dict:
    """Returns the current time in a specified city.

    Args:
        city (str): The name of the city for which to retrieve the current time.

    Returns:
        dict: status and result or error msg.
    """

    if city.lower() == "new york":
        tz_identifier = "America/New_York"
    else:
        return {
            "status": "error",
            "error_message": (
                f"Sorry, I don't have timezone information for {city}."
            ),
        }

    tz = ZoneInfo(tz_identifier)
    now = datetime.datetime.now(tz)
    report = (
        f'The current time in {city} is {now.strftime("%Y-%m-%d %H:%M:%S %Z%z")}'
    )
    return {"status": "success", "report": report}


root_agent = Agent(
    name="weather_time_agent",
    model="gemini-2.5-flash",
    description=(
        "Agent to answer questions about the time and weather in a city."
    ),
    instruction=(
        "You are a helpful agent who can answer user questions about the time and weather in a city."
    ),
    tools=[get_weather, get_current_time],
)

.env

touch multi_tool_agent/.env

.env 的內容

GOOGLE_GENAI_USE_VERTEXAI=TRUE
GOOGLE_API_KEY=PASTE_YOUR_ACTUAL_EXPRESS_MODE_API_KEY_HERE

啟動開發 UI

    adk web

https://ithelp.ithome.com.tw/upload/images/20250921/20121643hmi266xd9k.png

agent 執行結果

https://ithelp.ithome.com.tw/upload/images/20250921/20121643XLrBwUHErO.png

https://ithelp.ithome.com.tw/upload/images/20250921/20121643UKPMRdl7F8.png

總結

ADK有提供 command line 和 web 介面,還滿方便的,滿足了不同角色與不同階段的需求。它不僅讓 AI 代理的開發更像現代軟體工程,也讓整個開發、測試與維運的生命週期變得更為順暢。這也是為什麼 ADK 被認為是建構強大、可靠的 AI 代理系統的一個重要工具。

上一篇
我的 AI 學習之路:第19天 Gemma 與 Gemini - Gemini Cli + MCP
下一篇
我的 AI 學習之路:第21天 Gemma 與 Gemini - MCP 部署 Cloud Run , 加入Cli
系列文
我的 AI 學習之路:30天 Gemma 與 Gemini22
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言