跳转到内容

适用于 Android Development Kit (ADK) 的 Kotlin 快速入门指南

本指南将向您展示如何开始使用 Kotlin 的 Agent Development Kit。在开始之前,请确保您已经安装了以下软件:

  • Java 17 或更高版本
  • Gradle 8.0 或更高版本
正在为 Android 平台开发应用程序吗?

这个快速入门指南介绍了在 JVM 上使用 Kotlin 的方法。如果您正在开发 Android 应用程序,请先完成这个快速入门指南,以了解相关 API 的使用方法;之后再继续学习其他内容。 构建适用于 Android 的 ADK(Android Development Kit)代理程序。 `for` 用于 Android 特定的项目设置以及设备上的模型(on-device models)。

创建一个代理项目(Agent Project)。

创建一个代理项目(agent project),该项目包含以下文件和目录结构:

my_agent/
    src/main/kotlin/com/example/agent/
                        HelloTimeAgent.kt   # agent definition + tool
                        Main.kt             # entry point
    build.gradle.kts                        # project configuration
    .env                                    # API keys or project IDs
使用命令行创建这个项目结构。
mkdir my_agent\src\main\kotlin\com\example\agent
type nul > my_agent\src\main\kotlin\com\example\agent\HelloTimeAgent.kt
type nul > my_agent\src\main\kotlin\com\example\agent\Main.kt
type nul > my_agent\build.gradle.kts
type nul > my_agent\.env
mkdir -p my_agent/src/main/kotlin/com/example/agent && \
    touch my_agent/src/main/kotlin/com/example/agent/HelloTimeAgent.kt && \
    touch my_agent/src/main/kotlin/com/example/agent/Main.kt && \
    touch my_agent/build.gradle.kts my_agent/.env

定义代理代码(Agent Code)。

编写一个基本代理的代码,其中包括对 ADK(Audio Development Kit)的简单实现。 Function Tool被称为…… getCurrentTime()将以下代码添加到…… HelloTimeAgent.kt 将文件放入您的项目目录中。

my_agent/src/main/kotlin/com/example/agent/HelloTimeAgent.kt
package com.example.agent

import com.google.adk.kt.agents.Instruction
import com.google.adk.kt.agents.LlmAgent
import com.google.adk.kt.annotations.Param
import com.google.adk.kt.annotations.Tool
import com.google.adk.kt.models.Gemini

class TimeService {
    /** Mock tool implementation */
    @Tool
    fun getCurrentTime(
        @Param("Name of the city to get the time for") city: String
    ): Map<String, String> {
        return mapOf("city" to city, "time" to "The time is 10:30am.")
    }
}

object HelloTimeAgent {
    @JvmField
    val rootAgent = LlmAgent(
        name = "hello_time_agent",
        description = "Tells the current time in a specified city.",
        model = Gemini(
            name = "gemini-flash-latest",
            apiKey = System.getenv("GOOGLE_API_KEY")
                ?: error("GOOGLE_API_KEY environment variable not set."),
        ),
        instruction = Instruction(
            "You are a helpful assistant that tells the current time in a city. "
                + "Use the 'getCurrentTime' tool for this purpose."
        ),
        tools = TimeService().generatedTools(),
    )
}

关于我们 @Tool 以及 KSP

The @Tool `annotation` 将一个函数标记为代理(agent)可以调用的工具。在编译时,KSP(Kotlin Symbol Processing)注释处理器会生成相应的代码或数据结构,以便代理能够使用该函数。 .generatedTools() 上面使用的是扩展函数(extension function)。 这是一种“零反射”(zero-reflection)机制,用于函数工具的注册。 所需的 KSP 插件及处理器依赖项已包含在软件包中。 build.gradle.kts 请参考下面的配置信息。

配置项目及其依赖项。

一个基于 Kotlin 的 ADK(Android Development Kit)代理项目需要以下依赖项: build.gradle.kts 项目文件:

my_agent/build.gradle.kts (partial)
dependencies {
    implementation("com.google.adk:google-adk-kotlin-core:1.6.0")
    ksp("com.google.adk:google-adk-kotlin-processor:1.6.0")
}
完成 build.gradle.kts 项目的配置信息

以下代码展示了一个完整的……(请根据上下文填写“……”的具体内容)。 build.gradle.kts 该项目的配置:

my_agent/build.gradle.kts
plugins {
    kotlin("jvm") version "2.1.20"
    id("com.google.devtools.ksp") version "2.1.20-2.0.1"
    application
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("com.google.adk:google-adk-kotlin-core:1.6.0")
    implementation("com.google.adk:google-adk-kotlin-webserver:1.6.0")
    ksp("com.google.adk:google-adk-kotlin-processor:1.6.0")
}

kotlin {
    jvmToolchain(17)
}

application {
    mainClass.set(
        project.findProperty("mainClass") as? String
            ?: "com.example.agent.MainKt"
    )
}

tasks.named<JavaExec>("run") {
    standardInput = System.`in`
}

请设置您的 API 密钥。

该项目使用了 Gemini API,因此需要一个 API 密钥。如果您还没有 Gemini API 密钥,请在 Google AI Studio 中创建一个密钥。 API 密钥 page.

在终端窗口中,输入您的 API 密钥。 .env 您的项目文件 用于设置环境变量:

Update: my_agent/.env
echo 'export GOOGLE_API_KEY="YOUR_API_KEY"' > .env
Update: my_agent/env.bat
echo 'set GOOGLE_API_KEY="YOUR_API_KEY"' > env.bat
Update: my_agent/env.bat
echo set GOOGLE_API_KEY="YOUR_API_KEY" > env.bat
将其他 AI 模型与 ADK 结合使用

ADK 支持使用许多生成式 AI 模型。有关在 ADK 代理中配置其他模型的更多信息,请参阅…… 模型与身份验证(Models & Authentication).

创建一个入口点(entry point)。

创建一个…… Main.kt 需要运行并与其交互的文件 HelloTimeAgent 从命令行中执行该操作。 ReplRunner 提供了一个内置的交互式 REPL(Reactive Programming Language),它可以处理用户的输入、代理的响应以及工具的确认提示。

my_agent/src/main/kotlin/com/example/agent/Main.kt
package com.example.agent

import com.google.adk.kt.runners.ReplRunner

fun main() {
    ReplRunner(HelloTimeAgent.rootAgent).start()
}

运行你的代理程序。

您可以使用交互式的命令行界面(REPL)来运行您的 ADK 代理程序, 也可以使用 ADK 提供的 Web 用户界面来运行该代理程序。 AdkDevServer这两种选项都允许你测试并与你的代理程序进行交互。

通过命令行界面运行

使用 Gradle 的命令行接口来运行你的代理程序。 run 任务:

# Remember to load keys and settings: source .env OR env.bat
gradle run

代理程序会启动一个交互式会话。请输入一条消息,然后按回车键:

Agent hello_time_agent is ready. Type 'exit' to quit.

You > What time is it in New York?

hello_time_agent > The current time in New York is 10:30am.

You > exit
Exiting agent.

adk-run.png

通过 Web 界面运行该程序。

要通过 ADK Web 界面运行您的代理程序,请将 Web 服务器依赖项添加到您的配置中。 build.gradle.kts:

my_agent/build.gradle.kts (add to dependencies)
dependencies {
    implementation("com.google.adk:google-adk-kotlin-core:1.6.0")
    implementation("com.google.adk:google-adk-kotlin-webserver:1.6.0")
    ksp("com.google.adk:google-adk-kotlin-processor:1.6.0")
}

然后创建一个…… WebMain.kt 将文件与你的……一起保存。 Main.kt:

my_agent/src/main/kotlin/com/example/agent/WebMain.kt
package com.example.agent

import com.google.adk.kt.webserver.AdkServerConfig
import com.google.adk.kt.webserver.dev.AdkDevServer

fun main() {
    // inMemory() supplies the agent loader and the session and artifact
    // services, keeping their state in the process.
    val server = AdkDevServer(AdkServerConfig.inMemory(HelloTimeAgent.rootAgent))

    println("Starting ADK dev server on http://localhost:8080")
    server.start(wait = true)
}

使用以下命令运行 Web 服务器: -PmainClass 用于选择网页的属性 入口点(Entry Point):

# Remember to load keys and settings: source .env OR env.bat
gradle run -PmainClass=com.example.agent.WebMainKt

此命令会启动一个带有聊天界面的 Web 服务器,供您的代理使用。您可以通过以下地址访问该 Web 界面: http://localhost:8080在左上角选择您的代理,然后输入请求内容。

adk-web-dev-ui-chat.png

注意:ADK Web 仅适用于开发用途。

ADK Web 是 不适用于生产环境中的部署。您应该仅将 ADK Web 用于开发和调试目的。有关更多信息,请参阅 ADK 文档。 Web界面.

下一步:构建你的代理程序(agent)。

既然您已经安装了 ADK 并让您的第一个代理程序成功运行了,现在可以尝试根据我们的构建指南来创建自己的代理程序了。