Firebase ML Kit 101 : Smart Reply

Hitanshu Dhawan
AndroIDIOTS
Published in
3 min readMar 31, 2019

Smart Reply is an API that lets you generate relevant replies to messages.

You must have seen several apps like LinkedIn and Gmail that uses the power of machine learning to generate relevant and appropriate replies based on the full context of the conversation. It makes replying much easier than typing it.

ML Kit’s Smart Reply API works on the device itself and doesn't require you to send data to a remote server.

Firebase ML Kit Series

In this series of articles, we will deep dive into different ML Kit APIs that it offers…

Let’s look into the ML Kit’s Smart Reply API and how we can integrate it into our apps.

Let’s Code!

Step 1 : Add Firebase to your app

Offcourse! You can add Firebase to your app by following the steps mentioned here.

Step 2 : Include the dependencies

You need to include the ML Kit dependencies in your app-level build.gradle file.

dependencies {
// ...
implementation 'com.google.firebase:firebase-ml-natural-language:18.2.0'
implementation 'com.google.firebase:firebase-ml-natural-language-smart-reply-model:18.0.0'
}

Step 3 : Get! — the Text(s)

The Smart Reply model requires a List of FirebaseTextMessage objects to generate smart replies.

when the user sends a message

conversation.add(FirebaseTextMessage.createForLocalUser("Hi, how are you?", System.currentTimeMillis()))

when the user receives a message

conversation.add(FirebaseTextMessage.createForRemoteUser("I'm good", System.currentTimeMillis(), userId))

Here, userId is a string that uniquely identifies the sender in that conversation.

Step 4 : Set! — the Model

Now, It’s time to prepare our Smart Reply model.

val smartReply = FirebaseNaturalLanguage.getInstance().smartReply

Step 5 : Gooo!

Finally, we can pass the conversation to the model for generating smart replies.

smartReply.suggestReplies(conversation)
.addOnSuccessListener { result ->
if (result.status == STATUS_SUCCESS) {
// Task completed successfully
}
}
.addOnFailureListener {
// Task failed with an exception
}

Step 6 : Extract the information

Voilà! That’s it!
If the generation of smart replies was successful, the success listener will receive a SmartReplySuggestionResult object, which contains a list of top 3 suggested replies.

You can extract this information like this.

Have a Look!

This is what you can achieve with ML Kit’s Smart Reply API.

The full source-code with other ML Kit APIs can be found here!

Thanks for reading! Share this article if you found it useful.
Please do Clap 👏 to show some love :)

Let’s become friends on LinkedIn, GitHub, Facebook, Twitter.

--

--

Hitanshu Dhawan
AndroIDIOTS

Senior Software Engineer @ Urban Company | Google Certified Android Developer