Google ADKを使用して構築されたオブザーバーエージェント

Google エージェント開発キット(ADK)は、インテリジェントなエージェントベースのアプリケーションを構築、調整、および展開するためのフレームワークです。 これにより、エージェントのワークフローを定義し、ツールや外部サービスを統合し、エージェントの推論と実行を管理するための、体系的で拡張性の高い方法が提供されます。 ADKは、タスクの調整、ツールの起動、および大規模言語モデルとの連携のための抽象化機能を提供することで、実運用可能なAIエージェントをサポートするように設計されています。

Instana を使用して Google ADK アプリケーションを監視することで、エージェント主導のワークフローに対する包括的なエンドツーエンドの可観測性を実現できます。 ADKアプリケーションに計測機能を組み込むことで、エージェントのオーケストレーションロジック、ツールの起動、外部サービスへの呼び出し、および基盤となるインフラストラクチャにわたる詳細な実行パスを捕捉できます。

前提条件

以下の前提条件が満たされていることを確認してください:

  • Python 3.8 以降がインストールされている。

  • Geminiモデルにアクセスするための GoogleAPI キーについては、 Google AI Studioを参照してください。

  • Instana アプリケーションに合わせて設定されているか確認するには、 「はじめに」 を参照してください。

Google ADK アプリケーションへの計測機能の組み込み

Google ADK アプリケーションに Instana の可観測性を組み込むには、以下の手順を実行してください:

  1. Google ADKに必要なパッケージをインストールします:

    pip install google-adk traceloop-sdk
  2. 新しい Google ADKエージェントプロジェクトを作成します:

    adk create test_agent

    これにより、次のような構造を持つプロジェクトが作成されます。ここで、 agent.py にはエージェントの中核となる制御ロジックが含まれています:

    図 1. エージェントのファイル構造
    エージェントのファイル構造
  3. ファイル .env 内の ` Google ` および ` API ` キーを設定してください:

    GOOGLE_API_KEY=<your-google-api-key>

    API のキーを作成または管理するには、 Google AI Studio にアクセスしてください。

  4. OpenLLMetry の計測機能を備えたマルチエージェント・アプリケーションを作成するには、次のコードでファイルを agent.py 更新してください:

    from google.adk.agents.llm_agent import Agent
    from traceloop.sdk import Traceloop
    
    # Initialize OpenLLMetry with app name
    Traceloop.init(app_name="multi_agent_adk_app")
    
    # Tool implementations
    def get_current_time(city: str) -> dict:
        """Returns the current time for a given city."""
        return {"city": city, "time": "10:30 AM"}
    
    def get_weather(city: str) -> dict:
        """Returns the current weather for a given city."""
        return {"city": city, "weather": "Sunny", "temperature": "30°C"}
    
    
    # Time Agent
    time_agent = Agent(
        name="time_agent",
        model="gemini-2.0-flash-exp",
        description="Provides the current time for a city.",
        instruction=(
            "You provide the current time for a given city. "
            "Use the get_current_time tool to fetch the information."
        ),
        tools=[get_current_time],
    )
    
    # Weather Agent
    weather_agent = Agent(
        name="weather_agent",
        model="gemini-2.0-flash-exp",
        description="Provides the current weather for a city.",
        instruction=(
            "You provide the current weather details for a given city. "
            "Use the get_weather tool to fetch the information."
        ),
        tools=[get_weather],
    )
    
    # Root Agent orchestrating other agents
    root_agent = Agent(
        name="root_agent",
        model="gemini-2.0-flash-exp",
        description="Orchestrates multiple agents to answer user queries.",
        instruction=(
            "You are a coordinating agent. Based on the user request, "
            "delegate tasks to the appropriate agent (time_agent or weather_agent) "
            "and combine the responses into a single answer."
        ),
        sub_agents=[time_agent, weather_agent],
    )

    この例では、3つのエージェントを作成します:

    • time_agent : 各都市の現在時刻を提供します

    • weather_agent : 各都市の天気情報を提供します

    • root_agent : 他のエージェントを調整し、ユーザーのリクエストを処理する

  5. Google ADKエージェントを起動します:

    adk run test_agent

    エージェントが起動し、ユーザーの入力を待ちます。 「ニューヨークの天気はどう?」といった質問をすることで、このシステムとやり取りすることができます あるいは「ロンドンは今何時ですか?」

  6. Instana でデータを表示します。

    アプリケーションを実行し、エージェントとやり取りすると、データが Instana のGen AI可観測性ダッシュボードに表示されます:

    図 2. ユーザーからのリクエストへの対応
    ユーザーからのリクエストへの対応
    図 3. Instana 内の痕跡
    Instana 内の痕跡

    ダッシュボードには次のように表示されます:

    • エージェントのオーケストレーションフロー

    • 関数の呼び出し(get_current_time、get_weather)

    • LLMとGeminiモデルの連携

    • 応答時間とトークンの使用状況

トラブルシューティング

トレースが表示されない、または接続エラーが発生するなどの一般的な問題については、 「トラブルシューティング」 を参照してください。

Google API 主なエラー

問題 :エージェントの実行時に認証エラーが発生する。

この問題の解決には、以下の手順をお試しください:

  1. Google の API.env キーがファイルに正しく設定されているか確認してください

  2. API キーがGeminiモデルにアクセスできることを確認してください

  3. ファイル .env がプロジェクトのルートディレクトリにあることを確認してください