起動機能

Launcherは、ADKエージェントを1行で実行するシンプルな方法を提供します。デフォルトの最小構成では、adk-runnerによる軽量なコンソール起動機能です。完全なCLI引数パーサーとHTTPサーバーモードが必要な場合は、cli-openaiなどのオプトインCLI機能を有効にします。

概要

Launcherは、エージェントのデプロイを可能な限り簡単にするよう設計されています。1行のコードで、次のことができます。

  • テストと開発のために、インタラクティブなコンソールでエージェントを実行する
  • cli-*機能またはcargo-adkのapiテンプレートを使用して、Web UI付きのHTTPサーバーとしてエージェントをデプロイする
  • アプリケーション名とアーティファクトストレージをカスタマイズする

基本的な使用方法

コンソールモード(デフォルト)

Launcherを使用する最も簡単な方法は、エージェントを指定して作成し、run()を呼び出すことです。

use adk_rust::prelude::*;
use adk_rust::Launcher;
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<()> {
    let api_key = std::env::var("GOOGLE_API_KEY")?;
    let model = Arc::new(GeminiModel::new(&api_key, "gemini-3.7-flash")?);
    
    let agent = LlmAgentBuilder::new("my_agent")
        .description("A helpful assistant")
        .instruction("You are a helpful assistant.")
        .model(model)
        .build()?;
    
    // Run with the lightweight console launcher
    Launcher::new(Arc::new(agent)).run().await
}

エージェントを実行します。

# Interactive console (default)
cargo run

# Full CLI mode is available when your app enables a `cli-*` feature

サーバーモード

Web UI付きのHTTPサーバーとしてエージェントを実行するには、apiテンプレートを使用するか、cli-*機能を有効にします。

cargo adk new my-api --template api
cd my-api
cargo run

サーバーが起動し、次のように表示されます。

🚀 ADK Server starting on http://localhost:8080
📱 Open http://localhost:8080 in your browser
Press Ctrl+C to stop

設定オプション

カスタムアプリケーション名

デフォルトでは、Launcherはエージェント名をアプリケーション名として使用します。次のようにカスタマイズできます。

Launcher::new(Arc::new(agent))
    .app_name("my_custom_app")
    .run()
    .await

カスタムアーティファクトサービス

独自のアーティファクトサービス実装を指定します。

use adk_artifact::InMemoryArtifactService;

let artifact_service = Arc::new(InMemoryArtifactService::new());

Launcher::new(Arc::new(agent))
    .with_artifact_service(artifact_service)
    .run()
    .await

コンソールモードの詳細

コンソールモードでは、Launcherは次の処理を行います。

  1. メモリ内セッションサービスを作成する
  2. ユーザー用のセッションを作成する
  3. インタラクティブなREPLループを開始する
  4. エージェントのレスポンスをリアルタイムでストリーミングする
  5. マルチエージェントシステムでのエージェント転送を処理する

コンソール操作

🤖 Agent ready! Type your questions (or 'exit' to quit).

You: What is the capital of France?
Assistant: The capital of France is Paris.

You: exit
👋 Goodbye!

マルチエージェントコンソール

マルチエージェントシステムを使用すると、コンソールにはどのエージェントが応答しているかが表示されます。

You: I need help with my order

[Agent: customer_service]
Assistant: I'll help you with your order. What's your order number?

You: ORDER-12345

🔄 [Transfer requested to: order_lookup]

[Agent: order_lookup]
Assistant: I found your order. It was shipped yesterday.

サーバーモードの詳細

サーバーモードでは、Launcherは次の処理を行います。

  1. 可観測性のためのテレメトリを初期化します
  2. インメモリセッションサービスを作成します
  3. REST API エンドポイントを備えた HTTP サーバーを起動します
  4. エージェントと対話するためのウェブ UI を提供します

本番環境向けの切り替え

カスタムルート、ミドルウェア、メトリクス、またはサーブ ループの制御が必要な本番アプリケーションでは、build_app() を使用します。

let app = Launcher::new(Arc::new(agent))
    .with_a2a_base_url("https://agent.example.com")
    .build_app()?;

let app = app.merge(my_admin_routes());
let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await?;
axum::serve(listener, app).await?;

A2A ルートを明示的に有効にする場合は、build_app_with_a2a(...) を使用します。

利用可能なエンドポイント

サーバーは、次の REST API エンドポイントを公開します。

  • GET /health - ヘルスチェックエンドポイント
  • POST /run_sse - Server-Sent Events ストリーミングでエージェントを実行
  • GET /sessions - セッションを一覧表示
  • POST /sessions - 新しいセッションを作成
  • GET /sessions/:app_name/:user_id/:session_id - セッションの詳細を取得
  • DELETE /sessions/:app_name/:user_id/:session_id - セッションを削除

詳細なエンドポイント仕様については、サーバー API のドキュメントを参照してください。

ウェブ UI

サーバーには、http://localhost:8080/ui/ でアクセスできる組み込みのウェブ UI が含まれています。UI では次の操作を行えます。

  • 対話型チャットインターフェース
  • セッション、状態、アーティファクト、イベントタイムラインの検査
  • リアルタイムのストリーミング応答とツールのアクティビティ
  • チーム委任とハンドオフトポロジーの正確な確認
  • エージェントおよび UI プロトコルの機能検出

CLI 引数

完全な CLI ランチャーでは、cli-* 機能が有効になっている場合、次のコマンドをサポートします。

コマンド説明例
(なし)対話型コンソール(デフォルト)cargo run
chat対話型コンソール(明示的)cargo run -- chat
serveHTTP サーバーモードcargo run -- serve
serve --port PORTHTTP カスタムポートでサーバー起動cargo run -- serve --port 3000

完全な例

両方のモードを示す完全な例を次に示します。

use adk_rust::prelude::*;
use adk_rust::Launcher;
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<()> {
    // Load API key
    let api_key = std::env::var("GOOGLE_API_KEY")
        .expect("GOOGLE_API_KEY environment variable not set");
    
    // Create model
    let model = Arc::new(GeminiModel::new(&api_key, "gemini-3.7-flash")?);
    
    // Create agent with tools
    let weather_tool = FunctionTool::new(
        "get_weather",
        "Get the current weather for a location",
        |params, _ctx| async move {
            let location = params["location"].as_str().unwrap_or("unknown");
            Ok(json!({
                "location": location,
                "temperature": 72,
                "condition": "sunny"
            }))
        },
    );
    
    let agent = LlmAgentBuilder::new("weather_agent")
        .description("An agent that provides weather information")
        .instruction("You are a weather assistant. Use the get_weather tool to provide weather information.")
        .model(model)
        .tool(Arc::new(weather_tool))
        .build()?;
    
    // Run with Launcher. Enable a `cli-*` feature for full CLI/server mode.
    Launcher::new(Arc::new(agent))
        .app_name("weather_app")
        .run()
        .await
}

コンソールモードで実行します。

cargo run

生成された API プロジェクトからサーバーモードで実行します。

cargo adk new weather-api --template api
cd weather-api
cargo run

デプロイ前の検証

エージェントをデプロイする前に、cargo adk build を使用して、実際にデプロイせずにプロジェクトが正しくコンパイルされることを確認します。

# Verify compilation (no deployment)
cargo adk build

# Build with release optimizations
cargo adk build --release

これにより、デプロイを確定する前に、コンパイルエラー、依存関係の不足、設定の問題を検出できます。cargo adk deploy 前のゲートとして、CI パイプラインで特に役立ちます。

コマンドの詳細なドキュメントについては、cargo adk build を参照してください。

ベストプラクティス

  1. 環境変数: 機密性の高い設定(API キー)は、必ず環境変数から読み込む
  2. エラーハンドリング: Result 型を使用して適切にエラーを処理する
  3. グレースフルシャットダウン: Launcher は両方のモードで Ctrl+C を適切に処理します
  4. ポートの選択: 他のサービスと競合しないポートを選択する(デフォルトは 8080)
  5. セッション管理: 本番環境では、インメモリセッションの代わりに PostgresSessionService または SqliteSessionService の使用を検討する
  6. デプロイ前チェック: 問題を早期に検出するため、デプロイ前に cargo adk build を実行する

前へ: ← テレメトリ | 次へ: サーバー →