Building a Toxicity Detection SMS Bot with Cohere and Twilio

We built a simple SMS bot that detects toxic language in incoming text messages. Learn how we integrated Cohere's large language model and Twilio's communications platform in this step-by-step guide.

Toxicity detection SMS bot powered by Cohere and Twilio
Toxicity detection SMS bot powered by Cohere and Twilio

Powerful language processing technology has historically not been easily accessible by software developers. The barriers to access are quite large, which include building a machine learning engineering team, months to years of work designing a language model architecture, scraping terabytes of internet data to build a training set, and spending millions of dollars on compute that’s powerful enough to train large language models.

Cohere builds high-performing large language models that truly understand human language. Our talented team of machine learning engineers has spent years building and training generative and representation language models, as well as building a platform that removes all of the infrastructure complexity. The Cohere platform enables a developer to use simple APIs to integrate Language AI into their applications.

Our new sample app: A toxicity detection bot

We wanted to showcase how easy it is to combine Cohere APIs with third-party APIs and integrate them into an application. So, we built a simple SMS bot that detects toxic language in SMS text and then provides the confidence level in the accuracy of its results.

With Cohere, you can fine-tune our models using your own dataset specific to your business needs. In this case, we fine-tuned Cohere’s Classify model using a free toxicity dataset from Surge AI, which contains two labels: Toxic and Not Toxic. The dataset required to fine-tune a Cohere model is fairly simple, and the Surge AI dataset is an already formatted CSV file with two columns: Text and Label.

Labeling data, or “Classification,” has many different use cases. Our SMS bot labels content as either “toxic” or “not toxic,” but you can also use labeled data to make sense of the content on your platforms, such as understanding key topics, themes, and trends. This allows you to create personalized experiences for your users, remove toxic content from your platform to create a safer community, and identify trends in real-time to provide your users with the most engaging and relevant content.

Our SMS bot classifies content as either “toxic” or “not toxic”
Our SMS bot classifies content as either “toxic” or “not toxic”

How we built our SMS bot

To create our SMS bot, we used Twilio’s communications platform to set up a phone number, built out a workflow in the Twilio Studio application builder, and then had the workflow pass text through our APIs to our toxicity model that we trained on a free dataset from Surge AI (as noted above).

We integrated Cohere's toxicity model and Twilio's communications platform to build this bot
We integrated Cohere's toxicity model and Twilio's communications platform to build this bot

If you want to build your own version of our SMS bot, here is a step-by-step guide.

Step 1

Sign up for Cohere (you’ll get free credits).

Step 2

A fine-tuned model named “cohere-toxicity” is available for use, however, you can fine-tune your own model to be used in your bot. You’ll need to create an API key in the Cohere platform and make note of your model ID (see example below).

const cohere = require('cohere-ai');
cohere.init('{apiKey}');
(async () => {
 const response = await cohere.classify('cohere-toxicity', {
   inputs: ["sample text"]
 });
 console.log(`The confidence levels of the labels are ${JSON.stringify(response.body.classifications)}`);
})();

Step 3

Create a Twilio account (you’ll get free credits). From the Twilio console, you’ll purchase a phone number and set up your Studio workflows. Note: You may not see the products in the left-hand side view. Click “Explore Products” to expand the view and pin the relevant products to your side view.

Here is the workflow we set up for this project. The inbound trigger used for this workflow is an SMS message. You can also play around with other inbound triggers, such as voice calls (you’ll need to also implement speech-to-text), or any API call into the workflow from an application that you’re building.

The Twilio workflow we set up for this project
The Twilio workflow we set up for this project

We built variable handling steps to ensure that the text input was being passed correctly to the function. We also included an error handler that would text back if there was an error in the workflow.

Step 4

Build the function that will communicate with the Cohere model. On the Twilio Console navigation pane on the left-hand side, select Functions and Assets and then Services. From this page, you’ll click Create Service, name your service (ours is “is-toxic”), and then click Add Function.

Creating a service and adding a function
Creating a service and adding a function

Remove the sample code and use the code below. If you fine-tune your own model, be sure to replace the model ID in the below sample to your specific model ID.

const cohere = require('cohere-ai');

exports.handler = async(context, event, callback) => {
  cohere.init(context.API_KEY)

  let response = await cohere.classify('cohere-toxicity', {
    inputs: [event.Text]
  })

  let classification = response.body.classifications[0]
  let input = classification.input
  let prediction = classification.prediction
  let confidence = classification.confidences.find(c => c.option === prediction).confidence
  confidence = Math.round(confidence * 10000) / 100 // Pretty format percentage: 0.23536 -> 23.54

  callback(null, {input, prediction, confidence})
}

Next, under Settings, click Dependencies and add Module cohere-ai with Version 3.2.0

Adding a module to the application
Adding a module to the application

After that, Under Settings, click Environment Variables and add the variable named API_KEY with the value of your Cohere API key.

Adding the Cohere API key as an environment variable
Adding the Cohere API key as an environment variable

Once it’s complete, select Deploy All.

Step 5

Navigate back to your active numbers and select the number you want to use for this SMS bot. At the bottom of the configuration page, you’ll see the handler for Messaging. For “A Message Comes In,” select Studio Flow from the drop down and then select the Studio Flow that you set up. Hit Save at the bottom.

Adding the Studio Flow you set up to the bot
Adding the Studio Flow you set up to the bot

Step 6

Text the number to execute the SMS bot and have fun. We’d love your feedback on this as well. Feel free to join our Discord server to engage live with our team, ask questions, or get support for building this SMS bot.

Conclusion

This demonstration highlights the power of leveraging Cohere’s API to perform natural language processing (NLP) tasks without needing the expertise or infrastructure. Whereas this would otherwise be within the reach of only teams with machine learning expertise, this abstracted interface allows developers to integrate NLP capabilities into their applications.

By integrating with platforms like Twilio, we can build applications that solve a specific problem. The steps we took to set up this system were relatively simple with a mostly no-code interface. It is exciting to think about how we can realize the value of such technology with the barrier to development being now lower than ever.