Migrate to use Vertex AI SDKs instead of Google AI SDKs

This page describes how to migrate from the Google AI client SDKs to the Vertex AI in Firebase client SDKs in your mobile or web apps. The Vertex AI in Firebase SDKs are available for Apple platforms (Swift), Android (Kotlin and Java), Web (JavaScript), and Flutter (Dart).

Go directly to migration instructions

Why migrate to use Vertex AI?

You might have tried out an alternative version of a Gemini API using Google AI Studio or the Google AI client SDKs. These SDKs are useful for getting started with the Gemini API and prototyping. However, for production or enterprise-scale mobile and web apps that directly call a Gemini API client-side, Firebase strongly recommends calling the Vertex AI Gemini API using our Firebase SDKs.

Security features for mobile and web apps

For mobile and web apps, security is critical and requires special considerations because your code (including calls to the Gemini API) is running in an unprotected environment.

  • By default, the Vertex AI Gemini API is authorized by Google Cloud IAM (rather than by an API key like the Google AI Gemini API). The Vertex AI in Firebase SDKs are built to call the more secure Vertex AI Gemini API.

  • For mobile and web apps, you need to protect the Gemini API and your project resources (like tuned models) from abuse by unauthorized clients. You can use Firebase App Check to verify that all API calls are from your actual app, and this feature is only available if you use the Vertex AI in Firebase SDKs.

Ecosystem built for mobile and web apps

Firebase is Google's platform for developing mobile and web apps. Using the Vertex AI in Firebase SDKs means that your apps are in an ecosystem that's focused on the needs of full-stack apps and developers. For example, you're set up to do any of the following and much more:

  • Use Cloud Storage for Firebase to include large files in your multimodal requests. Also, take advantage of client SDKs that handle file uploads and downloads (even in poor network conditions) and offer more security for your end-users' data. Learn more in our solution guide about using Cloud Storage for Firebase.

  • Manage structured data using database SDKs built for mobile and web apps (like Cloud Firestore).

  • Dynamically set run-time configurations (like location) or swap out values in your app (like a model name) without releasing a new app version using Firebase Remote Config.

Additional benefits of using Vertex AI from Google Cloud

As your use of generative AI in your app and workflows mature, you might need a platform that offers end-to-end solutions for building and deploying generative AI applications. Google Cloud provides a comprehensive ecosystem of tools to enable you to harness the power of generative AI, from the initial stages of app development to app deployment, app hosting, and managing complex data at scale.

The Vertex AI platform from Google Cloud offers a suite of MLOps tools that streamline usage, deployment, and monitoring of AI models for efficiency and reliability. Additionally, integrations with databases, DevOps tools, logging, monitoring, and IAM provide a holistic approach to managing the entire generative AI lifecycle.

Learn more about the use cases of Vertex AI in the Google Cloud documentation.

Migrate to the Vertex AI in Firebase SDKs

Migrating to the Vertex AI in Firebase SDKs requires three main steps:

  1. Set up a new or existing Firebase project and connect your app to Firebase.

  2. Migrate your codebase, which only requires changing the SDK and initialization code (including the model name). There's no modification needed for any of the code which actually calls the Gemini API.

  3. Delete any unused API keys and disable unused APIs.

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

Even if you're already familiar with Firebase, review this section to make sure that your Firebase project and app are set up to use the Vertex AI in Firebase SDKs.

Step 2: Migrate your codebase

Select the platform of your app to view platform-specific instructions.

The Google AI SDKs and the Vertex AI in Firebase SDKs were built so that migrating between the two platforms is as straightforward as possible.

To migrate, you only need to change which SDK you integrate into your app's codebase and the initialization of the service and the generative model. You don't need to modify any of the code which actually calls the Gemini API!

Change the SDK

Google AI

Kotlin+KTX

dependencies {
  // ... other androidx dependencies

  // add the dependency for the Google AI client SDK for Android
  implementation("com.google.ai.client.generativeai:generativeai:VERSION")
}

Java

dependencies {
  // ... other androidx dependencies

  // add the dependency for the Google AI client SDK for Android
  implementation("com.google.ai.client.generativeai:generativeai:VERSION")

  // Required for one-shot operations (to use `ListenableFuture` from Reactive Streams)
  implementation("com.google.guava:guava:31.0.1-android")

  // Required for streaming operations (to use `Publisher` from Guava Android)
  implementation("org.reactivestreams:reactive-streams:1.0.4")
}

Vertex AI in Firebase

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.1")
}

Java

dependencies {
  // ... other androidx dependencies

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

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

Change the initialization

Google AI

Kotlin+KTX

val generativeModel = GenerativeModel(modelName = "MODEL_NAME",
    // Access your API key as a Build Configuration variable
    apiKey = BuildConfig.apiKey
)

Java

GenerativeModel gm = new GenerativeModel("MODEL_NAME",
    // Access your API key as a Build Configuration variable
    BuildConfig.apiKey
);

// Use the GenerativeModelFutures Java compatibility layer which offers support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

Vertex AI in Firebase

Kotlin+KTX

val generativeModel = Firebase.vertexAI.generativeModel("MODEL_NAME")

Java

GenerativeModel gm = FirebaseVertexAI.getInstance()
        .generativeModel("MODEL_NAME");
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

Update imports

The following code snippets use the Chat, Content, and GenerativeModelFutures classes as examples, but the same should apply to other classes under the same package name.

Google AI

Kotlin+KTX

import com.google.ai.client.generativeai.Chat
import com.google.ai.client.generativeai.type.Content
import com.google.ai.client.generativeai.java.GenerativeModuleFutures

Java

import com.google.ai.client.generativeai.Chat;
import com.google.ai.client.generativeai.type.Content;
import com.google.ai.client.generativeai.java.GenerativeModuleFutures;

Vertex AI in Firebase

Kotlin+KTX

import com.google.firebase.vertexai.Chat
import com.google.firebase.vertexai.type.Content
import com.google.firebase.vertexai.java.GenerativeModuleFutures

Java

import com.google.firebase.vertexai.Chat;
import com.google.firebase.vertexai.type.Content;
import com.google.firebase.vertexai.java.GenerativeModuleFutures;

Step 3: Delete any unused API keys and disable unused APIs

If you no longer need to use your Google AI API key, follow security best practices and delete it. You can view and delete your Google AI API keys in the API keys section of Google AI Studio.

Also, if you're no longer using the Google AI Gemini API, disable it in your project. You can do this in the Google Cloud console from the Generative Language API (generativelanguage.googleapis.com) page. ("Generative Language API" is the official name of the Google AI Gemini API.)

What else can you do?