Vertex AI for Firebase SDK を使用して Gemini API を使ってみる


このガイドでは、Vertex AI for Firebase SDK を使用して、アプリから直接 Vertex AI Gemini API の呼び出しを開始する方法について説明します。

前提条件

このガイドは、読者が Android Studio を使用して Android アプリを開発する方法に精通していることを前提としています。

  • 開発環境と Android アプリが次の要件を満たしていることを確認します。

    • Android Studio(最新バージョン)
    • Android アプリは API レベル 21 以降を対象にする必要があります。
  • (省略可)サンプルアプリを確認します。

    サンプルアプリをダウンロード

    SDK をすぐに試したり、さまざまなユースケースの完全な実装を確認したりできます。独自の Android アプリがない場合は、サンプルアプリを使用できます。サンプルアプリを使用するには、Firebase プロジェクトに接続する必要があります。

ステップ 1: Firebase プロジェクトを設定し、アプリを Firebase に接続する

Firebase プロジェクトと Firebase に接続されたアプリがすでにある場合

  1. Firebase コンソールで [Gemini で構築する] ページに移動し、2 番目のカードをクリックして、次のタスクに役立つワークフローを起動します。コンソールに Vertex AI のタブが表示されている場合、これらのタスクは完了しています。

  2. このガイドの次のステップに進んで、SDK をアプリに追加します。

Firebase プロジェクトと Firebase に接続されたアプリがまだない場合


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

Firebase プロジェクトを設定し、アプリを Firebase に接続したら(前のステップを参照)、アプリに Vertex AI for Firebase SDK を追加できます。

Vertex AI for Firebase SDK for Android(firebase-vertexai)を使用すると、Vertex AI Gemini API にアクセスできます。

モジュール(アプリレベル)の Gradle 構成ファイル(<project>/<app-module>/build.gradle.kts など)に、Vertex AI for Firebase SDK for Android の依存関係を追加します。

Kotlin+KTX

dependencies {
  // ... other androidx dependencies

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

Java

Java の場合、2 つのライブラリを追加する必要があります。

dependencies {
  // ... other androidx dependencies

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

  // 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 サービスと生成モデルを初期化する

API 呼び出しを行う前に、Vertex AI サービスと生成モデルを初期化する必要があります。

Kotlin+KTX

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

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-preview-0514");

// 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 のメソッドは suspend 関数であり、コルーチン スコープから呼び出す必要があります。
// 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-preview-0514")

// 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-preview-0514");
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 モデルの詳細

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

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

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

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


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