Firebase SDK の Vertex 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 に接続されたアプリがまだない場合

  1. Firebase コンソールにログインする

  2. [プロジェクトを作成] をクリックし、次のいずれかのオプションを使用します。

    • オプション 1: 「プロジェクトの作成」ワークフローの最初のステップで新しいプロジェクト名を入力して、完全に新しい Firebase プロジェクトを(およびその基盤となる Google Cloud プロジェクトを自動的に)作成します。

    • オプション 2: 「プロジェクトの作成」ワークフローの最初のステップで、プルダウン メニューから Google Cloud プロジェクト名を選択して、既存の Google Cloud プロジェクトに「Firebase を追加」します。

    プロンプトが表示されたら、Vertex AI in Firebase SDK を使用するように Google Analytics を設定する必要はありません

  3. Firebase コンソールで、[Gemini を使用した構築] ページに移動します。

  4. [Vertex AI in Firebase] カードをクリックして、次のタスクを完了するためのワークフローを開始します。

  1. コンソールの生成 AI ワークフローを続行して、アプリを Firebase に接続します。このワークフローには、次のタスクが含まれます。

    • Firebase プロジェクトにアプリを登録する。

    • Firebase 構成ファイル(google-services.json)と google-services Gradle プラグインをアプリに追加します。

  2. このガイドの次のステップでは、Vertex AI in Firebase SDK をアプリに追加し、SDK と Gemini API の使用に固有の必要な初期化を完了します。


ステップ 2: SDK を追加する

Firebase プロジェクトが設定され、アプリが Firebase に接続されている(前の手順を参照)ので、Vertex AI in Firebase SDK をアプリに追加できます。

Vertex AI in Firebase SDK for Android(firebase-vertexai)は、Gemini モデルと Imagen モデルを操作するための API へのアクセスを提供します。

モジュール(アプリレベル)の Gradle ファイル<project>/<app-module>/build.gradle.kts など)に、Android 用 Vertex AI in Firebase ライブラリの依存関係を追加します。ライブラリのバージョニングの制御には、Firebase Android BoM を使用することをおすすめします。

KotlinJava
dependencies {
    // ... other androidx dependencies

    // Import the BoM for the Firebase platform
    implementation(platform("com.google.firebase:firebase-bom:33.10.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 の場合は、2 つのライブラリを追加する必要があります。

dependencies {
    // ... other androidx dependencies

    // Import the BoM for the Firebase platform
    implementation(platform("com.google.firebase:firebase-bom:33.10.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 ライブラリを使用します。

Firebase BoM を使用しない場合は、依存関係の行でそれぞれの Firebase ライブラリのバージョンを指定する必要があります。

アプリで複数の Firebase ライブラリを使用する場合は、すべてのバージョンの互換性を確保するため、BoM を使用してライブラリのバージョンを管理することを強くおすすめします。

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

ステップ 3: Vertex AI サービスと生成モデルを初期化する

API 呼び出しを行い、Gemini モデルをプロンプトするには、Vertex AI サービスと生成モデルを初期化する必要があります。

KotlinJava
Kotlin の場合、この SDK のメソッドは suspend 関数であり、Coroutine スコープから呼び出す必要があります。
// 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 の場合、この 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() を使用すると、テキストのみのプロンプト リクエストからテキストを生成できます。

KotlinJava
Kotlin の場合、この SDK のメソッドは suspend 関数であり、Coroutine スコープから呼び出す必要があります。
// 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 の場合、この 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);

Google アシスタントの機能

サポートされているモデルの詳細

さまざまなユースケースで利用可能なモデルと、その割り当て料金について学びます。

Gemini API のその他の機能を試す

コンテンツ生成を制御する方法

Vertex AI Studio を使用して、プロンプトとモデル構成をテストすることもできます。


Vertex AI in Firebase の使用感に関するフィードバックを送信する