Google ADK: Building Multi-Agent Systems

Agent Types in ADK

In the Agent Development Kit (ADK), an agent is an independent execution unit designed to act autonomously to achieve specific goals. Google ADK provides different types of agents that can be extended from the base Agent class.

Agent Types in Google ADK showing LLM-based agents, workflow agents (Sequential, Parallel, Loop), and Custom Logic agents

The diagram illustrates the primary agent types in Google ADK:

  1. A. LLM-Based Agents: LlmAgent for reasoning, tool usage, and transfer capabilities
  2. B. Workflow Agents: Including SequentialAgent, ParallelAgent, and LoopAgent for structured processes
  3. C. Custom Logic: CustomAgent for specialized implementations

Official Resources

For comprehensive documentation, tutorials, and API references, visit the official Google Agent Development Kit website:

Google ADK Documentation

Overview

Google has announced a Multi-Agent Interaction Communication Protocol (Agent to Agent, Protocol A to A) that enables agents from different vendors and platforms to communicate and collaborate. Along with this protocol, Google has open-sourced the Agent Developer Kit (ADK) and an agent marketplace.

What is ADK?

ADK is a flexible, modular framework for developing and deploying AI agents. It works with popular LLMs and open-source AI tools, with special integration for Google's ecosystem and Gemini models. It allows developers to easily create simple agents powered by Gemini models and Google AI tools, while providing structure for more complex agent architectures.

Key Features:

Flexible Orchestration: Define workflows for predictable channels or use model-driven dynamic routing
Multi-Agent Architecture: Combine specialized agents in hierarchical structures
Rich Tool Ecosystem: Use pre-built tools, custom functionality, and third-party libraries
Deployment Options: Run locally, scale with Vertex AI Engine, or integrate with Cloud Run or Docker

Agent Engines

The foundation of all agents in ADK is the Base Agent class, which can be extended in three main ways:

  1. LLM Agents: Use LLM models as core engines for reasoning, planning, and dynamic tool selection
  2. Workflow Agents: Use predefined control paths for predictable, structured processes
  3. Custom Agents: Extend the Base Agent directly for specialized logic and integration

Building Your First Agent

Prerequisites:

  • Install Google ADK
  • Install LiteLLM

Implementation Steps:

  1. Create a project directory (e.g., my_project) with a subdirectory (e.g., hello_adk)
  2. Create three files in the subdirectory:
    • .env (for API keys if needed)
    • __init__.py (package initialization)
    • agent.py (main engine file)
agent.py
# Import necessary packages
from litellm import LiteLLM
from google.adk.agent import Agent

# Define LLM agent
model = LiteLLM("ollama/qwen:7b")  # Using Qwen model through Ollama

agent = Agent(
    engine="LLM",
    name="Base Agent",
    description="A programming development assistant",
    instruction="You are a programming development assistant"
)
  1. Run the agent from the my_project directory
  2. Access the provided web interface
  3. Select your agent from the dropdown menu
  4. Start interacting with your agent (e.g., "Write a simple web scraper program")

Code Analysis: Implementing ADK Agents

Let's analyze the code structure for a Google ADK agent implementation.

__init__.py Analysis

__init__.py
#package的初始文件
from . import agent

This is a standard Python package initialization file that imports the agent module from the current package. This makes the agent module accessible when importing the package. The comment at the top indicates this is a package initialization file in Chinese.

agent.py Analysis

agent.py
#執行 adk web
from google.adk.agents import LlmAgent
from google.adk.models.lite_llm import LiteLlm

root_agent = LlmAgent(
    model=LiteLlm(model="ollama/qwen2.5:latest"),
    name="base_agent",
    description=(
        "你是一位程式開發助手."
    ),
    instruction=(
        "你是一位程式開發助手"
    ),
)

The agent.py file implements a Google ADK agent with the following components:

  • The file imports LlmAgent from google.adk.agents - this is a class for creating language model-based agents
  • It also imports LiteLlm from google.adk.models.lite_llm - this is a wrapper for connecting to various LLM providers
  • The code creates a variable root_agent that instantiates an LlmAgent with the following parameters:
    • model: Uses LiteLlm to connect to the Qwen 2.5 model via Ollama (a local model server)
    • name: Sets the agent's name to "base_agent"
    • description: Provides a description in Chinese stating "You are a programming development assistant"
    • instruction: Sets instructions, also in Chinese, with the same message

Key Differences from the Example in the Tutorial

Comparing the actual code to the tutorial example, there are several differences:

  1. The import paths are different:
    • The example uses from litellm import LiteLLM and from google.adk.agent import Agent
    • The actual code uses from google.adk.agents import LlmAgent and from google.adk.models.lite_llm import LiteLlm
  2. The LLM model and class names differ:
    • The example uses model = LiteLLM("ollama/qwen:7b") (using Qwen 7B)
    • The actual code uses LiteLlm(model="ollama/qwen2.5:latest") (using the newer Qwen 2.5 model)
  3. The agent instantiation is different:
    • The example uses Agent with engine="LLM" parameter
    • The actual code directly uses the LlmAgent class

This suggests that the code uses a newer or different version of the Google ADK framework than what's described in the tutorial. The implemented agent is a language model-based agent that uses the Qwen 2.5 model through Ollama to act as a programming development assistant.

ADK 中的代理類型

在代理開發工具包 (ADK) 中,代理程式是一個獨立的執行單元,旨在自主行動以實現特定目標。Google ADK 提供了不同類型的代理,可以從基本 Agent 類擴展。

Google ADK 中的代理類型,顯示基於 LLM 的代理、工作流代理(順序、並行、循環)和自定義邏輯代理

該圖表說明了 Google ADK 中的主要代理類型:

  1. A. 基於 LLM 的代理:LlmAgent 用於推理、工具使用和轉移功能
  2. B. 工作流代理:包括 SequentialAgent、ParallelAgent 和 LoopAgent 用於結構化流程
  3. C. 自定義邏輯:CustomAgent 用於專門實現

官方資源

要獲取全面的文檔、教程和 API 參考,請訪問 Google 代理開發工具包官方網站:

Google ADK 文檔

概述

Google 宣布了多代理互動通信協議(Agent to Agent, Protocol A to A),使不同供應商和平台的代理能夠互相溝通和協作。Google 同時開源了代理開發套件(ADK)和代理市集。

什麼是 ADK?

ADK 是一個用於開發和部署 AI 代理的靈活模組化框架。它可以與流行的 LLM 和開源 AI 工具一起使用,特別是與 Google 生態系統和 Gemini 模型緊密整合。它允許開發者輕鬆創建由 Gemini 模型和 Google AI 工具驅動的簡單代理,同時提供更複雜代理架構所需的結構。

主要特點:

靈活編排:定義工作流程實現可預測的通道,或使用模型驅動的動態路由
多代理架構:在層次結構中組合多個專用代理
豐富工具生態系統:使用預建工具、自訂功能和第三方庫
部署選項:本地運行、使用 Vertex AI Engine 擴展,或整合到 Cloud Run 或 Docker

代理引擎

ADK 中所有代理的基礎是 Base Agent 類別,可通過三種主要方式擴展:

  1. LLM 代理:使用 LLM 模型作為核心引擎進行推理、規劃和動態工具選擇
  2. 工作流代理:使用預定義的控制路徑實現可預測、結構化的流程
  3. 自訂代理:直接擴展 Base Agent 以實現特殊邏輯和整合

建立您的第一個代理

先決條件:

  • 安裝 Google ADK
  • 安裝 LiteLLM

實施步驟:

  1. 創建項目目錄(例如 my_project)和子目錄(例如 hello_adk
  2. 在子目錄中創建三個文件:
    • .env(如需 API 密鑰)
    • __init__.py(套件初始化)
    • agent.py(主引擎文件)
agent.py
# 導入必要包
from litellm import LiteLLM
from google.adk.agent import Agent

# 定義 LLM 代理
model = LiteLLM("ollama/qwen:7b")  # 通過 Ollama 使用千問模型

agent = Agent(
    engine="LLM",
    name="Base Agent",
    description="一位程式開發助手",
    instruction="你是一位程式開發助手"
)
  1. my_project 目錄運行代理
  2. 訪問提供的網頁界面
  3. 從下拉菜單中選擇您的代理
  4. 開始與您的代理互動(例如,"幫我寫一個簡單的網頁爬蟲程式")

代碼分析:實現 ADK 代理

讓我們分析 Google ADK 代理實現的代碼結構。

__init__.py 分析

__init__.py
#package的初始文件
from . import agent

這是一個標準的 Python 包初始化文件,從當前包中導入 agent 模塊。這使得在導入包時可以訪問 agent 模塊。頂部的註釋說明這是一個包初始化文件。

agent.py 分析

agent.py
#執行 adk web
from google.adk.agents import LlmAgent
from google.adk.models.lite_llm import LiteLlm

root_agent = LlmAgent(
    model=LiteLlm(model="ollama/qwen2.5:latest"),
    name="base_agent",
    description=(
        "你是一位程式開發助手."
    ),
    instruction=(
        "你是一位程式開發助手"
    ),
)

agent.py 文件實現了一個 Google ADK 代理,具有以下組件:

  • google.adk.agents 導入 LlmAgent - 這是用於創建基於語言模型的代理的類
  • google.adk.models.lite_llm 導入 LiteLlm - 這是一個用於連接各種 LLM 提供商的包裝器
  • 創建一個名為 root_agent 的變量,它實例化了一個 LlmAgent,具有以下參數:
    • model:使用 LiteLlm 通過 Ollama(一個本地模型服務器)連接到 Qwen 2.5 模型
    • name:將代理的名稱設置為 "base_agent"
    • description:提供一個中文描述,說明 "你是一位程式開發助手"
    • instruction:設置指令,同樣是中文,內容相同

與教程示例的主要差異

比較實際代碼與教程示例,有幾個差異:

  1. 導入路徑不同:
    • 示例使用 from litellm import LiteLLMfrom google.adk.agent import Agent
    • 實際代碼使用 from google.adk.agents import LlmAgentfrom google.adk.models.lite_llm import LiteLlm
  2. LLM 模型和類名稱不同:
    • 示例使用 model = LiteLLM("ollama/qwen:7b")(使用 Qwen 7B)
    • 實際代碼使用 LiteLlm(model="ollama/qwen2.5:latest")(使用更新的 Qwen 2.5 模型)
  3. 代理實例化不同:
    • 示例使用 Agent 並帶有 engine="LLM" 參數
    • 實際代碼直接使用 LlmAgent

這表明代碼使用的是比教程中描述的更新或不同版本的 Google ADK 框架。實現的代理是一個基於語言模型的代理,它使用通過 Ollama 運行的 Qwen 2.5 模型來作為程式開發助手。