Firebase ML Kit 101 : Smart Reply
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…
- Firebase ML Kit 101 : Introduction
- Firebase ML Kit 101 : Text Recognition
- Firebase ML Kit 101 : Face Detection
- Firebase ML Kit 101 : Barcode Scanning
- Firebase ML Kit 101 : Image Labeling
- Firebase ML Kit 101 : Landmark Recognition
- Firebase ML Kit 101 : Language Identification
- Firebase ML Kit 101 : Smart Reply (you’re here)
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.
Here’s the source code for the above app…
Firebase ML Kit Series
Don’t forget to have a look at other ML Kit APIs covered this series of articles.
- Firebase ML Kit 101 : Introduction
- Firebase ML Kit 101 : Text Recognition
- Firebase ML Kit 101 : Face Detection
- Firebase ML Kit 101 : Barcode Scanning
- Firebase ML Kit 101 : Image Labeling
- Firebase ML Kit 101 : Landmark Recognition
- Firebase ML Kit 101 : Language Identification
- Firebase ML Kit 101 : Smart Reply