A.I - the frontiers of future human race
Ruptapas Chakraborty
Senior Manager (Mobile Solutions Architect) at Capgemini
This article provides a highlevel overview of building a chat bot a.k.a Software Robot using one of industries leading ai & machine learning service " api.ai".
First thing first, the below image is the high-level architecture that encompasses the whole system that plays a part in building this "weather bot"
As we now have the architecture in place, let's go ahead and build our Bot!!!
STEP 1: We start by creating an "api.ai" account. You can also use your Google account to register for the service.
STEP 2: In the api.ai console, you first need to start by creating an "agent". Here agent means the bot that you plan to make. Once created you should get a screen like below.
STEP 3: The next step would be to create an "Intent" Once created you should see a screen like below.
Now, here you need to concentrate on few section. This is the place where you start setting up your Bot. Under "User says" section, you start by asking some commonly used questions that you think the user of your app might ask. As this is a weather app I asked: "What's the weather in Pune?" The system automatically highlights "Pune" as it identifies that it's a name of a place and also assigns the entity "@sys.geo-city". The system when ready will internally refer "@sys.geo-city" when you will ask similar query with a different place name. This is where Machine Learning & Natural Language Processing comes into play.
Next is the "Action" section. Here I have assigned a value "yahooWeatherForecast". This is the name of the action that is written in the Python weather module. So adding this value means that whenever the api.ai system encounters a weather query, it will call this action using a webhook.
Webhook is nothing but your web service will receive a POST request from API.AI in the form of the response to a user query matched by intents with webhook enabled. You need to enable webhook by clicking "Fulfillment" option in the api.ai console. Refer image below:
STEP 4: You may now wonder, how did I get the URL mentioned in the above image. Well, this is when our favorite cloud-based hosting "Heroku" comes into play. For this example, the best would be to search for the Python api.ai module in Github and from there host the module into "Heroku". Refer the link: https://github.com/api-ai/apiai-weather-webhook-sample. Once hosting is successfully completed, you will get the URL. Remember to add "/webhook" to the url.
NOTE: Also remember to check the box "Use webhook" under your "Intent" section.
This is a right time to pat your back. You have completed configuring the backend of your bot.
STEP 5: Here you need to create a simple chat UI using your favorite programming language. I have used Swift.
Once the UI is ready, you need to make the following call to the api.ai service
import ApiAI
@IBAction func sendMessageButtonTapped(_ sender: AnyObject) {
let spacing = CharacterSet.whitespacesAndNewlines
let message = messageTextView.text.trimmingCharacters(in: spacing)
// TODO: add your custom method to add your query request to chat window
}
func apiaiRequest(ChatText textValue:String){
let request = ApiAI.shared().textRequest()
request?.query = [textValue]
request?.setMappedCompletionBlockSuccess({ (request, response) in
let response = response as! AIResponse
if response.result.action == "yahooWeatherForecast" {
print(response.result.fulfillment.value(forKey: "speech")!)
let chatText = response.result.fulfillment.value(forKey: "speech")
// TODO: add your custom method to add response to chat window
}
}, failure: { (request, error) in
})
ApiAI.shared().enqueue(request)
}
That's it, your weather bot app is ready; Let's Chat :)
This article would not have been possible without mentioning
- api.ai
- heroku.com
- https://developer.yahoo.com/weather/
- https://www.python.org/dev/
- https://github.com
On a slightly different note, I will like to share my blog's link which talks about the fast changing technological trends.
Hope you like this article.
Software Architect @Amdocs | Ex Fujitsu | Mobile(iOS & Android) Apps Architect | OTT App Streaming - Automotive - Banking and Finance - CRM | Building Autonomous Retail Store
7 年Nice article Ruptapas Da