启动器

Launcher 提供了一种简单、一行代码的方式来运行您的 ADK Agent,并内置支持交互式控制台模式和 HTTP 服务器模式。它处理 CLI 参数解析、Session 管理,并提供一致的接口来部署 Agent。

概述

Launcher 旨在使 Agent 部署尽可能简单。通过一行代码,您可以:

  • 在交互式控制台中运行您的 Agent,进行测试和开发
  • 将您的 Agent 部署为带有 Web UI 的 HTTP 服务器
  • 自定义应用程序名称和 artifact 存储

基本用法

控制台模式(默认)

使用 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:

# 交互式控制台(默认)
cargo run

# 或显式指定控制台模式
cargo run -- chat

服务器模式

要将您的 Agent 作为带有 Web UI 的 HTTP 服务器运行:

# 在默认端口(8080)上启动服务器
cargo run -- serve

# 在自定义端口上启动服务器
cargo run -- serve --port 3000

服务器将启动并显示:

🚀 ADK 服务器正在 http://localhost:8080 启动
📱 在浏览器中打开 http://localhost:8080
按 Ctrl+C 停止

配置选项

自定义应用程序名称

默认情况下,Launcher 使用 Agent 的名称作为应用程序名称。您可以自定义此项:

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

自定义Artifact服务

提供您自己的 artifact 服务实现:

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. 创建一个内存中 Session 服务
  2. 为用户创建一个 Session
  3. 启动一个交互式 REPL 循环
  4. 实时流式传输 Agent 响应
  5. 处理多 Agent 系统中的 Agent 转移

控制台交互

🤖 Agent 已准备就绪!输入您的问题(或输入'exit'退出)。

您:法国的首都是哪里?
助手:法国的首都是巴黎。

您:exit
👋 再见!

多Agent控制台

当使用多 Agent 系统时,控制台显示哪个 Agent 正在响应:

您:我的订单需要帮助

[Agent: customer_service]
助手:我将帮助您处理订单。您的订单号是多少?

您:ORDER-12345

🔄 [请求转移到: order_lookup]

[Agent: order_lookup]
助手:我找到了您的订单。订单已于昨天发货。

服务器模式详情

在服务器模式下,Launcher:

  1. 初始化遥测以进行可观测性
  2. 创建一个内存会话服务
  3. 启动一个带有 REST API 端点的 HTTP 服务器
  4. 提供一个 Web UI 以便与您的 Agent 交互

可用端点

服务器公开以下 REST API 端点:

  • GET /health - 健康检查端点
  • POST /run_sse - 使用 Server-Sent Events 流运行 Agent
  • GET /sessions - 列出会话
  • POST /sessions - 创建新会话
  • GET /sessions/:app_name/:user_id/:session_id - 获取会话详情
  • DELETE /sessions/:app_name/:user_id/:session_id - 删除会话

有关详细的端点规范,请参阅 Server API 文档。

Web UI

服务器包含一个内置的 Web UI,可通过 http://localhost:8080/ui/ 访问。该 UI 提供:

  • 交互式聊天界面
  • 会话管理
  • 实时流式响应
  • 多 Agent 可视化

CLI 参数

Launcher 支持以下 CLI 命令:

命令描述示例
(无)交互式控制台 (默认)cargo run
chat交互式控制台 (显式)cargo run -- chat
serveHTTP 服务器模式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

最佳实践

  1. 环境变量:始终从环境变量加载敏感配置 (API 密钥)
  2. 错误处理:使用 Result 类型进行适当的错误处理
  3. 优雅停机:Launcher 在两种模式下都能优雅地处理 Ctrl+C
  4. 端口选择:选择不与其他服务冲突的端口 (默认 8080)
  5. 会话管理:在生产环境中,考虑使用 DatabaseSessionService 而不是内存会话

相关


上一页: ← 遥测 | 下一页: 服务器 →