Salesforce Einstein Sentiment


In this blog, I am going to explain how to analyze the text sentiment by using Salesforce Einstein Language sentimental services.With Salesforce  Einstein Natural Language Sentiment analysis API you can able to extract meaning out of an unstructured text. A sentiment analysis model is used to analyze a text string and classify it with one of the labels that you provide from the model; for example, you could analyze a tweet to determine whether it is positive or negative or analyze an email to determine whether it is happy, frustrated, or sad. Behind sentiment analysis involves natural language processing to process the models to classify the intent of text or to classify text as positive, negative, and neutral or based on what you labeled. In this blog, we are going to use our own custom data model to analyze the text along with the labels like worry, happiness, sadness, love and etc from the file.You can look into the file from here  . The final output looks below. You can enter the text for the prediction from the input box and you can see  Polar area chart with predictions details.


Salesforce Einstein Platform Services APIs user OAuth token for authenticating. To generate a token, you create a JWT payload, sign the payload with your private key, and then call the API to get the token. So you need to get your own einstein_platform private key. please follow this steps to get your einstein_platform key.

  1. Go to the sign up page.
  2. Click Sign Up Using Salesforce.
  3. On the Salesforce login page, type your username and password, and click Log In.
  4. Click Allow so the page can access basic information, such as your email address, and perform requests.
  5. Download Key to save the key locally. The key file is einstein_platform.pem
  6. Upload the einstein_platform.pem key to Salesforce files.
  7. Copy JWTBearerFlow and JWT Apex Class from this git repository

Refer this link for more information on the above steps.

Create custom metadata type or custom setting whatever works for you here to store the service endpoint, token endpoint and account details. Below is the custom metadata type which we are going to use it in this blog post.

Let’s Get Started.
As we are going to user cURL for model upload and training, we need to Generate a token for authentication. Go to this link and generate the token as shown below. We are going to use this generated token for model upload and train the model for predictions.

Step 1: Collect data.

You must train your sentiment model against examples of the type of data that you are going to see when you use your model. For example, if you are trying to determine the sentiment of text and its labels. You can look into the file from here . which contains certain data set with the text and its label for prediction. Once you have collected your training samples, you will need to pre-classify each sample with a label. A label is a string that you think best describes that example, for example: “happy”, “sad”, “on the fence”.

Step 2: upload your dataset.

After creating a data set and labels you need to upload to upload the Einstein services API  with the below command

Replace token with a generated authentication token. And type  “text-sentiment” of HTTP header parameters indicated this prediction is for the sentiment analysis.

The API call is asynchronous, so you receive a dataset ID back immediately .use the dataset Id to track the status

Step 2: Get a Dataset status

Use the below command to get the data set status. Once you get the dataset status is “SUCCEEDED”, you can use that dataset for training.

 Step 3: train your model

 Once your dataset is upload successfully, you can able to train your data set for prediction. use the following commands to train your model.


After successfully training the model. you will get model Id reference which we will use to predict the sentiment.  You need to pass this model id along with a request to predict the result.

You can use below command to track the progress of your training. When training completes successfully, you can able to use the predictions.

Step 4: Prediction for sentiment

you can use below command to predict the sentiment from the cURL.

Now add the custom metadata with alone with details. Custom metadata developer name is same as model Id.

Step 5: Static Resource and Remote Site Setting.

Here I am using chart.js, so upload the chart.js files to the static resource as shown below.

Add remote site setting as shown below.

Here is the below apex class that use to send the callouts to Einseet Service API.

Here is the lightning component



You can able to predict the sentiment from the component as shown below.