透過 Firebase SDKs 的 Vertex AI AI 開始使用 Gemini'API


本指南說明如何開始使用所選平台的 Vertex AI in Firebase SDK,直接從應用程式呼叫 Vertex AI Gemini API

請注意,您也可以使用這份指南,開始使用 Vertex AI in Firebase SDK 存取 Imagen 模型

事前準備

本指南假設您熟悉使用 Android Studio 開發 Android 應用程式。

  • 請確認您的開發環境和 Android 應用程式符合下列規定:

    • Android Studio (最新版本)
    • Android 應用程式必須指定 API 級別 21 以上版本。
  • (選用) 查看範例應用程式。

    下載範例應用程式

    您可以快速試用 SDK、查看各種用途的完整實作方式,或在沒有自有 Android 應用程式時使用範例應用程式。如要使用範例應用程式,您必須將其連結至 Firebase 專案

步驟 1:設定 Firebase 專案,並將應用程式連結至 Firebase

如果您擁有 Firebase 專案和已連結至 Firebase 的應用程式

  1. Firebase 主控台中,前往「運用 Gemini 建構內容」頁面

  2. 按一下 Vertex AI in Firebase 資訊卡,即可啟動工作流程,協助您完成下列工作:

  3. 請繼續參閱本指南的後續步驟,將 SDK 新增至應用程式。

如果您「沒有」已建立 Firebase 專案,也沒有與 Firebase 連結的應用程式


步驟 2:新增 SDK

Firebase 專案設定完成,應用程式已連結至 Firebase (請參閱上一個步驟),現在您可以將 Vertex AI in Firebase SDK 新增至應用程式。

Vertex AI in Firebase SDK for Android (firebase-vertexai) 提供 API 存取權,可用於與 GeminiImagen 模型互動。

模組 (應用程式層級) Gradle 檔案 (例如 <project>/<app-module>/build.gradle.kts) 中,新增 Android 適用的 Vertex AI in Firebase 程式庫依附元件。建議您使用 Firebase Android BoM 來控制程式庫版本。

Kotlin

dependencies {
    // ... other androidx dependencies

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

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

Java

針對 Java,您需要額外新增兩個程式庫。

dependencies {
    // ... other androidx dependencies

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

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

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

只要使用 Firebase Android BoM,應用程式就會一律使用相容的 Firebase Android 程式庫版本。

步驟 3:初始化 Vertex AI 服務和生成式模型

您必須先初始化 Vertex AI 服務和生成式模型,才能發出任何 API 呼叫並提示 Gemini 模型。

Kotlin

對於 Kotlin,這個 SDK 中的函式為暫停函式,需要從 協同程式範圍中呼叫。
// Initialize the Vertex AI service and the generative model
// Specify a model that supports your use case
val generativeModel = Firebase.vertexAI.generativeModel("gemini-2.0-flash")

Java

對於 Java,這個 SDK 中的串流方法會傳回 Reactive Streams 程式庫中的 Publisher 類型。
// Initialize the Vertex AI service and the generative model
// Specify a model that supports your use case
GenerativeModel gm = FirebaseVertexAI.getInstance()
        .generativeModel("gemini-2.0-flash");

// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

完成這份入門指南後,請瞭解如何選擇適合用途和應用程式的模型和 (選用) 位置

步驟 4:向模型傳送提示要求

您已將應用程式連結至 Firebase、新增 SDK,並初始化 Vertex AI 服務和生成模型,因此可以向 Gemini 模型傳送提示要求。

您可以使用 generateContent(),根據僅限文字提示要求產生文字:

Kotlin

對於 Kotlin,這個 SDK 中的函式為暫停函式,需要從 協同程式範圍中呼叫。
// Initialize the Vertex AI service and the generative model
// Specify a model that supports your use case
val generativeModel = Firebase.vertexAI.generativeModel("gemini-2.0-flash")

// Provide a prompt that contains text
val prompt = "Write a story about a magic backpack."

// To generate text output, call generateContent with the text input
val response = generativeModel.generateContent(prompt)
print(response.text)

Java

對於 Java,這個 SDK 中的各個方法會傳回 ListenableFuture
// Initialize the Vertex AI service and the generative model
// Specify a model that supports your use case
GenerativeModel gm = FirebaseVertexAI.getInstance()
        .generativeModel("gemini-2.0-flash");
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

// Provide a prompt that contains text
Content prompt = new Content.Builder()
    .addText("Write a story about a magic backpack.")
    .build();

// To generate text output, call generateContent with the text input
ListenableFuture<GenerateContentResponse> response = model.generateContent(prompt);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
    @Override
    public void onSuccess(GenerateContentResponse result) {
        String resultText = result.getText();
        System.out.println(resultText);
    }

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

你還可以做些什麼?

進一步瞭解支援的型號

瞭解可用於各種用途的模型,以及相關配額價格

試用 Gemini API 的其他功能

瞭解如何控管內容產生

您也可以使用 Vertex AI Studio 實驗提示和模型設定。


針對 Vertex AI in Firebase 的使用體驗提供意見回饋