Build a WhatsApp Chatbot with Mantium & Twilio

By Blessing Adesiji

April 7, 2022   ·   15 min read

In this tutorial, we will build a WhatsApp chatbot powered by AI, capable of conversational question and answering (QA). This type of chatbot is a generative chatbot, in contrast to the more common retrieval based chatbot. To create the WhatsApp bot, we will use Mantium’s Python library, Twilio WhatsApp API, and the Flask framework. The Mantium API enables us to get a generative response, as it supports and provides access to OpenAI’s GPT-3 model.

Chatbots in business

Chatbots are applications that generate and process written or spoken human conversations in a way that allows interactions with digital devices as if they were conversing with actual people. You may have interacted with a chatbot recently while browsing websites on the internet. You probably noticed that the chatbot automatically engages you in a pleasant manner while trying to help achieve the objective of your visit to the site.

Chatbots have seen great applications in businesses, such as automating customer interactions, improving sales channels, etc., and they are becoming a lot smarter. They are capable of handling near-human-level interactions.

With recent advancements in the field of natural language processing (NLP), it is easier to solve language-based tasks such as generating and classifying text. It becomes even easier to use with Mantium, as they provide the development support needed to build effectively with large language models. If you are wondering what OpenAI’s GPT-3 model is, it is the third generation prediction model that OpenAI created, and it has a capacity of 175 billion machine learning parameters.

With Mantium, we will use this to build a conversational QA WhatsApp bot in just a few lines of code. You don’t need to have a solid knowledge of machine/deep learning to build a use case with Mantium.

Requirements to build a WhatsApp chatbot

To follow this tutorial you need the following;

  • A Mantium Account
  • An OpenAI API Key
  • Twilio Account
  • Flask Framework
  • Local Tunnel
  • A smartphone with an active WhatsApp Account

Prompt Design in the Mantium Platform

The first and essential step is to design the prompt (task) with the Mantium Platform. Prompt design is our way of guiding the GPT-3 model to give a favorable and contextual response as it enables us to provide a text input and get a generative response from the model. Think of this like you are playing the game of charades where you give the players information to figure out the secret word. Similarly, with GPT-3, we provide just enough information for the model to recognize patterns in the training prompt.

To do this, you don’t need to have prior machine learning experience, and almost anyone can do this in a No-Code approach to build a simple text application using the Mantium Platform.

I will break this into more straightforward steps that you can follow.

Step 1 – Register for a Mantium Account

Visit app.mantium.com to create an account.

Step 2 – Connect your OpenAI API key

To access the GPT-3 model through the OpenAI API, you must connect your OpenAI API key on the Mantium Platform. Click on AI Providers, click Add API Key on the left sidebar, and paste your OpenAI key. Notice that Mantium supports other AI providers such as Cohere, AI21 that you can connect with their respective API keys, but for this tutorial, we build with OpenAI.

If you don’t have your API key for any of the AI Providers, view this documentation to learn how you can get one.

Step 3 – Add New Prompt

The next step is to add a new prompt. Navigate to the AI Manager drop down tab on the left sidebar, click on Prompts, and click on the Add new prompt button, as shown below.

Step 4 – Prompt Design

Here, we will provide the information to build our training prompt.

A. Basic Info

Let’s fill in the basic information to identify our prompt. Note that you can provide any name and description.

Name of Prompt – WhatsApp Chatbot

Description – This is a WhatsApp bot

B. Provider

In this tutorial, we are using the OpenAI API, select OpenAI from the drop-down.

C. OpenAI Info

  1. Choose an Endpoint – Select the Completion endpoint. This endpoint provides the model with some text and generates a text completion that tries to fit the context of the provided text.
  2. Prompt Line – Here we are going to provide the model with examples, and it will figure out the patterns to perform the given task. Below is a prompt I designed for our conversational QA generative task.
Human: Hello, who are you?
AI: I am an AI created by Blessing. How can I help you today?
###
Human: What is a language model?
AI: A language model is a statistical model that describes the probability of a word given the previous words.
###
Human: What is Cryptocurrency?
AI: A cryptocurrency (or “crypto”) is a digital currency that can be used to buy goods and services, but uses an online ledger 
with strong cryptography to secure online transactions.
###
Human: What country won the 2010 world cup?
AI: Spain
###
Human: How many pounds are in a kilogram?
AI: There are 2.2 pounds in a kilogram.
###
Human: When did the first airplane fly?
AI: On December 17, 1903, Wilbur and Orville Wright made the first flights.
###
Human: How can I learn swimming?
AI: There are many ways you can learn to swim. The first thing you can do is talk to your parents and ask if they know any instructors. You can also look online to find a class that you can take in your area.
###
Human:

Copy and Paste this in the Prompt Line input field.

3. Choose an Engine – The OpenAI engine determines the language model used to execute tasks. OpenAI supports various models with varying capabilities. These models are described and accessed using engines. For this tutorial, select the “Davinci” Engine.

OpenAI Basic Settings

Here you will provide the basic settings for the OpenAI model. See image below.

Response Length - 150
Temperature - 0.9
Top P - 1
Frequency Penalty - 0.5
Presence penalty - 0.6
Stop Sequence - ###

Basic Settings Explanations

  1. Response Length – The response length sets a limit on how much text the OpenAI API includes in its completion. Take note of your response length, as OpenAI charges by the token, where one token is roughly 4 words. The max tokens controls the length of your completion and your prompt plus completion can’t add up to more than 2,049 tokens. For this example, on the Mantium Platform, input 150 as your response length.
  2. Temperature & Top P – The temperature controls the randomness of the response. It is one of the most important settings as it significantly impacts the output. Increase the temperature if you want more creative completions or if you want to let the model go off on a tangent. Decrease the temperature if you want the model to follow instructions or remain factual. Note that, If you alter the temperature, it’s recommended to not alter top_p as it defaults to 1. Set the value to 0.8.
  3. Frequency Penalty – This prevents word repetition. If you notice the output of a model is repeating the same word, and you want it to stop repeating the same word, increase the frequency_penalty.
  4. Presence Penalty – The presence penalty prevents topic repetition. If you notice the output of a model is stuck on a particular topic, and you want it to talk about something else, try increasing the presence_penalty.
  5. Stop Sequence – A stop sequence is a set of characters that signals the API to stop generating tokens.

Advanced Settings

We can leave the Advanced settings to the default value for this tutorial.

For more information about these settings, you can read this tutorial by Mantium.

Step 5 – Testing – Prompt Results

On the Mantium Platform, we will use the Input field to test our prompt. See the image below. I asked the question, “How can I learn Machine Learning”, and I got an interesting response.

Fill in the input field, and hit the Test run button.

Congratulations on completing the first essential step! Note that you can do the above step in code by using the Mantium API/Client Library, but I chose to explain the no-code approach to give you that first understanding and easy debugging.

With Mantium’s Deploy feature, you can quickly generate a single-page application that you can share with your friends or colleagues to test your chatbot. Our end goal, however, is to create a WhatsApp chatbot. So, in subsequent steps, we will grab this prompt using Mantium’s Client Library and integrate it with the Twilio WhatsApp API in Python.

Configure the Twilio WhatsApp Sandbox

For this tutorial we will use the Twilio WhatsApp Sandbox, where will develop and test our WhatsApp bot application.

Step 1 – Create an Account

Create an account with Twilio, and visit the Twilio Console, where you will click on Explore Products and select Messaging.

Step 2 – Setup Sandbox

Navigate to the side bar, under Messaging and Settings, select the WhatsApp sandbox settings.

Connect your sandbox by sending a WhatsApp message with the given code to the number assigned to your account. Twilio will send a reply stating that your sandbox is set, and you can complete the Two-way setup by sending “Hello”.

You can share the number assigned to your account for others to connect, and test your application in the Sandbox.

The last step is to configure the sandbox, which we will come back to.

Let’s Build the WhatsApp bot!

Here we are going to write Python code to build the WhatsApp chatbot. The process is easy. We will build and test our application with a few lines of code. We are going to take the user input (Your WhatsApp incoming message), pass the message as an input through the Mantium execute prompt method, which sends this request to the model and returns a response. This response is processed by Twilio, and it is returned as a reply on your WhatsApp application.

To better understand what we are trying to achieve, see the illustration below.

Let’s write some code!

Step 1 – Setup a Python Virtual Environment

Create a Python virtual environment by using the command below:

$ mkdir whatsapp-bot
$ cd whatsapp-bot
$ python -m venv whatsapp-bot-venv
$ source whatsapp-bot-venv/bin/activate

After this, create a file named app.py in the root directory of the whatsapp-bot folder.

Step 2 – Setup Mantium’s Client Library

Mantium has provided a Python client library to improve the developer’s experience, and we will use that in this tutorial.

Installation

Using the command below, install the Python client library.

pip install mantiumapi

Authenticate

Here you will authenticate by providing your log in credentials in an environment variable.

With the command below, install python-dotenv.

pip install python-dotenv

After this, create a .env file to store your credentials. Input your login credentials as shown below.

MANTIUM_USER='youremail.com'
MANTIUM_PASSWORD='your-password'

In app.py , you will use the code below to authenticate your Mantium account and create a prompt. Notice that I imported the Mantium prompt method, more on that later.

from mantiumapi import prompt
from mantiumapi import client

from dotenv import load_dotenv

# load Mantium credentials
load_dotenv()
mantium_user = os.getenv("MANTIUM_USER")
mantium_password = os.getenv("MANTIUM_PASSWORD")

#Mantium Token
mantium_token = client.BearerAuth().get_token()

Here is the link to the Mantium GitHub repository for more.

Step 3 – Setup Webhook with Flask Framework

Installation

Here we need to install the Flask framework, to create the web application, and Twilio Python Helper Library to work with Twilio APIs.

pip install twilio flask

Now let’s setup webhook with the Flask framework. We will create an endpoint that listens to POST requests, and when Twilio receives the incoming message they will invoke this endpoint. The body of the function is what processes the input message, and provides a response after Mantium executes the input.

Here is a starter template, in this we will input the body of the bot function.

from flask import Flask, request


# Init Flask App
app = Flask(__name__)

@app.route('/')
def hello():
  return "Welcome to Mantium WhatsApp Bot"
  
@app.route('/bot', methods=['POST'])
def bot():
    # send input to Mantium and return a response


if __name__ == '__main__':
    app.run()

Step 4 – Send Input to Mantium, and Return a Response

from flask import Flask, request

# Init Flask App
app = Flask(__name__)

@app.route('/')
def hello():
  return "Welcome to Mantium WhatsApp Bot"
  
@app.route('/bot', methods=['POST'])
def bot():
    incoming_msg = str(request.values.get('Body', '').lower())
    print(incoming_msg)
    responded = False
    if type(incoming_msg) == str:
      qa_prompt = prompt.Prompt.from_id('98b58a5d-12ff-4e64-868e-4dabf986eac7')
      result = qa_prompt.execute(incoming_msg)
      result.refresh()
      prompt_result = str(result.output)
    else:
      prompt_result = 'Check your Input and try again'
    resp = MessagingResponse()
    msg = resp.message()
    msg.body(prompt_result)
    responded = True
    return str(resp)
    
if __name__ == '__main__':
    app.run(debug=True)

Let me explain the code above.

Input message – This gets the user WhatsApp input message

incoming_msg = str(request.values.get('Body', '').lower())

You need your Prompt ID. Remember the prompt that we created above. Let’s grab the ID. The easiest way to get this is to use the URL of the prompt editing page . See the link below, the last string is your prompt ID.

https://app.mantiumai.com/ai-manager/prompt/{prompt_id}

Using the from_id method, retrieve the prompt from Mantium.

qa_prompt = prompt.Prompt.from_id('98b58a5d-12ff-4e64-868e-4dabf986eac7')

Execute the Input Mantium

Here we will send the Prompt to the OpenAI API endpoint, and we will get the response from the GPT-3 model. You need to refresh because this is asynchronously done to get a result.

result = qaPrompt.execute(incoming_msg)
result.refresh()
prompt_result = str(result.output)

Step 5 – Twilio Messages and Response

Twilio expects a response from the webhook in TwiML or Twilio Markup Language, which is an XML-based language format. With the Twilio helper library, we can easily create this response without having to create XML directly.

from twilio.twiml.messaging_response import MessagingResponse

resp = MessagingResponse()
msg = resp.message()
msg.body('this is the response text')

Putting everything together

This is the full code.

import os
from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse
from dotenv import load_dotenv

# load Mantium credentials
load_dotenv()
mantium_user = os.getenv("MANTIUM_USER")
mantium_password = os.getenv("MANTIUM_PASSWORD")

from mantiumapi import prompt
from mantiumapi import client

# Mantium Token
mantium_token = client.BearerAuth().get_token()

# Init Flask App
app = Flask(__name__)


@app.route("/")
def hello():
    return "Welcome to Mantium WhatsApp Bot"


@app.route("/bot", methods=["POST"])
def bot():
    incoming_msg = str(request.values.get("Body", "").lower())
    print(incoming_msg)
    responded = False
    if type(incoming_msg) == str:
        qaPrompt = prompt.Prompt.from_id("98b58a5d-12ff-4e64-868e-4dabf986eac7")
        result = qaPrompt.execute(incoming_msg)
        result.refresh()
        prompt_result = str(result.output)
    else:
        prompt_result = "Check you input and try again"
    resp = MessagingResponse()
    msg = resp.message()
    msg.body(prompt_result)
    responded = True
    return str(resp)


if __name__ == "__main__":
    app.run(debug=True)

Testing the WhatsApp bot

Step 1 – Run the app.py file

Run the command below to start the chatbot, and it will run at port 5000 on your local machine. See the image below.

python app.py

Step 2 – Local Tunnel

Localtunnel is a simple tool that provides a publicly-accessible URL that reroutes every request to the locally-running service. It exposes our localhost to the world for easy testing and sharing, which we need to connect the URL to Twilio (Remember the step that we skipped when configuring the Twilio sandbox)

You can create another terminal beside the initial terminal, and install the Localtunnel tool. Use the command below.

npm i localtunnel

After this, run the command below and the browser window below automatically pops up. I can see that the app is working.

Take note that my app is running on port 5000.

lt --port 5000 --subdomain shan --local-host "127.0.0.1" -o --print-requests
WhatsApp bot command

Step 3 – Configure Twilio Sandbox

On the Twilio Console, paste the URL below in the WHEN A MESSAGE COMES IN field, since our WhatsApp chatbot is exposed under the /bot URL. After this, click on the Save button.

URL – https://shan.loca.lt/bot

Step 4 – Send and Receive Messages

Now that the configuration is all done and the server is running, it’s time to test the WhatsApp bot.

Logging with Mantium

You can use the Mantium’s Log feature to check the output and status of your prompt. This can come in handy when you are not getting any reply to your WhatsApp message.

Navigate to the Monitoring tab on the left-side, click on Logs.

See the WhatsApp chatbot in action

The goal of the prompt for for this chatbot was to show the possibilities of large language models(LLM), and how you can use the Mantium API integrated with Twilio’s API to build something interesting. There are lot of ways that you can spin this tutorial to build custom use cases. Examples include Intelligent Customer Service bot and Virtual AI assistant etc.

This article was originally published on Analytics Vidhya.

To learn how to build an SMS Chatbot with Twilio, click here!

ABOUT THE AUTHOR

Blessing Adesiji
With a Bachelor's of Science in Petroleum Engineering, Blessing is a self taught Data Scientist and Software Engineer. He enjoys educating Mantium users on how to build AI applications as a Developer Relations Engineer. When he is not writing code and tutorials, he enjoys doing exercise in the gym, and playing and watching football.

Enjoy what you're reading?

Subscribe to our blog to keep up on the latest news, releases, thought leadership, and more.