クイックスタート: エージェント開発キットを使用してエージェントをビルドする

このクイックスタートでは、 Google Cloud プロジェクトの設定、エージェント開発キット(ADK)のインストール、基本的なエージェントの設定、デベロッパー ユーザー インターフェースの実行について説明します。

このクイックスタートでは、Python 3.10 以降とターミナル アクセスを備えたローカル IDE(VS Code、PyCharm など)を使用していることを前提としています。エージェントはマシン上で完全に実行されます。これは、ローカル アプリケーションの開発に推奨されます。

始める前に

次の手順を行います。

Google Cloud プロジェクトを設定する

Google Cloud プロジェクトを設定し、Vertex AI API を有効にします。

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Vertex AI API.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Vertex AI API.

    Enable the API

認証情報の設定

ローカル ターミナルで Google Cloud CLI を設定して認証します。Google AI Studio の Gemini API に精通している場合は、Vertex AI Gemini API では API キーではなく、Identity and Access Management を使用してアクセスを管理することに注意してください。

  1. Google Cloud CLI をインストールして初期化する

  2. すでに gcloud CLI をインストールしている場合は、このコマンドを実行して gcloud コンポーネントが更新されていることを確認します。

    gcloud components update
  3. 次のコマンドを実行して、ローカルのアプリケーションのデフォルト認証情報(ADC)ファイルを生成します。エージェントは、ローカル アプリケーションの開発中にこれらの認証情報を使用して Vertex AI にアクセスします。

    gcloud auth application-default login

    詳細については、アプリケーションのデフォルト認証情報を設定するをご覧ください。

仮想環境を設定して ADK をインストールする

  • 仮想環境を作成して有効にします(推奨)。

    # Create
    python -m venv .venv
    # Activate (uncomment the line relevant to your environment)
    # macOS/Linux: source .venv/bin/activate
    # Windows CMD: .venv\Scripts\activate.bat
    # Windows PowerShell: .venv\Scripts\Activate.ps1
    
  • ADK をインストールします。

    pip install google-adk
    

エージェントを作成する

ターミナルを使用して、フォルダ構造を作成します。

mkdir multi_tool_agent/
touch \
multi_tool_agent/__init__.py \
multi_tool_agent/agent.py \
multi_tool_agent/.env

構造:

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

作成した次の 3 つのファイルに次のコードをコピーして貼り付けます。

__init__.py

from . import agent

.env

# If using Gemini via Vertex AI on Google CLoud
GOOGLE_CLOUD_PROJECT="your-project-id"
GOOGLE_CLOUD_LOCATION="your-location" #e.g. us-central1
GOOGLE_GENAI_USE_VERTEXAI="True"

agent.py

from google.adk.agents import Agent

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

    Returns:
        dict: A dictionary containing the weather information with a 'status' key ('success' or 'error') and a 'report' key with the weather details if successful, or an 'error_message' if an error occurred.
    """
    if city.lower() == "new york":
        return {"status": "success",
                "report": "The weather in New York is sunny with a temperature of 25 degrees Celsius (41 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:
        dict: A dictionary containing the current time for a specified city information with a 'status' key ('success' or 'error') and a 'report' key with the current time details in a city if successful, or an 'error_message' if an error occurred.
    """
    import datetime
    from zoneinfo import ZoneInfo

    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)
    return {"status": "success",
            "report": f"""The current time in {city} is {now.strftime("%Y-%m-%d %H:%M:%S %Z%z")}"""}

root_agent = Agent(
    name="weather_time_agent",
    model="gemini-2.0-flash",
    description="Agent to answer questions about the time and weather in a city.",
    instruction="I can answer your questions about the time and weather in a city.",
    tools=[get_weather, get_current_time]
)

エージェントには、モック実装を備えた 2 つの関数ツールが装備されています。

エージェントを実行してテストする

  1. ターミナルで、エージェントの親ディレクトリに移動します(cd .. を使用するなど)。

    parent_folder/      <-- navigate to this directory
        multi_tool_agent/
            __init__.py
            agent.py
            .env
    
  2. 次のコマンドを実行して、デベロッパー向けウェブ UI を起動します。

    adk web
    

    指定された URL(通常は http://localhost:8000 または http://127.0.0.1:8000)をブラウザで開きます。この接続は完全にローカルマシンに留まります。multi_tool_agent を選択してエージェントとやり取りします。

試すプロンプトの例

次のようなプロンプトを試すことができます。

  • ニューヨークの天気は?
  • ニューヨークは何時?
  • パリの天気は?
  • パリは何時?

次のステップ