ไลบรารี Agent Development Kit (ADK) สำหรับ Android ช่วยให้คุณสร้างและผสานรวม AI Agent ที่ซับซ้อนลงในแอป Android ได้โดยตรง ADK เป็นเฟรมเวิร์กสำหรับนักพัฒนาแอปแบบโอเพนซอร์สสำหรับการสร้าง Agent ที่ขับเคลื่อนด้วย AI ซึ่งทำงานในเครื่อง ในบริการที่โฮสต์ และในอุปกรณ์เคลื่อนที่ Android เฟรมเวิร์กรองรับภาษาโปรแกรม Kotlin และ Java ซึ่งช่วยให้คุณเริ่มสร้าง Agent ได้อย่างรวดเร็วและเพิ่มทรัพยากรเป็นแอปพลิเคชันแบบหลาย Agent ที่ซับซ้อนได้
ไลบรารี ADK สำหรับ Android มีการพึ่งพาเฉพาะทางและการรองรับรันไทม์ที่ปรับให้เหมาะกับสภาพแวดล้อมบนอุปกรณ์เคลื่อนที่ คุณสามารถสร้างเอเจนต์ที่เรียกใช้โมเดล AI ในอุปกรณ์โดยใช้ Gemini Nano ผ่าน ML Kit GenAI API ซึ่งช่วยให้คุณสร้างประสบการณ์ 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 ให้เพิ่มการพึ่งพา 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
โค้ด Agent จะเหมือนกับ ADK Kotlin Quickstart ตัวอย่างโค้ด 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 และรวบรวมการตอบกลับจากโครูทีน ดังที่แสดงในตัวอย่างโค้ดต่อไปนี้
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 แนวทางนี้ช่วยให้ Agent ทำงานได้โดยไม่ต้องเข้าถึงเครือข่ายและเก็บข้อมูลไว้ในอุปกรณ์
หากต้องการใช้โมเดลในอุปกรณ์ ให้สร้างโมเดล 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."),
)
นอกจากนี้ คุณยังรวมโมเดลคลาวด์และโมเดลในอุปกรณ์ไว้ในระบบแบบหลาย Agent ได้ด้วย โดยใช้ Gemini ที่อิงตามคลาวด์สำหรับผู้ประสานงานหลัก และโมเดล GenaiPrompt ในอุปกรณ์สำหรับ Agent ย่อยที่จัดการงานที่ละเอียดอ่อนด้านความเป็นส่วนตัว
ดูตัวอย่าง Activity ที่ใช้งานได้และตัวอย่างเพิ่มเติมได้ที่ตัวอย่าง ADK Kotlin ใน GitHub