Android के लिए ADK एजेंट बनाना

Android के लिए Agent Development Kit (ADK) की लाइब्रेरी की मदद से, सीधे अपने Android ऐप्लिकेशन में बेहतर एआई एजेंट बनाए और इंटिग्रेट किए जा सकते हैं. ADK, डेवलपर के लिए एक ओपन-सोर्स फ़्रेमवर्क है. इसकी मदद से, एआई की मदद से काम करने वाले एजेंट बनाए जा सकते हैं. ये एजेंट, स्थानीय तौर पर, होस्ट की गई सेवाओं पर, और Android मोबाइल डिवाइसों पर काम करते हैं. यह फ़्रेमवर्क, Kotlin और Java प्रोग्रामिंग लैंग्वेज के साथ काम करता है. इससे आपको एजेंट बनाने में मदद मिलती है. साथ ही, जटिल और मल्टी-एजेंट ऐप्लिकेशन भी बनाए जा सकते हैं.

Android के लिए ADK की लाइब्रेरी, मोबाइल एनवायरमेंट के लिए खास तौर पर तैयार की गई डिपेंडेंसी और रनटाइम सहायता उपलब्ध कराती है. ML Kit GenAI एपीआई का इस्तेमाल करके, Gemini Nano की मदद से डिवाइस पर एआई मॉडल चलाने वाले एजेंट बनाए जा सकते हैं. इससे, निजता को ध्यान में रखते हुए और कम-लेटेंसी वाले एआई अनुभव बनाए जा सकते हैं. ये अनुभव, नेटवर्क ऐक्सेस के बिना भी काम कर सकते हैं.

Android प्रोजेक्ट में ADK Kotlin का इस्तेमाल करना

Android ऐप्लिकेशन में काम करने वाले एआई एजेंट बनाने के लिए, ADK Kotlin एजेंट एपीआई का इस्तेमाल किया जा सकता है. आपके लिखे गए एजेंट का कोड, ADK Kotlin शुरुआती गाइड में दिए गए कोड जैसा ही होता है. Gradle डिपेंडेंसी, प्रोजेक्ट कॉन्फ़िगरेशन, और रनटाइम के दौरान एजेंट को शुरू करने के तरीके में अंतर होता है.

ज़रूरी शर्तें

Android के लिए ADK की लाइब्रेरी के लिए, डेवलपमेंट से जुड़ी ये ज़रूरी शर्तें हैं:

  • Android Studio
  • Android SDK (compileSdk 34 या इसके बाद का वर्शन, minSdk 24 या इसके बाद का वर्शन)

अपने Android प्रोजेक्ट को कॉन्फ़िगर करना

अपने Android प्रोजेक्ट के build.gradle.kts में, 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)
}

अपने एजेंट के बारे में बताना

एजेंट का कोड, ADK Kotlin के क्विकस्टार्ट में दिए गए कोड जैसा ही होता है. 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(),
    )
}

अपने 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 के साथ डिवाइस पर मौजूद मॉडल

Android के लिए ADK आर्टफ़ैक्ट में, ML Kit GenAI एपीआई के ज़रिए Gemini Nano का इस्तेमाल करके, डिवाइस पर इन्फ़रेंस की सुविधा शामिल है. इस तरीके से, एजेंट नेटवर्क ऐक्सेस के बिना भी काम कर सकते हैं और डेटा डिवाइस पर ही रहता है.

डिवाइस पर मौजूद मॉडल का इस्तेमाल करने के लिए, Gemini के बजाय GenaiPrompt मॉडल बनाएं. जैसा कि कोड के इस उदाहरण में दिखाया गया है:

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 मॉडल का इस्तेमाल करें. इस पैटर्न के बारे में ज़्यादा जानने के लिए, Kotlin और Android के लिए ADK के बारे में ब्लॉग पोस्ट पढ़ें.

काम करने वाली पूरी गतिविधि और ज़्यादा उदाहरणों के लिए, GitHub पर ADK Kotlin के उदाहरण देखें GitHub.