Get started with the Gemini API using the Vertex AI in Firebase SDKs


This guide shows you how to get started making calls to the Vertex AI Gemini API directly from your app using the Vertex AI in Firebase SDK for your chosen platform.

Prerequisites

This guide assumes that you're familiar with using Android Studio to develop apps for Android.

  • Make sure that your development environment and Android app meet the following requirements:

    • Android Studio (latest version)
    • Your Android app must target API level 21 or higher.
  • (Optional) Check out the sample app.

    Download the sample app

    You can try out the SDK quickly, see a complete implementation of various use cases, or use the sample app if don't have your own Android app. To use the sample app, you'll need to connect it to a Firebase project.

Step 1: Set up a Firebase project and connect your app to Firebase

If you already have a Firebase project and an app connected to Firebase

  1. In the Firebase console, go to the Build with Gemini page.

  2. Click the Vertex AI in Firebase card to launch a workflow that helps you complete the following tasks:

  3. Continue to the next step in this guide to add the SDK to your app.

If you do not already have a Firebase project and an app connected to Firebase


Step 2: Add the SDK

With your Firebase project set up and your app connected to Firebase (see previous step), you can now add the Vertex AI in Firebase SDK to your app.

The Vertex AI in Firebase SDK for Android (firebase-vertexai) provides access to the Vertex AI Gemini API.

In your module (app-level) Gradle file (like <project>/<app-module>/build.gradle.kts), add the dependency for the Vertex AI in Firebase library for Android. We recommend using the Firebase Android BoM to control library versioning.

Kotlin+KTX

dependencies {
    // ... other androidx dependencies

    // Import the BoM for the Firebase platform
    implementation(platform("com.google.firebase:firebase-bom:33.6.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

For Java, you need to add two additional libraries.

dependencies {
    // ... other androidx dependencies

    // Import the BoM for the Firebase platform
    implementation(platform("com.google.firebase:firebase-bom:33.6.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")
}

By using the Firebase Android BoM, your app will always use compatible versions of Firebase Android libraries.

Step 3: Initialize the Vertex AI service and the generative model

Before you can make any API calls, you need to initialize the Vertex AI service and the generative model.

Kotlin+KTX

For Kotlin, the methods in this SDK are suspend functions and need to be called from a Coroutine scope.
// 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

For Java, the streaming methods in this SDK return a Publisher type from the Reactive Streams library.
// 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);

When you've finished the getting started guide, learn how to choose a Gemini model and (optionally) a location appropriate for your use case and app.

Step 4: Call the Vertex AI Gemini API

Now that you've connected your app to Firebase, added the SDK, and initialized the Vertex AI service and the generative model, you're ready to call the Vertex AI Gemini API.

You can use generateContent() to generate text from a text-only prompt request:

Kotlin+KTX

For Kotlin, the methods in this SDK are suspend functions and need to be called from a Coroutine scope.
// 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

For Java, the methods in this SDK return a 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);

What else can you do?

Learn more about the Gemini models

Learn about the models available for various use cases and their quotas and pricing.

Try out other capabilities of the Gemini API

Learn how to control content generation

You can also experiment with prompts and model configurations using Vertex AI Studio.


Give feedback about your experience with Vertex AI in Firebase