Vertex AI Gemini API

Nếu bạn mới làm quen với Gemini API, thì Gemini Developer API là nhà cung cấp API được đề xuất cho Nhà phát triển Android. Tuy nhiên, nếu bạn có các yêu cầu cụ thể về vị trí dữ liệu hoặc đã nhúng vào môi trường Vertex AI hoặc Google Cloud, thì bạn có thể sử dụng Vertex AI Gemini API.

Bắt đầu

Trước khi tương tác trực tiếp với Vertex AI Gemini API từ ứng dụng, bạn có thể thử nghiệm với câu lệnh trong Vertex AI Studio.

Thiết lập dự án Firebase và kết nối ứng dụng với Firebase

Sau khi bạn đã sẵn sàng gọi API từ ứng dụng, hãy làm theo hướng dẫn trong "Bước 1" của hướng dẫn bắt đầu sử dụng Firebase AI Logic để thiết lập Firebase và bật các API và dịch vụ bắt buộc.

Thêm phần phụ thuộc vào Gradle

Thêm các phần phụ thuộc vào Gradle sau đây vào mô-đun ứng dụng:

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")
}

Định cấu hình nhà cung cấp gỡ lỗi App Check cho quá trình phát triển cục bộ

Kể từ đầu tháng 7 năm 2026, trong quy trình thiết lập có hướng dẫn cho AI Logic trong bảng điều khiển Firebase, Firebase App Check sẽ tự động được thực thi để bảo vệ Gemini API. Đối với quá trình phát triển cục bộ, bạn cần định cấu hình nhà cung cấp gỡ lỗi App Check để bỏ qua quy trình chứng thực trong khi vẫn duy trì việc thực thi App Check.

  1. Trong bản dựng gỡ lỗi, hãy định cấu hình App Check để sử dụng nhà máy cung cấp gỡ lỗi:

    Kotlin

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

    Java

    FirebaseApp.initializeApp(/*context=*/ this);
    FirebaseAppCheck firebaseAppCheck = FirebaseAppCheck.getInstance();
    firebaseAppCheck.installAppCheckProviderFactory(
            DebugAppCheckProviderFactory.getInstance());
    
  2. Lấy mã thông báo gỡ lỗi:

    1. Chạy ứng dụng trong trình mô phỏng hoặc trên thiết bị thử nghiệm.

    2. Tìm mã thông báo gỡ lỗi App Check trong nhật ký. Ví dụ:

      D DebugAppCheckProvider: Enter this debug secret into the allow list
      in the Firebase Console for your project: 123a4567-b89c-12d3-e456-789012345678
      
    3. Sao chép mã thông báo (ví dụ: 123a4567-b89c-12d3-e456-789012345678).

  3. Đăng ký mã thông báo gỡ lỗi với App Check:

    1. Trong bảng điều khiển Firebase, hãy chuyển đến thẻ Bảo mật > App Check > Ứng dụng.

    2. Tìm ứng dụng của bạn, nhấp vào trình đơn mục khác (), rồi chọn Quản lý mã thông báo gỡ lỗi.

    3. Làm theo hướng dẫn trên màn hình để đăng ký mã thông báo gỡ lỗi.

Để biết thông tin chi tiết về nhà cung cấp gỡ lỗi (bao gồm cả cách lấy mã thông báo gỡ lỗi mới), hãy xem tài liệu chính thức về App Check.

Khởi chạy mô hình tạo sinh

Bắt đầu bằng cách tạo thực thể GenerativeModel và chỉ định tên mô hình:

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);

Trong tài liệu về Firebase, bạn có thể tìm hiểu thêm về các mô hình Gemini hiện có. Bạn cũng có thể tìm hiểu về cách định cấu hình các tham số của mô hình.

Tạo văn bản

Để tạo câu trả lời bằng văn bản, hãy gọi generateContent() bằng câu lệnh của bạn.

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);

Tương tự như Gemini Developer API, bạn cũng có thể truyền hình ảnh, âm thanh, video và tệp bằng câu lệnh văn bản. Để biết thông tin chi tiết, hãy xem phần Tương tác với Gemini Developer API từ ứng dụng.

Để tìm hiểu thêm về SDK Firebase AI Logic, hãy đọc tài liệu về Firebase.