Agent Platform Gemini API

หากคุณเพิ่งเริ่มใช้ Gemini API เราขอแนะนำให้ใช้ Gemini Developer API เป็น ผู้ให้บริการ API สำหรับนักพัฒนาแอป Android อย่างไรก็ตาม หากคุณมี ข้อกำหนดเฉพาะเกี่ยวกับตำแหน่งข้อมูล หรือใช้ระบบนิเวศของ Google Cloud อยู่แล้ว คุณสามารถใช้ Agent Platform Gemini API (เดิมคือ Vertex AI) กับ Firebase AI Logic เพื่อเข้าถึง โมเดล Gemini Pro และ Flash ได้

ภาพที่แสดงแอป Android ที่ใช้ Firebase Android SDK
    เพื่อส่งคำขอไปยังแบ็กเอนด์ของ Firebase ในระบบคลาวด์ ระบบจะกำหนดเส้นทางคำขอไปยัง Gemini API ของแพลตฟอร์ม Agent
    ซึ่งใช้ได้ทั้งโมเดล Gemini Pro และ Flash
รูปที่ 1 สถาปัตยกรรมการผสานรวม Firebase AI Logic เพื่อเข้าถึง Agent Platform Gemini API.

เริ่มต้นใช้งาน

ก่อนที่จะโต้ตอบกับ Agent Platform Gemini API จากแอปโดยตรง คุณสามารถทดลองใช้พรอมต์ใน Agent Studio ได้

ตั้งค่าโปรเจ็กต์ Firebase และเชื่อมต่อแอปกับ Firebase

เมื่อพร้อมที่จะเรียก API จากแอปแล้ว ให้ทำตามวิธีการใน "ขั้นตอนที่ 1" ของคู่มือเริ่มต้นใช้งาน Firebase AI Logic เพื่อ ตั้งค่า Firebase และเปิดใช้ API และบริการที่จำเป็น

เพิ่มทรัพยากร Dependency ของ Gradle

เพิ่มทรัพยากร Dependency ของ Gradle ต่อไปนี้ลงในโมดูลแอป

Kotlin

dependencies {
  // ... other androidx dependencies

  // Import the BoM for the Firebase platform
  implementation(platform("com.google.firebase:firebase-bom:34.17.0"))

  // Add the dependencies for the Firebase AI Logic and App Check libraries
  // When using the BoM, you don't specify versions in Firebase library dependencies
  implementation("com.google.firebase:firebase-ai")
  implementation("com.google.firebase:firebase-appcheck-debug")
}

Java

dependencies {
  // Import the BoM for the Firebase platform
  implementation(platform("com.google.firebase:34.17.0"))

  // Add the dependencies for the Firebase AI Logic and App Check libraries
  // When using the BoM, you don't specify versions in Firebase library dependencies
  implementation("com.google.firebase:firebase-ai")
  implementation("com.google.firebase:firebase-appcheck-debug")

  // Required for one-shot operations (to use `ListenableFuture` from Guava Android)
  implementation("com.google.guava:guava:31.0.1-android")

  // Required for streaming operations (to use `Publisher` from Reactive Streams)
  implementation("org.reactivestreams:reactive-streams:1.0.4")
}

กำหนดค่าผู้ให้บริการการแก้ไขข้อบกพร่องของ App Check สำหรับการพัฒนาในเครื่อง

ตั้งแต่ต้นเดือนกรกฎาคม 2026 เป็นต้นไป ระบบจะบังคับใช้ Firebase App Check โดยอัตโนมัติเพื่อปกป้อง Gemini API ซึ่งเป็นส่วนหนึ่งของเวิร์กโฟลว์การตั้งค่าแบบแนะนำสำหรับ AI Logic ในคอนโซล Firebase สำหรับการพัฒนาในเครื่อง คุณต้องกำหนดค่า ผู้ให้บริการการแก้ไขข้อบกพร่องของ App Check เพื่อข้ามการรับรองในขณะที่ยังคงบังคับใช้ App Check

  1. ในการบิลด์การแก้ไขข้อบกพร่อง ให้กำหนดค่า App Check ให้ใช้โรงงานผู้ให้บริการการแก้ไขข้อบกพร่อง

    Kotlin

    Firebase.initialize(context = this)
    Firebase.appCheck.installAppCheckProviderFactory(
        DebugAppCheckProviderFactory.getInstance(),
    )
    

    Java

    FirebaseApp.initializeApp(/*context=*/ this);
    FirebaseAppCheck firebaseAppCheck = FirebaseAppCheck.getInstance();
    firebaseAppCheck.installAppCheckProviderFactory(
            DebugAppCheckProviderFactory.getInstance());
    
  2. รับโทเค็นการแก้ไขข้อบกพร่องโดยทำดังนี้

    1. เรียกใช้แอปในอีมูเลเตอร์หรือในอุปกรณ์ทดสอบ

    2. มองหาโทเค็นการแก้ไขข้อบกพร่องของ App Check ในบันทึก เช่น

      D DebugAppCheckProvider: Enter this debug secret into the allow list
      in the Firebase Console for your project: 123a4567-b89c-12d3-e456-789012345678
      
    3. คัดลอกโทเค็น (เช่น 123a4567-b89c-12d3-e456-789012345678)

  3. ลงทะเบียนโทเค็นการแก้ไขข้อบกพร่องกับ App Check โดยทำดังนี้

    1. ในคอนโซล Firebase ให้ไปที่แท็บ ความปลอดภัย > App Check > แอป

    2. ค้นหาแอป คลิกเมนูรายการเพิ่มเติม () แล้วเลือก จัดการโทเค็นการแก้ไขข้อบกพร่อง

    3. ทำตามวิธีการบนหน้าจอเพื่อลงทะเบียนโทเค็นการแก้ไขข้อบกพร่อง

ดูรายละเอียดเกี่ยวกับผู้ให้บริการการแก้ไขข้อบกพร่อง (รวมถึงวิธีรับโทเค็นการแก้ไขข้อบกพร่องใหม่) ได้ในเอกสารอย่างเป็นทางการของ App Check

เริ่มต้นโมเดล Generative

เริ่มต้นโดยสร้างอินสแตนซ์ GenerativeModel และระบุชื่อโมเดล

Kotlin

val model = Firebase.ai(backend = GenerativeBackend.agentPlatform())
    .generativeModel("gemini-3.5-flash")

Java

GenerativeModel firebaseAI = FirebaseAI.getInstance(GenerativeBackend.agentPlatform())
        .generativeModel("gemini-3.5-flash");

GenerativeModelFutures model = GenerativeModelFutures.from(firebaseAI);

คุณสามารถดูข้อมูลเพิ่มเติมเกี่ยวกับ โมเดล Gemini ที่พร้อมใช้งานได้ในเอกสารประกอบของ Firebase รวมถึงดูข้อมูลเกี่ยวกับ การกำหนดค่าพารามิเตอร์ของโมเดล

สร้างข้อความ

หากต้องการสร้างคำตอบเป็นข้อความ ให้เรียก generateContent() ด้วยพรอมต์

Kotlin

suspend fun generateText(model: GenerativeModel) {
    // Note: generateContent() is a suspend function, which integrates well
    // with existing Kotlin code.
    val response = model.generateContent("Write a story about a magic backpack.")
    // ...
}

Java

Content prompt = new Content.Builder()
        .addText("Write a story about a magic backpack.")
        .build();

ListenableFuture<GenerateContentResponse> response = model.generateContent(prompt);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
    @Override
    public void onSuccess(GenerateContentResponse result) {
        String resultText = result.getText();
        // ...
    }

    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
}, executor);

คุณยังส่งรูปภาพ เสียง วิดีโอ และไฟล์ไปพร้อมกับพรอมต์ข้อความได้ด้วยเช่นเดียวกับ Gemini Developer API ดูรายละเอียดได้ที่หัวข้อ โต้ตอบกับ Gemini Developer API จากแอป

ดูข้อมูลเพิ่มเติมเกี่ยวกับ Firebase AI Logic SDK ได้ใน เอกสารประกอบของ Firebase