AI-Based Sentiment Analysis Chatbot for WhatsApp and SMS with OpenAI, Twilio Studio and Twilio Functions

Developers working on AI-based app
July 26, 2023
Written by
Reviewed by
Paul Kamp
Twilion

By now, you probably know that ChatGPT is a language model developed by OpenAI. It is part of the GPT (Generative Pre-trained Transformer) family, which means it uses deep learning techniques to generate human-like text based on the given input. As an AI language model, ChatGPT is designed to simulate conversations and respond to user prompts and questions. It has been trained on a large corpus of text data from the internet to acquire a broad range of knowledge on various topics. It has various applications, among them backing chatbots, virtual assistants, and interactive storytellers.

In this blog post, I will teach you how to get started on building an intelligent chatbot, capable of sentiment analysis, using OpenAI’s API, Twilio Functions and Twilio Studio. We will build with Twilio’s WhatsApp Programmable Messaging API as the text channel, but it works exactly the same if you want to use Twilio’s SMS Programmable Messaging API.

Prerequisites

Before you get started, you’ll need to set up a few things:

Get the OpenAI API Key

After creating your OpenAI account, you'll need an API Key. If you don’t know how to get one, here is where you can create or find it. Save that API key for our next step to use the OpenAI client library in your Twilio Function.

Create a Twilio Function

First, we have to create a new Service under Functions and Assets in your Twilio Console. If you haven’t done it before, here is how to create a Service.

After that, inside your newly create Service, you can add your OpenAI API Key under Settings & More > Environment Variables:

OpenAI Environment Variable

Now it’s time to add OpenAI’s npm module and its version under Settings & More > Dependencies. You can either type “latest” as the version, or choose the version you want here.

OpenAI Npm Package

Ok, we’re ready to create our OpenAI function now. Click the Add + button to add a new function inside your Service and name whatever you like. Here we’ll name ours /openai.

Leave the visibility of this function to “protected”, since we will only use it inside Twilio’s environment. Here you can find more about visibility and how to set it.

Delete all the pre-generated code and paste the code below:

const { Configuration, OpenAIApi } = require('openai')

exports.handler = async function (context, event, callback) {
  const apiKey = context.OPEN_AI_KEY
  const configuration = new Configuration({ apiKey })
  const openAI = new OpenAIApi(configuration)

  const { message, systemContent } = event

  const executeAI = async () => {
    const completion = await openAI.createChatCompletion({
      model: 'gpt-3.5-turbo-16k',
      messages: [
        { role: 'system', content: systemContent },
        { role: 'user', content: message }
      ],
      temperature: 1,
      max_tokens: 256
    })

    return completion.data.choices[0].message.content
  }

  try {
    const content = await executeAI()

    return callback(null, { content })
  } catch (error) {
    console.log(error)
    return callback(error)
  }
}

Click Save and next click in the Deploy All button to deploy your function.

In this tutorial, I am using the gpt-3.5-turbo-16k model. If you are not familiar with GPT models, please read more about them here. By the time you read this, there is probably a newer, faster, and more efficient model, so feel free to change it accordingly.

Optional properties max_tokens and temperature specify the maximum completion length and control randomness. You can learn more about optional properties for completion in OpenAI's documentation.

Twilio Functions execute for at most 10 seconds. Any Function that exceeds the 10-second limit will be terminated, and the client will receive an HTTP 504 Gateway Time-out response.

For the vast majority of chatbots, you will not face problems with the 10-second limitation. If you are facing issues, make sure you follow GPT’s best practices to speed up the responses such as providing reference texts, splitting complex tasks into simpler subtasks, and fine-tuning. It's worth noting that ChatGPT may sometimes generate answers that are incorrect or lack context, so it’s even more important to pay attention to these best practices.

Create a Twilio Studio Flow

We will now create our chatbot using Twilio Studio. Studio is Twilio’s no code platform that enables the building of Twilio applications. If you are not familiar with it, here’s a great way to get you started.

Instead of creating a new flow from scratch and starting yourself, we will give you a head start. You can choose the option Import from JSON and paste the JSON content found here in this file.

Create Studio Flow

After you create the flow with the JSON content, make sure to point the “Run Function” widgets to the function you created earlier.

You will notice that there are 2 calls to our /openai function in our flow: fetch-sentiment and fetch-content. In a nutshell, the first call is for ChatGPT to understand the sentiment of the message, and the second call will get the content of what was said. In both function calls, we need to pass 2 parameters: message and systemContent.

Studio Flow

Message is the user input, and system content is the instruction we give to ChatGPT. For example, to get the sentiment, we can send the system content something like “You will be provided with a message, and your task is to classify its sentiment in one word: Positive, neutral or negative”. This way, ChatGPT will return one of those words in order for us to split the flow accordingly. And to get the content, we can just pass something like “Summarize for me in one paragraph”. It is important to understand that this example is not fully error proof. You can test different system instructions, as well as fine tune the prompt as mentioned above in order to get better results.

In our flow, we have a simpler decision split. If it’s negative, we right away transfer to a human agent. Under the negative scenario, you can forward the flow to another phone number, send this payload to your own application for a human to interact with the user, or even forward the user to our contact center solution, Twilio Flex. Twilio Studio allows you to integrate with outside applications and make external requests to send this payload wherever you like.

For the positive result, we just return a thank you message. And on neutral results (a question, for example, is a neutral sentiment) we will proceed to another call to fetch the AI response and send it back to the user.

Here is a short example gif showing all 3 scenarios:

Conversation Gif

Connect your Twilio Number to your Studio Flow

Now let's proceed to hook up your Twilio number to your Studio Flow. There are a few ways to do this inside Twilio's Console. In this tutorial I’ll teach you how to do it via Messaging Service for both WhatsApp Sender numbers and SMS Capable numbers.

Go to your Studio Flow, click on the configuration icon inside the Trigger widget, and copy the Flow’s Webhook URL:

Webhook URL

Next, follow this step by step guide on how to create a Messaging Service in your Twilio console.

On Step 3, “Set up Integration”, paste the Studio Flow Webhook URL you copied above under Send a webhook > Request URL:

Send a Webhook URL

After you completed all the steps and the service was created, you have 2 options depending on the number you have. 

For WhatsApp Sender numbers, you need to go into Messaging > Senders > WhatsApp senders. Now click on your WhatsApp number, and under Endpoint configuration, choose the option to Use a Messaging Service and select your newly created service in the dropdown menu:

WhatsApp Sender Config

For SMS Capable numbers, you need to go into Phone Numbers > Active numbers, click on your number, and under Messaging Configuration > Messaging Service, choose your newly created service in the dropdown menu:

Messaging Number Configuration

And that's it. All the incoming messages to your Twilio number will be directed to your Twilio Studio Flow automatically.

Conclusion

This tutorial showed a quick way to get you started building an AI-based sentiment analysis chatbot on WhatsApp and SMS. You can access this tutorial’s function code and Studio Flow JSON on GitHub here.

Now it’s up to you to be creative! Try making a request to your CRM in the beginning of the Flow, to see if the incoming number is from a known customer or a new customer. Create other branches in your Studio Flow. You get the gist; the sky's the limit to what you can do. We can’t wait to see what you build!


Gustavo Varallo is a Solutions Architect and part of the global Solutions Engineering enablement team at Twilio. He helps customers architect complex IT projects using cutting edge technology. Feel free to connect with him on LinkedIn here.