Implementing a toxicity detector in your chatbots

Tags: chatbots, toxicity, xatkit, opensource
Published: 2021-03-25 11:16:16

The <a href="https://xatkit.com/" target="_blank" rel="noopener">Xatkit team</a> worries about good behavior in chatbots. Respect is fundamental in Internet communication. This also applies to chatbots. We don't want you to offend our bots. And even more important, we don't want our clients to waste resources in processing toxic comments from trolls and other undesirable visitors.

Today we explain how we have made our chatbots safer by integrating a <strong>toxicity detector for the messages the chatbots receive.</strong>
<h2>What is toxicity?</h2>
The formal definition of toxicity is "very harmful or unpleasant in a pervasive or insidious way".  This kind of language can be offensive to a lot of people or specific collectives and must be avoided as much as possible, to guarantee a <a href="https://livablesoftware.com/bots-against-harassment-in-open-source/" target="_blank" rel="noopener">healthy community</a> and no hate.
<h2>How can it be detected?</h2>
As in most of Natural Language Processing, a toxicity detector is implemented as a language model that, given an input (e.g. the message we send to a chatbot) it produces an output (in this case the probability of the message being toxic). Most models can go further than detecting whether a message is toxic or not and are able also to classify the type of toxicity the message contains, such as "insult", "threat", "obscenity", etc. This can be useful, for instance, when a web page admin has to analyze which are the main causes of toxicity, to ban disrespectful users or to censor inappropriate language.
<h2>Two ways to add a toxicity detector for your chatbots with Xatkit</h2>
Xatkit is a <a href="https://xatkit.com/chatbot-orchestration-platform-open-source/" target="_blank" rel="noopener">chatbot orchestration platform</a> where we always try to bring you the best NLP solutions out there and enable them for easy use in your own bots. For toxicity detection, we have added a <a href="https://github.com/xatkit-bot-platform/xatkit/wiki/Processors" target="_blank" rel="noopener">new language processor </a>for toxicity analysis. Even more, we provide two different implementations of this toxic language detector. You're free to choose the one that best fits your needs.
<h3>PerspectiveAPI</h3>
"Perspective API is the product of a collaborative research effort by <a href="https://jigsaw.google.com/" target="_blank" rel="noopener noreferrer">Jigsaw</a> and Google’s Counter Abuse Technology team. We open-source experiments, tools, and research data that explore ways to combat online toxicity and harassment." <em>(extracted from <a href="https://www.perspectiveapi.com/">Pespective API web page</a>)</em>

This tool provides the capability of detecting a lot of different toxicity labels, some of them in different languages. When using this implementation, Xatkit will send a request to the API for every user utterance and receive a score between 0 (0% toxic) and 1 (100% toxic) for each toxic label we want. These scores are available to the chatbot designer to decide what to do with the message based on its toxicity level.
<h3>Detoxify</h3>
"At <a class="cs is" href="https://www.unitary.ai/" rel="noopener nofollow">Unitary</a> we are working to stop harmful content online by interpreting visual content in context."<em> (by Laura Hanu from Unitary AI, <a href="https://medium.com/unitary/how-well-can-we-detoxify-comments-online-bfffe5f716d7">full article here</a>)</em>

<a href="https://github.com/unitaryai/detoxify#-detoxify">Detoxify</a> is the result of three <a href="https://www.kaggle.com/" target="_blank" rel="noopener">Kaggle</a> competitions proposed to improve toxicity classifiers. Each had a different purpose within the toxicity classifiers context.
<ul>
 	<li><a href="https://www.kaggle.com/c/jigsaw-toxic-comment-classification-challenge">Toxicity comment classification challenge</a>: The first competition aimed to build a generic toxicity classification model that contemplates different kinds of toxicity (insult, threat, sexuality...)</li>
 	<li><a href="https://www.kaggle.com/c/jigsaw-unintended-bias-in-toxicity-classification">Unintended Bias in Toxicity Classification</a>: It is a fact that there are some words that cause confusion since they are often used to harm some collectives (e.g. homosexual, women or race-related words). When these kinds of words are used in a healthy context they can also be considered toxic by biased language models. The 2nd version of the original competition wanted to improve the unintended bias when classifying toxic messages.</li>
 	<li><a href="https://www.kaggle.com/c/jigsaw-multilingual-toxic-comment-classification">Multilingual Toxic Comment Classification</a>: The last competition aimed to classify toxicity in a wide range of languages. The 2 previous worked only in English, so this time the objective was to achieve good results with other languages.</li>
</ul>
In Xatkit, you can rely on these models for your toxic language detection. Same as before, the toxicity scores are available to the chatbot designer to react and kick out trolls from your conversations. You can check our prototype implementation of a <a href="https://github.com/xatkit-bot-platform/labs-detoxify-server" target="_blank" rel="noopener">REST API that wraps and exposes the Detoxify models</a> to Xatkit.
<h2>Example chatbot</h2>
We have built an example bot that detects toxicity in messages, which can be found in our <a href="https://github.com/xatkit-bot-platform/xatkit-examples/tree/master/LanguageProcessorsBots">LanguageProcessorsBots directory</a>. Feel free to play with it!

<img class="wp-image-119547 size-full" src="https://xatkit.com/wp-content/uploads/2021/03/toxic-bot.gif" alt="Example bot" width="367" height="514" /> Example usage of the ToxicityPostProcessor in a chatbot

In this code excerpt, you can see a simple definition of a state of a bot. We want to get the toxicity value the Perspective API has computed, and based on its value we choose the chatbot response. Here we assume a threshold of 0.7. This means that every message with a score greater than 0.7 will be considered toxic. Note that we also check if the score has been set properly with the<em> !toxicity.equals(PerspectiveApiScore.DEFAULT_SCORE)</em> boolean expression. Every score will be equal to "default expression" when it has not been set properly. It is like a security value to not break the chatbot when accessing the scores.

{% gist https://gist.github.com/mgv99/69952e197b959e8fd509b6f387eb23c9 %}