在 Firebase SDK 中使用 Vertex AI 開始使用 Gemini API


本指南說明如何開始呼叫 Vertex AI Gemini API Vertex AI in Firebase SDK (適用於您選擇的平台)。

事前準備

本指南假設您熟悉 Android Studio 的使用方式 。

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

如果您已經有 Firebase 專案,且有一個應用程式連結至 Firebase

  1. 前往 Firebase 控制台中的 使用 Gemini 建構內容」頁面

  2. 按一下 Vertex AI in Firebase 資訊卡即可啟動可協助您的工作流程 請完成下列任務(請注意,如果您在控制台中看到 Vertex AI,表示這些工作已完成)。

  3. 請繼續進行本指南的下一步驟,將 SDK 加入應用程式。

如果您尚未有 Firebase 專案,且有一個應用程式連結至 Firebase


步驟 2:新增 SDK

設定 Firebase 專案,並將應用程式連結至 Firebase (請參閱上一步) 您現在可以在應用程式中加入 Vertex AI in Firebase SDK。

Vertex AI in Firebase Android 版 SDK (firebase-vertexai) 提供 對 Vertex AI Gemini API 的存取權。

模組 (應用程式層級) Gradle 設定檔中 (例如 <project>/<app-module>/build.gradle.kts),請新增 Vertex AI in Firebase Android 版 SDK:

Kotlin+KTX

dependencies {
  // ... other androidx dependencies

  // add the dependency for the Vertex AI in Firebase SDK for Android
  implementation("com.google.firebase:firebase-vertexai:16.0.0-beta04")
}

Java

針對 Java,您必須額外新增兩個程式庫。

dependencies {
  // ... other androidx dependencies

  // add the dependency for the Vertex AI in Firebase SDK for Android
  implementation("com.google.firebase:firebase-vertexai:16.0.0-beta04")

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

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

您必須先初始化 Vertex AI,才能發出 API 呼叫 以及生成式模型

Kotlin+KTX

針對 Kotlin,這個 SDK 中的方法為暫停函式,需要呼叫 協同程式範圍中的映像檔。
// Initialize the Vertex AI service and the generative model
// Specify a model that supports your use case
// Gemini 1.5 models are versatile and can be used with all API capabilities
val generativeModel = Firebase.vertexAI.generativeModel("gemini-1.5-flash")

Java

如果是 Java,此 SDK 中的串流方法會傳回 Reactive Streams 程式庫Publisher 類型。
// Initialize the Vertex AI service and the generative model
// Specify a model that supports your use case
// Gemini 1.5 models are versatile and can be used with all API capabilities
GenerativeModel gm = FirebaseVertexAI.getInstance()
        .generativeModel("gemini-1.5-flash");

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

閱讀完入門指南後,請瞭解如何選擇 Gemini 模型,以及視需要調整 找出適合您的用途和應用程式的位置

步驟 4:呼叫 Vertex AI Gemini API

您已將應用程式連結至 Firebase、加入 SDK 並初始化 Vertex AI 服務和生成式模型 你準備呼叫 Vertex AI Gemini API 了。

您可以使用 generateContent(),透過純文字提示生成文字 要求:

Kotlin+KTX

針對 Kotlin,這個 SDK 中的方法為暫停函式,需要呼叫 協同程式範圍中的映像檔。
// Initialize the Vertex AI service and the generative model
// Specify a model that supports your use case
// Gemini 1.5 models are versatile and can be used with all API capabilities
val generativeModel = Firebase.vertexAI.generativeModel("gemini-1.5-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
// Gemini 1.5 models are versatile and can be used with all API capabilities
GenerativeModel gm = FirebaseVertexAI.getInstance()
        .generativeModel("gemini-1.5-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 模型

進一步瞭解 適用於各種用途配額與定價

試試 Gemini API 的其他功能

瞭解如何控管內容生成功能

,瞭解如何調查及移除這項存取權。 您也可以使用 Vertex AI Studio


提供意見 你使用 Vertex AI in Firebase 的感想