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

<script type="importmap">
  {
    "imports": {
      "@google/generative-ai": "https://esm.run/@google/generative-ai"
    }
  }
</script>

Vertex AI in Firebase

  1. Install the Firebase JS SDK for Web using npm:

    npm install firebase
    
  2. Initialize Firebase in your app:

    import { initializeApp } from "firebase/app";
    
    // TODO(developer) Replace the following with your app's Firebase configuration
    // See: https://firebase.google.com/docs/web/learn-more#config-object
    const firebaseConfig = {
      // ...
    };
    
    const firebaseApp = initializeApp(firebaseConfig);
    

Change the initialization

Google AI

<html>
<body>
  <!-- ... Your HTML and CSS -->
  <script type="module">
      import { GoogleGenerativeAI } from "@google/generative-ai";

      // Fetch your API_KEY
      const API_KEY = "...";

      // Access your API key
      const genAI = new GoogleGenerativeAI(API_KEY);

      // ...

      const model = genAI.getGenerativeModel({ model: "MODEL_NAME"});

      // ...
  </script>
</body>
</html>

Vertex AI in Firebase

// ...
import { getVertexAI, getGenerativeModel } from "firebase/vertexai";

// ...

const vertexAI = getVertexAI(firebaseApp);

const model = getGenerativeModel(vertexAI, { model: "MODEL_NAME" });

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?