Generate smart replies with ML Kit on Android

  • ML Kit's Smart Reply API generates up to three relevant reply suggestions for English conversations using an on-device model.

  • You can integrate Smart Reply by either bundling the model with your app (larger size) or dynamically downloading it (smaller size, requires Google Play Services).

  • To use the API, provide a conversation history as input, and ML Kit will suggest replies to the last message if it's from a non-local user.

  • The suggested replies are returned only if the conversation is in English, does not contain sensitive content, and the model is confident in their relevance.

ML Kit can generate short replies to messages using an on-device model.

To generate smart replies, you pass ML Kit a log of recent messages in a conversation. If ML Kit determines the conversation is in English, and that the conversation doesn't have potentially sensitive subject matter, ML Kit generates up to three replies, which you can suggest to your user.

BundledUnbundled
Library namecom.google.mlkit:smart-replycom.google.android.gms:play-services-mlkit-smart-reply
ImplementationModel is statically linked to your app at build time.Model is dynamically downloaded via Google Play Services.
App size impactAbout 5.7 MB size increase.About 200 KB size increase.
Initialization timeModel is available immediately.Might have to wait for model to download before first use.

Try it out

  • Play around with the sample app to see an example usage of this API.

Before you begin

  1. In your project-level build.gradle file, make sure to include Google's Maven repository in both your buildscript and allprojects sections.

  2. Add the dependencies for the ML Kit Android libraries to your module's app-level gradle file, which is usually app/build.gradle. Choose one of the following dependencies based on your needs:

    • To bundle the model with your app:
    dependencies {
      // ...
      // Use this dependency to bundle the model with your app
      implementation 'com.google.mlkit:smart-reply:17.0.4'
    }
    
    • To use the model in Google Play Services:
    dependencies {
      // ...
      // Use this dependency to use the dynamically downloaded model in Google Play Services
      implementation 'com.google.android.gms:play-services-mlkit-smart-reply:16.0.0-beta1'
    }
    

    If you choose to use the model in Google Play Services, you can configure your app to automatically download the model to the device after your app is installed from the Play Store. By adding the following declaration to your app's AndroidManifest.xml file:

    <application ...>
          ...
          <meta-data
              android:name="com.google.mlkit.vision.DEPENDENCIES"
              android:value="smart_reply" >
          <!-- To use multiple models: android:value="smart_reply,model2,model3" -->
    </application>
    

    You can also explicitly check the model availability and request download through Google Play services ModuleInstallClient API.

    If you don't enable install-time model downloads or request explicit download, the model is downloaded the first time you run the smart reply generator. Requests you make before the download has completed produce no results.

    1. Create a conversation history object

    To generate smart replies, you pass ML Kit a chronologically-ordered List of TextMessage objects, with the earliest timestamp first.

    Whenever the user sends a message, add the message and its timestamp to the conversation history:

    Kotlin

    conversation.add(TextMessage.createForLocalUser(
            "heading out now", System.currentTimeMillis()))