iT邦幫忙

2025 iThome 鐵人賽

DAY 11
0
生成式 AI

Multi-Agent 實戰:開發多代理智慧小幫手系列 第 11

【Day 11】 Master Agent 基礎實作(下)

  • 分享至 

  • xImage
  •  

解說程式碼

這次的程式比之前稍微進階一點,所以我會一段一段來解說,重點放在 root_agent 的部分。

匯入 ADK

這行是匯入 Google ADK 的 Agent 類別,我們會用它來建立自己的 AI Agent:

from google.adk.agents import Agent

第一個工具 – add_numbers

這個工具負責處理 加法運算,把兩個數字加總並回傳結果:

def add_numbers(a: int, b: int) -> dict:
    """Adds two numbers and returns the result.
    Args:
        a (int): First number
        b (int): Second number
    Returns:
        dict: status and result
    """
    try:
        result = a + b
        return {"status": "success", "result": f"The sum of {a} and {b} is {result}."}
    except Exception as e:
        return {"status": "error", "error_message": str(e)}

第二個工具 – reverse_text

這個工具用來將使用者輸入的 字串反轉

def reverse_text(text: str) -> dict:
    """Reverses the given text string.
    Args:
        text (str): Input string to reverse
    Returns:
        dict: status and result
    """
    try:
        reversed_text = text[::-1]
        return {"status": "success", "result": f"The reversed text is: {reversed_text}"}
    except Exception as e:
        return {"status": "error", "error_message": str(e)}

建立 root_agent

最後我們建立一個 Agent,並指定它的屬性:

  • name:Agent 名稱,用來識別
  • model:要使用的模型(這裡是 Gemini 2.0 Flash
  • description:簡單描述它能做什麼
  • instruction:給模型的行為指令,告訴它應該如何回應
  • tools:Agent 可以調用的工具函式
root_agent = Agent(
    name="math_text_agent",
    model="gemini-2.0-flash",
    description="Agent to perform simple math operations and text manipulations.",
    instruction="You are a helpful agent who can solve simple math problems and reverse text.",
    tools=[add_numbers, reverse_text],
)

實際測試 Agent

資料夾結構

範例的資料夾結構如下:

ExampleProject
 └─ RootAgentTest
     ├─ __init__.py
     ├─ .env
     └─ RootAgent.py

方法一:使用 Dev UI

  1. 在 VSCode 終端機切換到 ExampleProject,執行:
adk web --port 5678

這裡將 port 指定為 5678

  1. 成功後會看到類似畫面:
    https://ithelp.ithome.com.tw/upload/images/20250925/201684561NrDogkWA7.png
  2. 開啟瀏覽器進入 http://localhost:5678/
    https://ithelp.ithome.com.tw/upload/images/20250925/20168456mDGGTqcUkw.png
  3. 點擊 Select an agent,選擇 RootAgentTest,就能開始互動:
    https://ithelp.ithome.com.tw/upload/images/20250925/201684567hn0fyBqNz.png

範例

  • 使用者輸入加法問題 → Agent 調用 add_numbers → 回傳 The sum of 56 and 46 is 102.
    https://ithelp.ithome.com.tw/upload/images/20250925/20168456XaQEDCzgcq.png
  • 使用者輸入字串 → Agent 調用 reverse_text → 回傳 The reversed text is: olleh
    https://ithelp.ithome.com.tw/upload/images/20250925/20168456rHCZa2pZJg.png

方法二:使用 Terminal

  1. 開啟終端機,切換到 ExampleProject 資料夾,執行:
adk run RootAgentTest
  1. 出現以下畫面後就能開始輸入內容:
    https://ithelp.ithome.com.tw/upload/images/20250925/20168456pbwkUiXW1R.png

範例

  • 輸入加法問題 → 回傳 The sum of 5 and 8 is 13.
    https://ithelp.ithome.com.tw/upload/images/20250925/201684569iRpZggN3w.png
  • 輸入字串 → 回傳 The reversed text is: aheh
    https://ithelp.ithome.com.tw/upload/images/20250925/20168456EHTe2Du1mk.png

小提醒:有些系統需要用 系統管理員身分 開終端機才能正常執行。

延伸方法:用 FastAPI 來執行

除了以上兩種方式,我們也可以把 Agent 包在 FastAPI 裡執行,這樣前端應用就能更方便地和 Agent 串接。


上一篇
【Day 10】 Master Agent 基礎實作(上)
系列文
Multi-Agent 實戰:開發多代理智慧小幫手11
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言