With AI constantly on the rise in terms of the way you can use it within your own application or business, it is essential to cover how it can be used when creating real-time systems. Many use cases fall under this category, whether you're making an in-app chat, IoT, or live events solution; there is almost always a way AI can be applied to your solution to make it that much better.
This blog will teach you how to monitor your live chat for “positive” or “negative” behaviors using sentiment analysis. This application has a couple of use cases, such as monitoring a support chat for an inappropriate customer or, on the other hand, monitoring a live events chat to see if your customers are enjoying the event they are watching. In any case, many applications can be considered using AI, especially when creating real-time systems.
This is where PubNub fits in. PubNub provides a highly available, real-time communication platform. You can use it to build chat, IoT device control systems, geolocation apps, and many other communication systems. To learn more about what PubNub is capable of visit our docs or pricing page to get started.
What are PubNub Integrations?
First, for those unaware of PubNub Functions, PubNub Functions allow you to capture events happening within your PubNub instance. This enables you to write code to leverage existing integrations or transform, re-route, augment, filter, and even aggregate your data. So what exactly does that mean? Essentially, suppose you are sending a message, signal, file, etc. (data) across PubNub. In that case, you are able to write code either before it is received by the client or after to block the data being sent across, modify the data or even route the data to another third-party system.
PubNub Integrations is an extension of PubNub Functions that allows you to quickly set up a PubNub Function and integrate it with some third-party services such as AWS, Giphy, Google Cloud, etc. It is essentially pre-written code to connect your real-time system to any third-party systems in our Functions catalog. This catalog is found on the PubNub portal or the “Home” tab.
Refer to the image below and click “Discover & Integrate” to browse PubNub’s pre-made Functions.
Getting Started with Hugging Face
At this point, probably everyone has heard about OpenAI, GPT-4, Claude or any of the popular Large Language Models (LLMs). However, using these LLMs in a production environment can be expensive or nondeterministic regarding its results. I guess that is the downside of being good at everything; you could be better at performing one specific task. This is where Hugging Face can utilized.
Hugging Face provides open-source AI and machine learning models that can easily be deployed on Hugging Face itself or third-party systems such as Amazon SageMaker or Azure ML. You can interface with these deployments through an API and control the scaling of these models, which makes them perfectly suited for production environments. These models range in size but are generally small AI models that are good at doing one specific task. With capabilities to fine-tune these models, or use the pre-trained model for specific tasks, embedding them into various applications becomes more efficient, enhancing automation and performance. Combining these models can create new and intricate AI applications. In this case, by utilizing Hugging Face models, you wouldn’t have to depend on a production application on a third-party provider such as OpenAI or Google, ensuring a more targeted and customizable approach to deploying deep learning solutions in your operations.
Refer to the screenshot below to see how many different AI models Hugging Face provides.
What is Sentiment Analysis
Sentiment analysis (also referred to as opinion mining or emotion AI) is the use of natural language processing and text analysis to determine if the emotion or tone of the message is either positive, negative or neutral. Today, companies have huge volumes of text-based data such as emails, customer support messages, social media comments, reviews, etc. Sentiment analysis can scan these texts to determine the author's attitude towards a topic. Companies can then use this data to increase customer service or brand reputation.
Sentiment Analysis Use Cases
Moderation: You can use sentiment analysis to monitor your Live Events, Support, or In-App Chat to see if any customers are misbehaving and quickly ban and delete their messages. This would allow for a healthy environment on your platform.
Data Analytics: Companies sometimes want to scan through thousands of emails or texts to see how customers respond to the event or company brand. They can use this data to target what they are doing right or wrong depending on their actions and make better decisions on what to do next.
Implementing Sentiment Analysis in a PubNub Function
Using the Hugging Face PubNub Integration, let’s implement Sentiment Analysis right into PubNub Functions. First, head to the PubNub portal and open the Function Catalog on the Home page. Once in the Functions Catalog, click on the “Hugging Face Serverless API.”
After selecting the PubNub integration, follow the steps by choosing the keyset and app for which you want to configure your PubNub Function. After you get to the variables step, we will need to grab your API key from Hugging Face. Head over to your profile settings within Hugging Face and select Access Tokens, as we will need to add this to your PubNub Functions secrets. Then hit create, and we will officially integrate your PubNub instance with Hugging Face. It’s that easy.
You should now see a PubNub Function with the following code sample.
We will focus on the first model used within this PubNub Function, which is “distilbert-base-uncased-finetuned-sst-2-english”. To learn more about this function, navigate to distilbert-base-uncased-finetuned-sst-2-english or the model it is based on, which is distilbert/distilbert-base-uncased. You can also check out the model on github. This model will do all the heavy lifting for us and determine the sentiment analysis of a PubNub message when sent through the network.
I refactored my current function to look like this:
This code is now calling the Sentiment Analysis Hugging Face model and attaches it to the message payload under score
. Through testing, if I upload a message such as:
We will get a negative score of 0.9997703433036804
, which is a high negative score, and that message can be flagged. It is important to note that the range of this models output is from 0 - 1
, 0 being a very low score and 1 being the highest. On the other hand, if we send a message such as:
We will receive a negative score of 0.00014850986190140247
, which is a really low negative score, meaning the message has a positive tone.
Get Started with PubNub
With only a few steps, we have implemented sentiment analysis into the PubNub network utilizing PubNub integrations. If you are interested in more you can do with AI and PubNub, check out some of our other blogs, such as:
Build a Chatbot with PubNub and ChatGPT / OpenAI: We will walk you through setting up your own Chatbot using an OpenAI integration and PubNub.
Build an LLM Chatbot with a Custom Knowledge Base: Here, we go through how to build your own custom knowledgebase LLM Chatbot utilizing PubNub Functions. Meaning you can answer questions about your own company data.
A Developers Guide to Prompt Engineering and LLMs: Take a guide on prompt engineering and how to get the answers you want from these Large Language Models.
Geolocation Tutorial integrated with ChatGPT: In this tutorial, you’ll discover how to build a geolocation tracking app that allows users to share locations and send messages in real-time, as well as integrate with ChatGPT to provide information about your location.