Dynamically update your Vertex AI in Firebase app with Firebase Remote Config

When calling the Gemini API from your app using a Vertex AI in Firebase SDK, your request contains a number of parameters that control generative AI responses. These usually include the model name, the model generation configuration (maximum tokens, temperature, etc.), safety settings, system instructions, and prompt data.

In most cases, you'll want to change these on-demand or as needed for a number of scenarios:

  • Update your generative AI model without releasing a new app. You can upgrade to newer, stable model versions before earlier versions are decommissioned, drop to lower-cost or higher performance models based on your users' needs and attributes, or conditionally deploy the latest and greatest models to specific user segments (like beta testers).
  • Set the location where you access the model so that it's closer to your users.
  • A/B test different system instructions and prompts, then slowly roll out the winning experiment values to your users.
  • Use feature flags to quickly expose or hide generative AI features in your app.

Firebase Remote Config does all of this and more, letting you update parameter values as needed and conditionally for app instances that match characteristics you set in the Firebase console, without releasing a new version of your app.

This solution guide provides specific recommended use cases and describes how to add Remote Config to your generative AI app.

Jump to code implementation

Why use Firebase Remote Config with your app?

Firebase Remote Config lets you dynamically adjust your app's behavior without requiring app updates. This is especially powerful for apps that use generative AI, where rapid iteration and fine-tuning are crucial.

Essential use cases for Remote Config with generative AI apps

We recommend using Remote Config with Vertex AI in Firebase for the following essential use cases:

  • Upgrade to the latest model version without an app update: Use Remote Config parameters to change the model name as needed, so that you can upgrade to the latest version of your preferred Gemini model as soon as it's available.
  • Update system instructions and safety settings without an app update: Store system instructions and safety settings inside Remote Config parameters to ensure that you can change them on-demand if you discover issues after deployment.
  • Reduce risk and enforce AI safety: Use Remote Config Rollouts to safely and gradually release generative AI changes to your iOS and Android users.

Advanced and recommended use cases for Remote Config with generative AI apps

After instrumenting your app with Remote Config and Google Analytics, you can explore advanced use cases:

  • Set location based on client location: Use Remote Config conditions to set the location of the model based on the client's detected location.
  • Experiment with different models: Quickly test and switch between various generative AI models, or even deploy different models to different user segments, to find the best fit for your specific use case.
  • Optimize model performance: Fine-tune model parameters, such as system prompt, maximum output tokens, temperature, and other settings.
  • Use different system instructions, prompts, and model configuration based on client attributes: When using Remote Config with Google Analytics, you can create conditions based on client attributes or custom audiences and set different parameters based on these attributes.

    For example, if you're using generative AI to provide technical support in your app, you might want to set system instructions specific to the app platform to ensure accurate instructions are provided to your Android, iOS, and web platform users.

  • Personalize experiences for each user: Use Remote Config personalization to automatically determine the optimum generative AI settings for each user.

  • Control costs: Remotely adjust which generative AI models are called, how frequently they are used, and dynamically configure maximum output token values based on user audience to reduce unnecessary costs.

  • Optimize app experience and results: Use A/B Testing with Remote Config with your iOS, Android, and Flutter apps to test changes to generative AI parameters across different user segments to see how they affect key metrics like retention and revenue.

By instrumenting your generative AI app with Firebase Remote Config, you can build flexible, safe, and cost-effective AI-powered applications while creating delightful experiences for your users.

Add Firebase Remote Config to your app

In this solution guide, you'll use Firebase Remote Config to dynamically update parameters in your Android app that use the Vertex AI in Firebase SDK. You will learn how to:

  • Fetch and activate parameters like model names and system instructions from Firebase Remote Config.
  • Update your Gemini API calls to use the dynamically retrieved parameters, letting you switch between different models or modify system instructions without an app update.
  • Control parameters remotely, adjusting model behavior and capabilities as needed.

Prerequisites

This guide assumes that you're familiar with using Android Studio to develop apps for Android platforms. Before you begin, make sure that you have done the following:

  • Complete the Get started guide for the Vertex AI in Firebase SDK. Make sure that you've done all of the following:

    1. Set up a new or existing Firebase project, including using the Blaze pricing plan and enabling the required APIs.
    2. Connect your app to Firebase, including registering your app and adding your Firebase config to your app.
    3. Add the SDK and initialize the Vertex AI service and the generative model in your app.
  • Enable Google Analytics in your project and add its SDK to your app (required for conditional targeting, like setting the service and model's location based on the client device's location).

Step 1: Set parameter values in the Firebase console

Create a client Remote Config template and configure parameters and values to fetch and use in the app.

  1. Open your Firebase project in the Firebase console and, from the navigation menu, expand Run and select Remote Config.
  2. Ensure that Client is selected from the Client/Server selector at the top of the Remote Config page.
    • If this is your first time using Remote Config client templates, click Create Configuration. The Create your first parameter pane appears.
    • If this is not your first time using Remote Config templates, click Add parameter.
  3. Define the following Remote Config parameters:

    Parameter name Description Type Default value
    model_name Model name. For up-to-date lists of model names to use in your code, see Available model names. String gemini-1.5-flash
    system_instructions System instructions are like a "preamble" that you add before the model gets exposed to any further instructions from the end user to influence model behavior, based on specific needs and use cases. String You are a helpful assistant who knows everything there is to know about Firebase!
    prompt Default prompt to use with your generative AI feature. String I am a developer who wants to know more about Firebase!
    vertex_location Optionally control the location to run the Vertex AI service and access a model. You can set conditions to configure this option based on client location detected by Google Analytics. String us-central1
  4. When you've finished adding parameters, click Publish changes. If this is not a new Remote Config template, review the changes and click Publish changes again.

Step 2: Add and initialize the Remote Config SDK in your app

Add Remote Config dependencies and set up Remote Config within your app.

  1. Add the Remote Config dependency to your module (app-level) Gradle file (usually app/build.gradle.kts or app/build.gradle):

    dependencies {
        implementation(platform("com.google.firebase:firebase-bom:33.6.0"))
        implementation("com.google.firebase:firebase-vertexai")
        implementation("com.google.firebase:firebase-config")
        // ... other dependencies
    }
    
  2. Add Remote Config to your main application logic. Here, you'll initialize Remote Config and add a minimum fetch interval:

    Kotlin+KTX

    val remoteConfig: FirebaseRemoteConfig = Firebase.remoteConfig
    val configSettings = remoteConfigSettings {
    minimumFetchIntervalInSeconds = 3600
    }
    remoteConfig.setConfigSettingsAsync(configSettings)
    

    Java

    FirebaseRemoteConfig mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
    FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
        .setMinimumFetchIntervalInSeconds(3600)
        .build();
    mFirebaseRemoteConfig.setConfigSettingsAsync(configSettings);
    

In this example, the default fetch interval is 3600 seconds, but we recommend that you set a relatively low minimum fetch interval inside your code during development.

Step 3: Set in-app parameter values

You should set in-app default parameter values in the Remote Config object. This ensures that your app behaves as expected even if it cannot fetch values from the Remote Config service.

  1. From the Firebase console, open Remote Config.
  2. In the Parameters tab, open the Menu, and select Download default values.
  3. When prompted, enable .xml for Android, then click Download file.
  4. Save the file in your app's XML resources directory.
  5. Update your main activity file to add the defaults after the configSettingsyou added previously:

    Kotlin+KTX

    // Set default values.
    remoteConfig.setDefaultsAsync(R.xml.remote_config_defaults)
    

    Java

    // Set default values.
    mFirebaseRemoteConfig.setDefaultsAsync(R.xml.remote_config_defaults);
    

Step 4: Fetch and activate values

After setting defaults, add the following to fetch and activate values:

Kotlin+KTX

// Fetch and activate Remote Config values
remoteConfig.fetchAndActivate()
     .addOnCompleteListener(this) { task ->
          if (task.isSuccessful) {
              val updated = task.result
              Log.d(TAG, "Remote Config values fetched and activated: $updated")
          } else {
              Log.e(TAG, "Error fetching Remote Config", task.exception)
          }

Java

  // Fetch and activate Remote Config values
  mFirebaseRemoteConfig.fetchAndActivate()
    .addOnCompleteListener(this, new OnCompleteListener<Boolean>() {
        @Override
        public void onComplete(@NonNull Task<Boolean> task) {
            if (task.isSuccessful()) {
                boolean updated = task.getResult();
                Log.d(TAG, "Config params updated: " + updated);
            } else {
                Log.e(TAG, "Error fetching Remote Config", task.exception)
            }
          }
    });

Step 5: Add a real-time Remote Config listener

Add a real-time Remote Config listener to your app to ensure that changes you make to the Remote Config template are propagated to the client as soon as they're updated.

The following code updates the Remote Config object whenever a parameter value changes. Optionally, you can also configure an action inside the addOnCompleteListener activation:

Kotlin+KTX

      // Add a real-time Remote Config listener
      remoteConfig.addOnConfigUpdateListener(object : ConfigUpdateListener {
          override fun onUpdate(configUpdate : ConfigUpdate) {
              Log.d(ContentValues.TAG, "Updated keys: " + configUpdate.updatedKeys);
              remoteConfig.activate().addOnCompleteListener {
                  // Optionally, add an action to perform on update here.
              }
          }

          override fun onError(error : FirebaseRemoteConfigException) {
              Log.w(ContentValues.TAG, "Config update error with code: " + error.code, error)
          }
      }

Java

  // Add a real-time Remote Config listener
  remoteConfig.addOnConfigUpdateListener(new ConfigUpdateListener() {
      @Override
      public void onUpdate(ConfigUpdate configUpdate) {
          Log.d(ContentValues.TAG, "Updated keys: " + configUpdate.getUpdatedKeys());
                remoteConfig.activate().addOnCompleteListener(new OnCompleteListener<Boolean>() {
                  @Override
                  public void onComplete(@NonNull Task<Boolean> task) {
                      // Optionally, add an action to perform on update here.
                  }
              });
          }

      @Override
      public void onError(FirebaseRemoteConfigException error) {
          Log.w(ContentValues.TAG, "Config update error with code: " + error.getCode(), error);
      }
  });

Step 6: Assign Remote Config values to Vertex AI variables

Now that Remote Config is fully configured, update your code to replace hard-coded values with values sourced from Remote Config.

Replace hard-coded values for location, model name, system instructions, and user prompt with the values sourced from Remote Config.

Kotlin+KTX

// Initialize FirebaseVertexAI instance
// Optionally specify a location in which to run the service and access the model
val vertexAI = Firebase.vertexAI(location = remoteConfig.getString("vertex_location"))

// Initialize the Vertex AI service and the generative model
// Specify a model that supports system instructions, like a Gemini 1.5 model
val generativeModel = Firebase.vertexAI.generativeModel(
  modelName = remoteConfig.getString("model_name"),
  systemInstruction = content { text(remoteConfig.getString("system_instructions")) }
)

// To generate text output, call generateContent with the text input
val response = generativeModel.generateContent(remoteConfig.getString("prompt"))
print(response.text)

Java

// Initialize FirebaseVertexAI instance
// Optionally specify a location in which to run the service and access the model
FirebaseVertexAI vertexAI = FirebaseVertexAI.getInstance(remoteConfig.getString("vertex_location"));

// Initialize the Vertex AI service and the generative model
// Specify a model that supports system instructions, like a Gemini 1.5 model
GenerativeModel gm = FirebaseVertexAI.getInstance().generativeModel(
  /* modelName */ remoteConfig.getString("model_name"),
  /* generationConfig (optional) */ null,
  /* safetySettings (optional) */ null,
  /* requestOptions (optional) */ new RequestOptions(),
  /* tools (optional) */ null,
  /* toolsConfig (optional) */ null,
  /* systemInstruction (optional) */ new Content.Builder().addText(remoteConfig.getString("system_instructions")).build()
);
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

// Provide a prompt that contains text
Content userPrompt = new Content.Builder()
 addText(remoteConfig.getString("prompt"))
 build();

// To generate text output, call generateContent with the text input
ListenableFuture<GenerateContentResponse> response = model.generateContent(userPrompt);
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);

Step 7: Run the app

Build and run the app and verify that it works. Make changes to your configuration from the Remote Config page in the Firebase console, publish the changes, and verify the result.

Next steps