เริ่มต้นใช้งานเวิร์กโฟลว์แบบ Agentic

หน้านี้อธิบายวิธีตั้งค่าโปรเจ็กต์ Android, กำหนดค่าไลบรารี genai-agent และสร้าง Agent ในอุปกรณ์ตัวแรก

ข้อกำหนดเบื้องต้น

ก่อนสร้างเวิร์กโฟลว์แบบ Agentic ด้วย Gemini Nano โปรดตรวจสอบว่าสภาพแวดล้อมของคุณเป็นไปตามข้อกำหนดต่อไปนี้

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

เพิ่มทรัพยากร Dependency genai-agent ลงในไฟล์ build.gradle.kts ของโมดูล

dependencies {
    // Gemini Nano agentic workflows
    implementation("com.google.mlkit:genai-agent:1.0.0-alpha1")

    // Optional: Only required when defining tools using Android AppFunctions
    implementation("androidx.appfunctions:appfunctions:1.0.0-alpha13")
    ksp("androidx.appfunctions:appfunctions-compiler:1.0.0-alpha13")
}

สร้าง Agent รายการแรก

Agent มีองค์ประกอบหลัก 2 อย่าง ได้แก่

  • คำจำกัดความของ Agent: คำสั่งและมาตรการป้องกันที่กำหนดค่าลักษณะการทำงานของ Agent
  • Agent Runner: เครื่องมือการดำเนินการที่สื่อสารกับ AICore และจัดการการสนทนา

1. กำหนดค่า Agent และ Runner

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีกำหนด Agent ด้วยคำสั่งของระบบและเริ่มต้นคลาส AgentRunner

import com.google.mlkit.genai.agent.Agent
import com.google.mlkit.genai.agent.AgentRunner

// 1. Build the agent configuration
val agent = Agent.builder()
    .setName("Assistant")
    .setInstructions("You are a helpful on-device assistant.")
    .build()

// 2. Initialize the runner
val runner = AgentRunner(agent)

2. เริ่มการสนทนา

การสนทนาแบบหลายเทิร์นจะเก็บประวัติการสนทนาไว้ในเทิร์นต่างๆ โดยใช้อินสแตนซ์ AgentSession รวมข้อความพรอมต์ไว้ในออบเจ็กต์ Content และรวบรวมคำตอบที่สตรีม

import android.util.Log
import androidx.lifecycle.lifecycleScope
import com.google.mlkit.genai.agent.textTokens
import com.google.mlkit.genai.prompt.Content
import kotlinx.coroutines.launch

// Create or retrieve a conversation session
val session = runner.getOrCreateSession("chat_session")

val prompt = Content.builder()
    .text("Hello! Can you help me organize my day?")
    .build()

// Collect streamed tokens in a coroutine
lifecycleScope.launch {
    session.runAsync(prompt)
        .textTokens()
        .collect { token ->
            Log.d("Agent", token)
        }
}

สำหรับการค้นหาแบบครั้งเดียวที่คุณไม่จำเป็นต้องเก็บประวัติการสนทนาไว้ คุณสามารถเรียกใช้เมธอด runner.runSingleTurnAsync(prompt) ได้โดยตรง ดังนี้

lifecycleScope.launch {
    runner.runSingleTurnAsync(prompt)
        .textTokens()
        .collect { token ->
            Log.d("Agent", token)
        }
}

3. ล้างทรัพยากร

คลาส AgentRunner จะใช้การเชื่อมต่อ AutoCloseable เมื่อคอมโพเนนต์ ViewModel หรือ Activity ถูกทำลาย ให้ปิด Runner เพื่อปล่อยเซสชันกิจกรรมที่ใช้งานอยู่และยกเลิก Coroutine ในเบื้องหลัง ดังนี้

runner.close()

ขั้นตอนถัดไป

  • เพิ่มความสามารถด้วยเครื่องมือ: ให้ Agent มีความสามารถในการดำเนินการหรือค้นหาข้อมูลแอปโดยใช้ AppFunctions ของ Android ดูหัวข้อกำหนดเครื่องมือ
  • คงเซสชันไว้ตลอดการหมุน: ดูวิธีคงอินสแตนซ์ AgentSession ไว้ในคลาส ViewModel ตลอดการเปลี่ยนแปลงการกำหนดค่าในจัดการเซสชันและวงจร
  • เชื่อมต่อกับ Compose: สตรีมเหตุการณ์และแสดงผลบับเบิลแชทแบบอินเทอร์แอกทีฟในสตรีมเหตุการณ์และการผสานรวม UI