Vertex AI Gemini API

Nếu bạn mới sử dụng 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, bạn có thể sử dụng Vertex AI Gemini API.

Di chuyển từ Vertex AI in Firebase

Nếu ban đầu bạn đã tích hợp các mô hình Gemini Flash và Pro bằng Vertex AI in Firebase, bạn có thể di chuyển sang và tiếp tục sử dụng Vertex AI làm nhà cung cấp API. Hãy đọc tài liệu về Firebase để xem hướng dẫn di chuyển chi tiết migration guide.

Bắt đầu

Trước khi tương tác với Vertex AI Gemini API trực tiếp 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 Vertex AI Gemini API từ ứng dụng, hãy làm theo hướng dẫn trong phần "Bước 1" của hướng dẫn bắt đầu sử dụng Firebase AI Logic để thiết lập Firebase và SDK trong ứng dụng.

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

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

dependencies {
  // ... other androidx dependencies

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

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

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 hiện có để sử dụng với Gemini Developer API. 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.