Vertex AI Gemini API

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

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

ก่อนที่จะโต้ตอบกับ Vertex AI Gemini API จากแอปโดยตรง คุณ สามารถทดลองใช้พรอมต์ใน Vertex AI 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.15.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.15.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.vertexAI())
    .generativeModel("gemini-2.5-flash")

Java

GenerativeModel firebaseAI = FirebaseAI.getInstance(GenerativeBackend.vertexAI())
        .generativeModel("gemini-2.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 เพื่อดูข้อมูลเพิ่มเติมเกี่ยวกับ Firebase AI Logic SDK