Photo by Serge Kutuzov on Unsplash

Using GPT-3 To Measure Sentiment

And How To Add User Sentiment Classification To A Chatbot

Cobus Greyling
5 min readJun 10, 2021

--

Introduction

Sentiment detection can generally be divided into three three indicators; Positive, Neutral or Negative. Sentiment can also be performed real-time or post processed.

A scenario that might use post processing is where social media posts are monitored, or other customer conversations. A conversational AI (chatbot) interface can also be post processed, but real-time monitoring is the gold standard.

The simple GPT-3 code configuration. Training data is used to describe the solution and perform training. The red block is populated after the code is executed.

Realtime or post processing of sentiment chat conversations between an agent and a customer is also a good use-case for sentiment.

Agents with regular negative sentiment attached to their conversations can be identified and assisted.

Chatbot conversations with low sentiment can be barged-in by an agent to rescue the conversation.

Sentiment classification is available via OpenAI’s GPT-3 API and in true GPT-3 fashion, the implementation code and training data are combined in a very simplistic fashion.

The approach of GPT-3 in general seems to be:

  • A low-code approach
  • A NLU or NLP solution is defined by describing it in natural language.
  • Training data is minimal.
  • All functionality can be prototyped in a matter of minutes via the Playground.

Sentiment In GPT-3

To illustrate how the sentiment classifier can be implemented, and how the return payload looks, let’s implement the Python code in a Notebook.

Below a the Colab Jupyter Notebook showing the 14 lines of code to test classification.

Running the playground example in Python via a Jupyter Notebook.

The code block starts with installing OpenAI, the OpenAI API Key. The prompt is defined with description and the example data.

pip install openai
import os
import openai
openai.api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
response = openai.Completion.create(
engine="davinci",
prompt="This is a Sentence sentiment classifier\n\n\nSentence: \"I loved the new Batman movie!\"\nSentiment: Positive\n###\nSentence: \"I hate it when my phone battery dies.\"\nSentiment: Negative\n###\nSentence: \"My day has been 👍\"\nSentiment: Positive\n###\nSentence: \"This is the link to the article\"\nSentiment: Neutral\n###\nSentence: \"This new music video blew my mind\"\nSentiment:",temperature=0.3,
max_tokens=60,
top_p=1.0,
frequency_penalty=0.5,
presence_penalty=0.0,
stop=["###"])
print (response)

The response below shows the JSON format with the response classification of “Positive”.

{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"text": " Positive\n"
}
],
"created": 1623252934,
"id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"model": "davinci:2020-05-03",
"object": "text_completion"
}

There is a common thread running through all GPT-3 API calls and functionality. The simplicity, the similar composition of the requests, and responses.

It takes a very astute approach to surface such complexity with this simplicity.

The Good & The Not So Good

Good

  • GPT-3 has a wide range of services and functionality available which can serve to augment any current chatbots.
  • Dialog can be diversified with the NLG capability. The NLG capability is absolutely natural and flowing.
  • General chit-chat can easily be created.
  • Chit-chat and Q&A can be multi-turn elongated dialogs with conversational context maintained.
  • Copywriting is made easy for slogans, headlines, reviews etc. Scripts, summarizations, key words etc. can be created automatically.
  • Text transformation
  • Text generation
  • Creating a general purpose bot to chat to without any dialog management of development.
  • With their underlying processing power and data, creating flexible Machine Learning stories should be a good fit in the near future

Not-so Good

  • The API is cloud hosted
  • Cost could be an impediment
  • Social media bot content generation
  • Not a framework for sustainable chatbot scaling; yet.
  • Possible over and under steering with training data.
  • Fine tuning, forms, policies & dialog defilement are not currently possible.

Conclusion

GPT-3 has the ability to bring ML and AI to the masses, where users define their API requirement in human speech and submitting the request. The Natural Language Generation (NLG) is exceptional. With truly natural, logical, cohesive sentences and expressions.

Conversational context is maintained; with no fallback proliferation or out of domain messages.

For social media copywriting GPT-3 is a perfect match in its current state. For a corporate domain specific conversational agent, there are a few elements which will have to be added.

https://www.linkedin.com/in/cobusgreyling
https://www.linkedin.com/in/cobusgreyling

--

--

Cobus Greyling

I explore and write about all things at the intersection of AI & language; LLMs/NLP/NLU, Chat/Voicebots, CCAI. www.cobusgreyling.com