The Process of Building AI Assistant App

Introduction

Every day we hear more and more news about virtual assistants that are conquering the IT industry. Almost every large company is trying to do something different. Siri by Apple, Bixby by Samsung, Alexa by Amazon, Cortana by Microsoft, etc. You can order a pizza by saying, “Siri, I want a pizza,” or you can get information about route by asking your device a few questions. Thanks to the abundance of such AI assistants, they don’t seem unusual.

Many companies want to develop an AI assistant app or to customize already existing ones. But why? Well, any company wants to make its business flexible. Using chat or voice bots, it can simplify generic human operations. This is really important for businesses like an e-commerce, banking, telecom, or service sector. For instance, one can robotize processes of decision-support or accept orders via state-of-the-art solutions by engineers from leading IT corporations.

And how do you make personal AI assistant app?

In this article I’ll answer this question and demonstrate how you can make your own AI assistant quickly. First, I want to cover the technologies that are behind AI Bots.

Technologies in AI Assistants

Virtual assistants are built on roughly the same technologies. Consider the main ones:

  • Speech-to-text. System converts user’s speech to text. This is used, for example, when generating search queries.
  • Text-to-speech. Convert text to speech. This is very useful for remote telephone control, e.g. while driving. Also, it makes your phone more “alive”.
  • Voice tagging. This function is very useful for improved authentication. It means that assistant could recognize your voice and respond only to your commands.
  • Voice recognition. This technology lets the assistant understand what you’re saying, and more specifically understand the meaning of your words.
  • Voice entity recognition. Methods for understanding what you said by context. For example, if you will ask your AI Assistant “Will I need an umbrella tomorrow?” it understand that you want to get some information about weather forecast.

Three ways to develop AI assistant apps

Virtual assistants can be a service, or they can be included in a mobile app. So, how do you make it? We have three ways to create your own Siri.

In the first, you can use ready-made solutions like a API from Siri, Cortana or Google Assistant. It’s so simple.

The second method involves using the open-source libraries and API (like a Melissa, Jasper, wit.ai and Dialogflow). It’s more difficult, but your virtual assistant will be more flexible and powerful.

And the last method — develop all components from scratch and integrate them into your mobile app after that.

Simple method

Using this method you can integrate trusted technologies from famous companies — e.g. Apple, Microsoft, Amazon, etc.

If you want create Siri-integrated apps, you’ll need Apple’s SiriKit. In this picture, you can see how it works. A user sends a voice request, the system defines the intents’ characteristics and sends it to the Intents App Extension, it processes the received data, if needed, uses your app’s services and then shows the result to the user.

In the second way, you can create Cortana-bases dapplications. Microsoft has developed very powerful instruments for creating your virtual assistant. The Cortana Dev Center provides you with detailed information on using Cortana’s skill in your app and integrating the app’s name into a voice command.

You may integrate the app’s name into a voice command using these three ways:

  • Prefixal. The name of the app stands before the command: ‘SML, get some information.’
  • Infixal. The name of the app stands in the middle of the voice command: ‘Get some info SML for me, please!’
  • Suffixal. The name of the app stands at the end of a command: ‘Get some information SML.’

Cortana represents many different skills for bots and improves performance of your app.

Advanced methods

This solution is based in open-source libraries and systems. Here are a some cross-platform tools for developing assistant apps.

  • Melissa. It’s a virtual assistant for OS X, Windows and Linux systems. She currently uses Google Chrome’s speech-to-text engine, OS X’s say command, Linux’s e-speak command.
  • Dialogflow. It incorporates Google’s machine learning expertise and products such as Google Cloud Speech-to-Text and runs on Google Cloud Platform, letting you scale to hundreds of millions of users.
  • Wit.ai. It’s so similar to Dialogflow. It requires two components to be set up: intents (users request) and entities (intent’s characteristics). Wit.ai has a good long list of intents, so you don’t have to build it yourself. And it is completely free for private and public versions.

Hard level. For real professionals

If you have some expertise in Deep Learning, Natural Language Processing and Conversational systems, you can develop your own infrastructure for AI Assistant.

Deep Learning Libraries

In this way you can use TensorFlow or PyTorch.

TensorFlow is an open source software library for numerical computation using data flow graphs. Thanks to it’s flexible architecture, you may deploy computation to multiple CPUs or GPUs on a desktop, server, or mobile device with a single API.

PyTorch is a python package that provides two high-level features:

  • Tensor computation (like numpy) with strong GPU acceleration;
  • Deep Neural Networks built on a tape-based autodiff system.

These are very powerful, flexible and portable instruments. You can create a mobile AI assistant with TensorFlow that is trained on data, such as SpeakEasy AI, which was a chatbot built on a neural model that was trained on millions of comments from Reddit.

Azure ML Studio

Azure ML Studio is a Microsoft product that gives its users the opportunity to create and train models, which then can be made into APIs. With Azure, you get a wide range of algorithms.

Azure ML Studio’s features:

  • Natural language processing
  • Recommendation systems
  • Computer vision
  • Predictive modeling

But in this article, we will tell you how to develop an AI assistant app using an equally cool solution.

Tutorial: How to make your own AI Assistant

In this part we will develop simple AI Assistant for Telegram using Dialogflow and Python 3.

First step: Creating bot into Telegram

Let’s think up a name and write to @botfather. After that, we will receive an API token. We need to save it.

Second step: Writing a bot’s foundation

Create folder, e.g. “Bot”, and create a file named bot.py. We will write the code of our bot.

pip install python-telegram-bot – -upgrade

Open the console, go to the directory with bot.py and install python-telegram-bot

After that, we will already be able to write a “foundation”, which will be simple and will respond with the same type of message. We will import the necessary modules and register our API token:

# Settings
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
updater = Updater ( token = ‘Your API token’)
dispatcher = updater.dispatcher

Next, we should write to handlers. This callback-function will be called when the update is received. We need two functions for the command “/start” for any text message. We pass, two arguments — bot and update. Bot contains the necessary methods for interacting with the API, and update contains information about the incoming message.

# Callbacks
def startCommand(bot, update):
bot.send_message(chat_id=update.message.chat_id, text=‘Hello, How are you?’)
def textMessage(bot, update):
response = ‘Got your message:’ + update.message.text
bot.send_message(chat_id=update.message.chat_id, text=response)

Let’s write handlers. It’s so simple:

# Handlers
start_command_handler = CommandHandler(‘start’, startCommand)
text_message_handler = MessageHandler(Filters.text, textMessage)
# Add handlers to dispatcher
dispatcher.add_handler(start_command_handler)
dispatcher.add_handler(text_message_handler)
# Start searching for updates
updater.start_polling(clean=True)
# Stop bot, if clicked Ctrl + C
updater.idle()

As a result, we have a full foundation for our bot.

Okay, let’s check how our bot works. Insert our API token, save changes, go to the console, and start the bot:

python bot.py

Write some words for our bot after starting:

The foundation of the bot is written! We shall proceed to the next step!

Note: Don’t forget stop your bot using Ctrl+C

Third step: AI Installation

Now you should register in Dialogflow. (You can do it using your Google acc). We will get to the Control Panel.

You can integrate your assistant in difference apps, e.g.:

Click the button “Create agent” and fill in the fields.

Click “Create” and you will see the following:

In the tab “Intents,” we can see the commands on which the bot is working. Now it can reply to phrases like “Hello”, or if it doesn’t understand, it answers “I didn’t understand you”. Not very impressive.

After creating our “empty” agent, we will have many other tabs. We should press “Prebuilt Agents” (it’s specially trained agents, who have a lot of commands). We will use Small Talk, but you can use some different agents for your business.

Click Import and OK. The agent was imported and now we can set it up. We can edit the agent’s name, timezone, and language.

Go to General and copy Client Access Token.

Cool! Our AI is fully tuned. You can go back to the bot.

Fourth step: Putting it all together!

The AI is ready. The base of the bot is ready. What’s next? Next, we need to download the wrapper API from Dialogflow for Python.

pip install apiai

Import apiai and json (JSON for work with answers from digitalflow).

# Settings
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import apiai, json
updater = Updater(token = ‘API Token for Telegram’)
dispatcher = updater.dispatcher

Okay, go to method textMessage and send the recieved message to Dialogflow’s servers.

def textMessage(bot, update):
request = apiai.ApiAI(‘Your API token’).text_request() # Dialogflow’s token
request.lang = ‘en’ # Language
request.session_id = SMLBot # Session ID
request.query = update.message.text # Send request

This code should send requests to Dialogflow. But we must receive an answers. Write few lines:

def textMessage(bot, update):
request = apiai.ApiAI(‘Your API token’).text_request() # Dialogflow’s token
request.lang = ‘en’ # Language
request.session_id = SMLBot # Session ID
request.query = update.message.text # Send request
responseJson = json.loads(request.getresponse().read().decode(‘utf-8’))
response = responseJson[‘result’][‘fulfillment’][‘speech’] # Parse JSON and get answer.
# If we have responses from bot — send to user, else — bot doesn’t understand you
if response:
bot.send_message(chat_id=update.message.chat_id, text=response)
else:
bot.send_message(chat_id=update.message.chat_id, text=‘I don’t understand you:( ‘)

A few explanations.

Using

request.getresponse().read()

we get an answer from the server, encoded in bytes. We must decode it using method

decode(‘utf-8’)

And parse json answer using

json.loads()

If we hadn’t recieved an answer, it means that Small-Talk did not understand the user (you can this study later). Therefore, if there is no “answer”, then we write to the user “I don’t understand you!”. Total, the full bot code with AI will look like this:

# Settings
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import apiai, json
updater = Updater(token  = ‘Your API Token’) # Telegram token

dispatcher = updater.dispatcher
# Callbacks

def startCommand(bot, update):
bot.send_message(chat_id=update.message.chat_id, text=‘Hello, how are you?’)
def textMessage(bot, update):
request = apiai.ApiAI(‘Your Dialog API token’).text_request() # Token API for Dialogflow
request.lang = ‘en’ # Requests language

request.session_id = SMLBot # Session ID
request.query = update.message.text # Send request

    responseJson = json.loads(request.getresponse().read().decode(‘utf-8’))
response = responseJson[‘result’][‘fulfillment’][‘speech’] # Parse JSON and get answer

    # If we have responses from bot — send to user, else — bot doesn’t understand you
if response:
bot.send_message(chat_id=update.message.chat_id, text=response)
else:
bot.send_message(chat_id=update.message.chat_id, text=‘I don’t understand you:( ‘)
# Handlers
start_command_handler = CommandHandler(‘start’, startCommand)
text_message_handler = MessageHandler(Filters.text, textMessage)
# Add handlers to dispatcher

dispatcher.add_handler(start_command_handler)
dispatcher.add_handler(text_message_handler)
# Search updates
updater.start_polling(clean=True)
# Stop bot if was clicked Ctrl + C
updater.idle()

Save changes and start bot:

That’s all!

Last step: At the end

I think you were convinced that writing a bot with AI itakes 10 minutes. All that remains is to train it. By the way, you can do it in menu “Training”. There you can see all the messages that were written and that the bot responded to (or did not answer). There it can also be trained, telling the bot where he answered correctly, and where not.

How to make personal AI assistant app: conclusion

AI in virtual assistants is a big trend today and many companies want to create their own service or mobile app. Now you have more information about the development process and you can use it in the creation of your own Siri.

If you have additional questions, please don’t hesitate to contact us. You will receive a free consultation on machine learning from our experts.