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


このガイドでは、 Vertex AI Gemini API 選択したプラットフォーム用の Vertex AI in Firebase SDK。

前提条件

このガイドは、読者が Android Studio を使用した開発に慣れていることを前提としています。 Android 向けアプリ。

ステップ 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 SDK for Android(firebase-vertexai)には、次の機能があります。 Vertex AI Gemini API へのアクセス権。

モジュール(アプリレベル)の Gradle 構成ファイル (<project>/<app-module>/build.gradle.kts のように)実行するには、依存関係を Vertex AI in Firebase SDK for Android:

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 の場合、2 つのライブラリを追加する必要があります。

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

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

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 モデルと(必要に応じて) location を使用します。

ステップ 4: Vertex AI Gemini API を呼び出す

アプリを Firebase に接続し、SDK を追加して初期化を終えたので、 Vertex AI サービスと生成モデル、 Vertex AI Gemini API を呼び出す準備が整いました。

generateContent() を使用すると、テキストのみのプロンプトからテキストを生成できます。 request:

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

// 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);
<ph type="x-smartling-placeholder">で確認できます。 で確認できます。

Google アシスタントの機能

Gemini モデルの詳細

詳しくは、 さまざまなユースケースで利用可能な および 割り当てと料金をご確認ください。

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

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

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


フィードバックを送信 Vertex AI in Firebase の感想をお聞かせください。