ไลบรารี Agent Development Kit (ADK) สำหรับ Android ช่วยให้คุณสร้างและผสานรวม AI Agent ที่ซับซ้อนลงในแอป Android ได้โดยตรง ADK เป็นเฟรมเวิร์กสำหรับนักพัฒนาซอฟต์แวร์แบบโอเพนซอร์สสำหรับการสร้าง Agent ที่ทำงานด้วยระบบ AI ซึ่งทำงานในเครื่อง บนบริการที่โฮสต์ และบนอุปกรณ์เคลื่อนที่ Android เฟรมเวิร์กนี้รองรับภาษาโปรแกรม Kotlin และ Java ซึ่งช่วยให้คุณเริ่มสร้าง Agent ได้อย่างรวดเร็วและปรับขนาดเป็นแอปพลิเคชันแบบหลาย Agent ที่ซับซ้อนได้
ไลบรารี ADK สำหรับ Android มีทรัพยากร Dependency เฉพาะและการรองรับรันไทม์ที่ปรับให้เหมาะกับสภาพแวดล้อมบนอุปกรณ์เคลื่อนที่ คุณสามารถสร้าง Agent ที่เรียกใช้โมเดล AI ในอุปกรณ์โดยใช้ Gemini Nano ผ่าน GenAI API ของ ML Kit ซึ่งช่วยให้คุณสร้างประสบการณ์ AI ที่เน้นความเป็นส่วนตัวและมีเวลาในการตอบสนองต่ำ ซึ่งทำงานได้โดยไม่ต้องเข้าถึงเครือข่าย
ใช้ ADK Kotlin ในโปรเจ็กต์ Android
คุณสามารถใช้ ADK Kotlin Agent API เพื่อสร้าง AI Agent ที่ทำงานภายในแอป Android ได้ โค้ด Agent ที่คุณเขียนจะเหมือนกับคู่มือเริ่มต้นใช้งาน 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
}
packaging {
resources {
excludes += "/META-INF/{INDEX.LIST,DEPENDENCIES}"
}
}
}
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
โค้ด 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 เพื่อเรียกใช้ Agent และรวบรวมคำตอบจาก Coroutine ดังที่แสดงในตัวอย่างโค้ดต่อไปนี้
// 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 } } }
ใช้ Agent เพื่อสร้าง UI
เมื่อ Agent จำเป็นต้องแสดงผลอินเทอร์เฟซผู้ใช้แบบอินเทอร์แอกทีฟที่มีโครงสร้าง เช่น การ์ด แบบฟอร์ม หรือรายการ แทนข้อความธรรมดา ให้ใช้โปรโตคอล Agent-to-UI (A2UI)
คุณสามารถใช้ Jetpack Compose A2UI Renderer เพื่อแสดงผลการตอบกลับ A2UI เป็นคอมโพเนนต์ Jetpack Compose และ Material 3 แบบเนทีฟในลำดับชั้น UI ของแอปโดยอัตโนมัติ หากต้องการเริ่มต้นใช้งาน โปรดดูคู่มือ Jetpack Compose A2UI
โมเดลในอุปกรณ์ที่มาพร้อม Gemini Nano
อาร์ติแฟกต์ ADK สำหรับ Android รองรับการอนุมานในอุปกรณ์โดยใช้ Gemini Nano ผ่าน GenAI API ของ ML Kit แนวทางนี้ช่วยให้ Agent ทำงานได้โดยไม่ต้องเข้าถึงเครือข่าย และเก็บข้อมูลไว้ในอุปกรณ์
หากต้องการใช้โมเดลในอุปกรณ์ ให้สร้างโมเดล GenaiPrompt แทน Gemini ดังที่แสดงในตัวอย่างโค้ดต่อไปนี้
// import com.google.adk.kt.models.mlkit.GenaiPrompt // import com.google.mlkit.genai.prompt.Generation // import com.google.mlkit.genai.prompt.GenerativeModel // Create an ML Kit GenerativeModel for on-device inference val generativeModel: GenerativeModel = Generation.getClient() 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."), )
นอกจากนี้ คุณยังรวมโมเดลระบบคลาวด์และโมเดลในอุปกรณ์ไว้ในระบบ Multi-Agent ได้ด้วย โดยใช้ Gemini ที่ทำงานบนระบบคลาวด์สำหรับ Orchestrator ระดับรูท และโมเดล GenaiPrompt ในอุปกรณ์สำหรับ Agent ย่อยที่จัดการงานที่ละเอียดอ่อนด้านความเป็นส่วนตัว ดูข้อมูลเพิ่มเติมเกี่ยวกับรูปแบบนี้ได้ในบล็อกโพสต์ ADK สำหรับ Kotlin และ Android
ดูกิจกรรมการทำงานที่สมบูรณ์และตัวอย่างเพิ่มเติมได้ที่ตัวอย่าง ADK Kotlin ใน GitHub