Sentiment analysis is the task of identifying the overall polarity of a piece of text and classifying it as Positive, Negative or Neutral. Relevance AI’s Sentiment analysis Template receives an input text and produces categorize it into one of the three sentiment classes.

This tool does not break the input text into its composing sentences. The assigned label is the overall sentiment. Therefore, if the input is composed of multiple sentences, some positive (e.g. She is truly excited) and some negative (e.g. but she was not successful last time) the overall sentiment is likely to be Neutral.

Sentiment analysis

How to use the Tool

Locate the Tool in the template page and click on Use template. You can use the Tool as is or clone it.

Tool inputs and output

The Tool requires one main input: Text to analyze Provide the main input and hit Run once, you will see the LLM response in a few seconds as shown in the image above.

The output is the overall sentiment label (Positive, Negative or Neutral).

Tool execution

Tools and templates can be

Tool components

If you clone a template, or make a Tool from scratch, you will have access to the Build tab. Build is where one put together different components to build a Tool suitable for their needs.

User inputs

User inputs

Long text input: An input text component suitable for long text pieces (more than one line), such as answers to a question, reviews, a text to summarize.

This component is to provide the input text and is of type long tex inputs.

Tool steps

There is only 1 LLM component under the Tool steps in this analysis flow.

Large Language Model (LLM)

LLM

A large language model component is all set up to provide you access to GPT (and many other LLMs). In the prompt section, you will provide the required information as well as instructions to what is expected to be done.

A Good Prompt

  1. Be short and precise with your instruction/request from the LLM
  2. Explicitly note constraints and goals
  3. Include a few examples when possible

Modify the Tool: Extracting the reason behind the given sentiment

The existing Tool conducts sentiment analysis using large language models. You might want to modify the Tool to receive other analysis results. This can be easily achieved by cloning the existing tool and customizing it in your account.

In this tutorial, we’re going to enhance the existing sentiment analyzer by configuring multiple outputs to our Tool and adding them back to our original spreadsheet as two new columns - we’re extracting the reason behind the given sentiment as well. You’ll learn how you can have a single LLM prompt produces multiple pieces of information.

Copy the existing Tool

Cloning the Tool creates a copy of the Tool in your account and enables you to modify the components. After cloning the Tool, select it from the Tools page and go to the Build tab.

Extracting Sentiment and Reason Simultaneously

We’re going to layer on multiple pieces of functionality within the same Tool to give us the desired output.

Modify the LLM Prompt

To obtain the explanation for the sentiment score, we could either introduce a separate LLM prompt or simply modify our existing prompt and then transform the output into a JSON. We’ll proceed with the latter.

To accomplish this, we adjust our prompt:

MESSAGE:

"""
{{message}}
"""

Based on the above message, respond with a JSON object of the format "{ sentiment, reason }". 
The sentiment of the message as one of "Neutral", "Positive" or "Negative" and the reason as to why that sentiment is assigned. 
Only respond with valid JSON.

Modify the LLM System Prompt

You are an expert sentiment classifier. Only ever respond with JSON in the format "{ sentiment, reason }". Do not say anything else.

After we’ve adjusted our prompt, we can proceed to try the Tool on one data sample (i.e. simply copy one of the messages from our dataset and then hit run). We can now observe that this has provided us with a JSON-like output that includes a reason and the sentiment.

JSON-like output.svg

Note that LLM outputs are of type string (text). The example above, is a string that is JSON convertible.

Configure the LLM Output

We aim to add a ‘sentiment’ column and a ‘reason’ column to our original dataset. To do this, we need to adjust our output. We must properly convert our string to JSON. This can be accomplished by adding the ‘Convert string to JSON’ step to our Tool.

Add a ‘Convert string to JSON’ component to your Tool. Next, specify the input to this component (i.e the output of the LLM component). For an LLM component that is called LLM the output is accessible via {{llm.answer}}.

convert string to JSON.svg

Configure the Tool Output

The goal is to reference the JSON output for ‘sentiment’, as well as the ‘reason’ instead of having the sentiment label as the only output. This can be achieved by clicking on Configure on the output component (i.e. last component of the page) and add the output fields.

Configure output.svg

Save all the changes and you are ready for running your Tool and exporting the results.