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


このガイドでは、 Vertex AI Gemini API を、Vertex AI Gemini API を Vertex AI for Firebase SDK。

前提条件

このガイドは、読者が 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-beta03")
}

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

  // 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);
で確認できます。 で確認できます。

Google アシスタントの機能

Gemini モデルの詳細

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

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

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

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


フィードバックを送信 Vertex AI for Firebase のご経験についてお聞かせください。