How to use OpenAI's GPT-3 for sentiment analysis

Learn how to use Open AI and GPT-3 inside the spreadsheet to classify sentiment in social media comments, summarize customer reviews and more.

About OpenAI and GPT

OpenAI developed GPT, a type of artificial intelligence model that is capable of generating human-like text. It uses a machine learning technique called pre-training to learn the statistical patterns of a large dataset and then fine-tunes its knowledge on a smaller dataset to perform a specific task. It is trained on a dataset with billions of elements, including a significant part of the internet web pages, books, and more.

GPT can be used for a variety of language-based tasks, such as language translation, summarization, question answering, and text generation. It can generate coherent and coherently structured paragraphs, articles, and even entire books, given a prompt or topic. GPT can learn and adapt to new tasks quickly, making it a powerful tool for natural language processing.

NOTE: There are known limitations to these models and the output is not guaranteed to be factually accurate.

What is Sentiment analysis

Sentiment analysis is the process of analyzing text data to determine the sentiment or opinion expressed in it. It is a type of natural language processing (NLP) that uses machine learning algorithms to identify and extract subjective information from text. Sentiment analysis can be used to identify customer sentiment towards products, services, or brands, as well as to detect sentiment in social media posts and other online conversations.

How to use OpenAI in Rows

OpenAI template

There's a near-infinite amount of tasks you can solve using OpenAI. Follow this guide on how to set up the integration and use this template showcase to get started with 10 pre-built examples, follow along the list, or watch our video tutorial.

Using the ASK_OPENAI function

The OpenAI integration contains one function - ASK_OPENAI - that you can use to perform hundreds of tasks

You can use the ASK_OPENAI function via the Autocomplete in the editor, or via the Actions wizard.

ASK_OPENAI on the editor

The function has 4 parameters: 1 mandatory and 3 optional, with the following signature: ASK_OPENAI(prompt,[temperature], [max_tokens],[model]) prompt. Let's go one by one.

Prompt

The prompt is the instruction to give to the model. This is where you'll enter the "ask" you want the AI to answer. You can use the prompt to solve a task by explicitly writing it in prose or you can use spreadsheet functions to concatenate values in different cells to construct the prompt. Examples:

  • Use =ASK_OPENAI("Population of France is, in millions, :") if you are not using any cell references inside the prompt.
  • Use =ASK_OPENAI(CONCATENATE("The population of ",A2," in millions, is: ")) if you're using A2 as an input (the country name) to the prompt.

Prompt creation with Concatenate

Temperature (optional)

The temperature is the sampling temperature to use, varying between 0 and 1. Use 1 for creative applications, and 0 for well-defined answers.

If you're doing tasks that require a factual answer (e.g. country populations, capitalize text), then 0 (the default) is a better fit. If you're using the AI for tasks where there aren't definite answers - such as generating text, summarizing text, or translating - then experiment with a higher temperature

Max_tokens (optional)

This max_tokens represents the maximum number of tokens to generate in the completion. You can think of tokens as pieces of words. Here are a few helpful rules of thumb examples from the Open AI Help center:

  • 1 token ~= 4 chars in English
  • 1 token ~= 3/4 words

You can use any number starting with 0. The default value is 200. Most models have a context length of 2048 tokens, except for the newest models which support a maximum of 4096. For tasks that require more text output - text generation/summarization/translation - pick a higher value (e.g. 250).

Model (optional)

The AI model to use to generate the answer. By default, it uses "text-davinci-003". You can see here a list of all of the available GPT-3 models.

Examples of ASK_OPENAI for sentiment analysis

There are several ways to use OpenAI for sentiment analysis:

  • Classify sentiment in social media comments: Determine the overall sentiment of a piece of text, as well as identify specific opinions and emotions within the text.
  • Rate sentiment from customer feedback: Get a numerical rating of the customer sentiment on product reviews, social media comments, or feedback survey responses.
  • Tag customer reviews with sentiment: Automatically tag customer reviews into tags that summarize the reviewer's feedback on the product.
  • Generate emojis from sentiment: Turn text into emojis that represent the sentiment in a piece of text

Classify sentiment in social media comments

Goal: Analyze comments from social media accounts and extract the sentiment from the text.

Example:

1=ASK_OPENAI(CONCATENATE("Classify the sentiment of this post between 'Positive', 'Neutral' or 'Negative'. ,",A2," :"))

Details:

💡 The use of the CONCATENATE function in the prompt creates the desired structure for the response:

  1. Starts with the sentiment types to be classified. You can add others to your implementation.
  2. It adds the social comment to be analyzed (A2).
  3. It ends with : to prompt the AI to respond solely with the sentiment extracted from the text.

Sentiment analysis GIF

Rate sentiment from customer feedback

Goal: Get a numerical rating of the customer sentiment on product reviews, social media comments of feedback survey responses.

Example:

1=ASK_OPENAI(CONCATENATE("Classify the sentiment of this post from 1 to 5, with 1 being very negative and 5 very positive. ,",A2," :"))

Details:

Assumes that A2 contains the piece of feedback from the customer.

Rate feedback

Tag customer reviews with sentiment

Goal: Automatically tag customer reviews into tags that summarize the reviewers' feedback on the product.

Example:

1=ASK_OPENAI(concatenate("Classify the following text into tags that express the sentiment of the writer. Max 3 tags. Separate the tags with a ; . Text: ",A2))

Details:

Assumes that A2 contains the customer review.

Sentiment in reviews

Generate emojis from sentiment

Goal: Turn text into emojis that represent the sentiment in a piece of text

Example:

1=ASK_OPENAI(CONCATENATE("Return the emoji that represents being ",A2))

Details:

Assumes that A2 contains the emotion.

Emoji