Launcher
Launcherは、ADKエージェントをインタラクティブなコンソールモードとHTTPサーバーモードの両方で実行するための、シンプルで一行で記述できる方法を提供します。CLI引数の解析、Session管理を処理し、Agentをデプロイするための一貫したインターフェースを提供します。
概要
Launcherは、Agentのデプロイを可能な限りシンプルにするように設計されています。たった一行のコードで、以下のことが可能になります。
- テストおよび開発のために、Agentをインタラクティブなコンソールで実行する
- AgentをWeb UI付きのHTTPサーバーとしてデプロイする
- アプリケーション名とアーティファクトストレージをカスタマイズする
基本的な使い方
コンソールモード (デフォルト)
Launcherを使用する最も簡単な方法は、Agentでインスタンスを作成し、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-2.5-flash")?);
let agent = LlmAgentBuilder::new("my_agent")
.description("A helpful assistant")
.instruction("You are a helpful assistant.")
.model(model)
.build()?;
// Run with CLI support (console by default)
Launcher::new(Arc::new(agent)).run().await
}
Agentを実行します。
# Interactive console (default)
cargo run
# Or explicitly specify console mode
cargo run -- chat
サーバーモード
AgentをWeb UI付きのHTTPサーバーとして実行するには:
# Start server on default port (8080)
cargo run -- serve
# Start server on custom port
cargo run -- serve --port 3000
サーバーが起動し、次のように表示されます。
🚀 ADK Server starting on http://localhost:8080
📱 Open http://localhost:8080 in your browser
Press Ctrl+C to stop
設定オプション
カスタムアプリケーション名
デフォルトでは、LauncherはAgentの名前をアプリケーション名として使用します。これをカスタマイズできます。
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は以下の処理を行います。
- インメモリのSessionサービスを作成します。
- ユーザーのためにSessionを作成します。
- インタラクティブなREPLループを開始します。
- Agentの応答をリアルタイムでストリーミングします。
- マルチAgentシステムにおけるAgent間の引き継ぎを処理します。
コンソール操作
🤖 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!
マルチAgentコンソール
マルチAgentシステムを使用する場合、コンソールはどのAgentが応答しているかを表示します。
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は以下の処理を行います。
- 可観測性のためのテレメトリーを初期化します
- インメモリのSessionサービスを作成します
- REST APIエンドポイントを持つHTTPサーバーを起動します
- エージェントとやり取りするためのWeb UIを提供します
利用可能なエンドポイント
サーバーは以下のREST APIエンドポイントを公開しています。
GET /health- ヘルスチェックエンドポイントPOST /run_sse- Server-Sent Eventsストリーミングでエージェントを実行GET /sessions- Session一覧の取得POST /sessions- 新しいSessionの作成GET /sessions/:app_name/:user_id/:session_id- Session詳細の取得DELETE /sessions/:app_name/:user_id/:session_id- Sessionの削除
詳細なエンドポイント仕様については、Server APIドキュメントを参照してください。
Web UI
サーバーには、http://localhost:8080/ui/でアクセスできる組み込みのWeb UIが含まれています。このUIは以下を提供します。
- インタラクティブなチャットインターフェース
- Session管理
- リアルタイムのストリーミング応答
- マルチエージェントの可視化
CLI引数
Launcherは以下のCLIコマンドをサポートしています。
| Command | Description | Example |
|---|---|---|
| (none) | 対話型コンソール(デフォルト) | cargo run |
chat | 対話型コンソール(明示的) | cargo run -- chat |
serve | HTTPサーバーモード | cargo run -- serve |
serve --port PORT | カスタムポートでのHTTPサーバー | 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-2.5-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 (supports both console and server modes via CLI)
Launcher::new(Arc::new(agent))
.app_name("weather_app")
.run()
.await
}
コンソールモードで実行:
cargo run
サーバーモードで実行:
cargo run -- serve --port 8080
ベストプラクティス
- 環境変数: 機密性の高い設定(API keys)は常に環境変数からロードする
- エラーハンドリング:
Result型を使用して適切なエラーハンドリングを行う - グレースフルシャットダウン: Launcherは両方のモードでCtrl+Cをグレースフルに処理します
- ポート選択: 他のサービスと競合しないポートを選択する(デフォルトは8080)
- Session管理: 本番環境では、インメモリSessionの代わりに
DatabaseSessionServiceの使用を検討する
関連情報
- Server API - 詳細なREST APIドキュメント
- Sessions - Session管理
- Artifacts - Artifactストレージ
- Observability - Telemetryとログ記録
前へ: ← Telemetry | 次へ: Server →