สร้าง Agent ประเภท ADK สำหรับ Android

ไลบรารี Agent Development Kit (ADK) สำหรับ Android ช่วยให้คุณสร้างและผสานรวม AI Agent ที่ซับซ้อนลงในแอป Android ได้โดยตรง ADK เป็นเฟรมเวิร์กสำหรับนักพัฒนาแอปแบบโอเพนซอร์สสำหรับสร้าง Agent ที่ทำงานด้วยระบบ AI ซึ่งทำงานในเครื่อง บนบริการที่โฮสต์ และบนอุปกรณ์เคลื่อนที่ Android เฟรมเวิร์กนี้รองรับภาษาโปรแกรม Kotlin และ Java ซึ่งช่วยให้คุณเริ่มสร้างเอเจนต์ได้อย่างรวดเร็วและขยายขนาดไปจนถึงแอปพลิเคชันแบบหลายเอเจนต์ที่ซับซ้อนได้

ไลบรารี ADK สำหรับ Android มีการพึ่งพาและรันไทม์เฉพาะ ที่ปรับให้เหมาะกับสภาพแวดล้อมบนอุปกรณ์เคลื่อนที่ คุณสามารถสร้างเอเจนต์ที่เรียกใช้โมเดล AI ในอุปกรณ์โดยใช้ Gemini Nano โดยใช้ ML Kit GenAI API ซึ่งจะช่วยให้คุณสร้างประสบการณ์ AI ที่เน้นความเป็นส่วนตัวและมีเวลาในการตอบสนองต่ำซึ่งทำงานได้โดยไม่ต้องเข้าถึงเครือข่าย

ใช้ ADK Kotlin ในโปรเจ็กต์ Android

คุณสามารถใช้ API ของ ADK Kotlin Agent เพื่อสร้าง AI Agent ที่ทำงานภายในแอป Android ได้ โค้ดเอเจนต์ที่คุณเขียนจะเหมือนกับคู่มือเริ่มต้นใช้งาน ADK Kotlin ความแตกต่างคือทรัพยากร Dependency ของ Gradle การกำหนดค่าโปรเจ็กต์ และวิธีเรียกใช้ Agent ที่รันไทม์

สิ่งที่ต้องมีก่อน

ไลบรารี ADK สำหรับ Android มีข้อกำหนดในการพัฒนาต่อไปนี้

  • Android Studio
  • Android SDK (compileSdk 34 ขึ้นไป, minSdk 24 ขึ้นไป)

กำหนดค่าโปรเจ็กต์ Android

ใน build.gradle.kts ของโปรเจ็กต์ Android ให้เพิ่มทรัพยากร Dependency ของ ADK Android และโปรเซสเซอร์สำหรับคำอธิบายประกอบ KSP ดังนี้

plugins {
    id("com.android.application")
    kotlin("android")
    id("com.google.devtools.ksp") version "2.1.20-2.0.1"
}

android {
    namespace = "com.example.agent"
    compileSdk = 34

    defaultConfig {
        applicationId = "com.example.agent"
        minSdk = 24
        targetSdk = 34
    }
}

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

kotlin {
    jvmToolchain(17)
}

กำหนด Agent

โค้ดของเอเจนต์จะเหมือนกับ Kotlin Quickstart ของ ADK ตัวอย่างโค้ด HelloTimeAgentที่มีไวยากรณ์ @Tool, @Param และ .generatedTools() จะทำงานใน Android ได้โดยไม่ต้องแก้ไข

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(),
    )
}

เรียกใช้ Agent จากแอป Android

ในอุปกรณ์ที่ใช้ Android ให้ใช้ InMemoryRunner เพื่อเรียกใช้เอเจนต์และรวบรวม การตอบกลับจากโครูทีน ดังที่แสดงในตัวอย่างโค้ดต่อไปนี้

import com.google.adk.kt.runners.InMemoryRunner
import com.google.adk.kt.sessions.InMemorySessionService
import com.google.adk.kt.types.Content
import com.google.adk.kt.types.Part
import com.google.adk.kt.types.Role
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
// Create a runner and session service
val sessionService = InMemorySessionService()
val runner = InMemoryRunner(
    agent = HelloTimeAgent.rootAgent,
    sessionService = sessionService,
)
// Call the agent from a coroutine (e.g. in a ViewModel or Activity)
scope.launch {
    runner.runAsync(
        userId = "user-123",
        sessionId = "session-123",
        newMessage = Content(
            role = Role.USER,
            parts = listOf(Part(text = "What time is it in New York?")),
        ),
    ).collect { event ->
        val text = event.content?.parts?.firstOrNull()?.text
        if (!text.isNullOrBlank()) {
            // Update your UI with the agent's response
        }
    }
}

โมเดลในอุปกรณ์ที่มี Gemini Nano

อาร์ติแฟกต์ ADK สำหรับ Android รองรับการอนุมานในอุปกรณ์โดยใช้ Gemini Nano ผ่าน ML Kit GenAI API แนวทางนี้ช่วยให้ตัวแทนทำงานได้ โดยไม่ต้องเข้าถึงเครือข่ายและเก็บข้อมูลไว้ในอุปกรณ์

หากต้องการใช้โมเดลในอุปกรณ์ ให้สร้างโมเดล GenaiPrompt แทนGemini ดังที่แสดงในตัวอย่างโค้ดต่อไปนี้

import com.google.adk.kt.models.mlkit.GenaiPrompt
import com.google.mlkit.genai.prompt.GenerativeModel
// Create an ML Kit GenerativeModel for on-device inference
val generativeModel: GenerativeModel = // ... initialize using ML Kit
val onDeviceModel = GenaiPrompt.create(
    generativeModel = generativeModel,
    name = "gemini-nano",
)
val agent = LlmAgent(
    name = "on_device_agent",
    model = onDeviceModel,
    instruction = Instruction("You are a helpful assistant."),
)

นอกจากนี้ คุณยังรวมโมเดลในระบบคลาวด์และในอุปกรณ์ไว้ในระบบหลายเอเจนต์ได้ด้วย โดยใช้Geminiในระบบคลาวด์เป็นตัวจัดการหลัก และใช้โมเดลGenaiPromptในอุปกรณ์สำหรับเอเจนต์ย่อยที่จัดการงานที่ละเอียดอ่อนด้านความเป็นส่วนตัว ดูข้อมูลเพิ่มเติมเกี่ยวกับรูปแบบนี้ได้ในบล็อกโพสต์ ADK สำหรับ Kotlin และ Android

ดู Activity ที่ใช้งานได้ทั้งหมดและตัวอย่างเพิ่มเติมได้ที่ตัวอย่าง ADK Kotlin ใน GitHub