Sentiment Analysis with Data Augmentation Using ChatGPT

Sentiment analysis, a subfield of Natural Language Processing (NLP), aims to discern and classify the underlying sentiment or emotion expressed in textual data. Whether it is understanding customers' opinions about a product, analyzing social media posts, or gauging public sentiment towards a political event, sentiment analysis plays a vital role in unlocking valuable insights from vast amounts of textual data.

However, training an accurate sentiment classification model often demands a substantial volume of annotated data, which may not always be readily available or time-consuming to acquire. This limitation has led researchers and practitioners to explore innovative techniques, such as data augmentation, to generate synthetic data and augment the training set.

In this article, we will delve into the world of data augmentation, specifically using ChatGPT, a powerful language model developed by OpenAI, to generate additional training samples and bolster the performance of sentiment classification models. By leveraging the capabilities of ChatGPT, we can efficiently create diverse and realistic data, opening new possibilities for sentiment analysis in scenarios where limited annotated data would otherwise be an obstacle.

Sentiment Classification without Data Augmentation

To train the sentiment classification model, we will use the IMDD dataset, which contains movie reviews labeled with sentiments. We'll then train a Random Forest model using TF-IDF (Term Frequency-Inverse Document Frequency) features, which allow us to represent the text data numerically. By dividing the dataset into training and testing sets, we can evaluate the model's performance on unseen data. The accuracy score will be used to measure how well the model predicts sentiments.

Now, let's proceed with the code:

import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

dataset = pd.read_csv(r"D:\Datasets\IMDB Dataset.csv")
dataset.head()

dataset = dataset.head(600)
X_train, X_test, y_train, y_test = train_test_split(dataset['review'], 
                                                    dataset['sentiment'], 
                                                    test_size=0.2, 
                                                    random_state=42)

# Create TF-IDF vectorizer
vectorizer = TfidfVectorizer()
X_train_tfidf = vectorizer.fit_transform(X_train)

# Train the Random Forest model
rf_model = RandomForestClassifier(n_estimators  = 500)
rf_model.fit(X_train_tfidf, y_train)

# Transform the test data using the same vectorizer
X_test_tfidf = vectorizer.transform(X_test)

# Predict the sentiment on the test data
y_pred = rf_model.predict(X_test_tfidf)

accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)

Without data augmentation, I achieved an accuracy of 0.6916.

Data Augmentation with ChatGPT

Lets now augment our data using ChatGPT. We will generate 100 more reviews. So, lets begin without an ado.

import os
import openai

api_key = os.getenv('OPENAI_KEY2')
openai.api_key = api_key

In the above code snippet, we are setting up the OpenAI API for accessing their language model, specifically the "GPT-3.5" model. This is the language model used by ChatGPT behind the scenes.

First, we import the required libraries. Then, we set the OpenAI API key by retrieving it from the environment variable OPENAI_KEY2. This key is necessary for making API calls to OpenAI services.

Next, we configure the openai library with the obtained API key by assigning it to openai.api_key.

The next step is to define a function that generates movie reviews.

import time

def generate_reviews(review):

    content = """Generate one short IMDB movie review using the following review as an example. The review sentiment should be positive, negative, or neutral.
    Review = {}. Also mention the sentiment of the review in one word (positive, negative, neutral) in the beginning of your reply.""".format(review)


    generated_review = openai.ChatCompletion.create(
      model="gpt-3.5-turbo",
      temperature = 0.7,
      messages=[
            {"role": "user", "content": content}
        ]
    )

    return generated_review["choices"][0]["message"]["content"]

The code above defines a function called generate_reviews(review) that uses the OpenAI GPT-3.5 Turbo language model to generate a movie review based on an example review. The function takes the input review as the example and prompts the model to create a new movie review with a specified sentiment (positive, negative, or neutral). The temperature parameter controls the creativity of the generated text.

The function then returns the generated movie review. This approach allows us to easily generate diverse movie reviews with different sentiments, leveraging the power of the GPT-3.5 Turbo language model from OpenAI.

Next, we will iterate through the first 100 movie reviews in our training set and use them as examples to generate new reviews.

The following code contains a loop that generates 100 movie reviews using the generate_reviews(review) function. The generated reviews are stored in the generated_reviews list. Each review is based on a different example from the training data (X_train). This approach allows to create diverse and creative movie reviews.

generated_reviews = []

X_train_list = X_train.tolist()

for i in range(100):

    review = X_train_list[i]
    generated_review = generate_reviews(review)
    generated_reviews.append(generated_review)
    print(i + 1, generated_review)
Training with Augmented Data

We will now train our machine learning model using original plus augmented data.

Lets first convert ChatGPT to generated reviews into Pandas dataframe containing review, and sentiment columns. The following script iterates through each generated review, splits the review into sentiment and review, and returns these values to the calling function. Text ad sentiments for all the generated reviews are stored in a dictionary which is then appended to a list, and converted to a Pandas dataframe.

import re
def split_string_into_two(input_string):

    # Split the string into words
    words = input_string.split()

    # Create the first string with the first word
    first_string = words[0].lower()
    first_string = re.sub(r'[^a-zA-Z0-9]', '', first_string)

    # Create the second string with the second word
    second_string = ' '.join(words[1:]) if len(words) > 1 else ''

    # Remove new line characters
    second_string = second_string.replace('\n', ' ')

    return first_string, second_string

generated_reviews_dicts = []
for review in generated_reviews:
    first, second = split_string_into_two(review)
    review_dict = {'sentiment':first, 'review':second}
    generated_reviews_dicts.append(review_dict)

df = pd.DataFrame(generated_reviews_dicts)
df.sentiment.value_counts()

The script generated a total of 99 movie reviews. Among them, 48 reviews were predicted to have a positive sentiment, 46 had a negative sentiment, and 5 were labeled as neutral. However, one review had the text "review" as the predicted sentiment, which seemed incorrect. Consequently, I removed that particular record from the results, retaining only reviews with sentiments classified as positive, negative, or neutral.

Next, I will add the generated reviews to the reviews in the original training set:

X_train_aug = df["review"]
X_train_new = X_train.append(X_train_aug)

y_train_aug = df["sentiment"]
y_train_new = y_train.append(y_train_aug)

The rest of the process is the same, we will use TFIDF to convert text to vectors, use the random forest algorithm to train our model and make predictions on the test set.

# Create TF-IDF vectorizer
vectorizer = TfidfVectorizer()
X_train_tfidf = vectorizer.fit_transform(X_train_new)

# Train the Random Forest model
rf_model = RandomForestClassifier(n_estimators  = 500)
rf_model.fit(X_train_tfidf, y_train_new)

# Transform the test data using the same vectorizer
X_test_tfidf = vectorizer.transform(X_test)

# Predict the sentiment on the test data
y_pred = rf_model.predict(X_test_tfidf)

accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)

In the output, I achieved a classification accuracy of 0.75, which is roughly a 6% improvement on the original 0.6916. The results are very impressive with only 100 newly generated records. This shows the significant capabilities of ChatGPT for data augmentation.

I hope you would have enjoyed this tutorial. Feel free to share your thoughts on how I can further improve these results.

Text Classification Using Data Annotation with ChatGPT

Data annotation for text classification is time-consuming and expensive. In the case of smaller training datasets, pre-trained ChatGPT models might achieve higher classification accuracy on test sets than training classifiers from scratch or fine-tuning existing models. Additionally, ChatGPT can aid in annotating data for fine-tuning text classification models.

In this article, I demonstrate two experiments. First, I make predictions on text data using ChatGPT and compare the results with the test set. Next, I annotate text data using ChatGPT and utilize the annotated data to train a machine learning model. The findings reveal that directly predicting text labels using ChatGPT outperforms data annotation followed by model training. These experiments highlight the practical benefits of using ChatGPT in data annotation and text classification tasks.

Text Classification Using Base Machine Learning Model

To start, I will use a basic machine-learning model to classify text. This will give us a starting point to compare the results later. In the next part of the experiment, we will use ChatGPT to annotate the data and see how it performs compared to the baseline. This way, we can find out if ChatGPT helps improve the classification results.

We'll use the IMDB dataset with labeled movie reviews to train a text classification model. The dataset consists of positive and negative movie reviews. Employing a Random Forest model and TF-IDF features, we'll convert the text data into numerical representations. By splitting the dataset into training and testing sets, we can assess the model's performance using the accuracy score as a metric for sentiment prediction.

Here is the code that trains the text classification model for predicting IMDB movie review sentiments.

# Import necessary libraries
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# Load the IMDb dataset from a CSV file and display the first few rows
dataset = pd.read_csv(r"D:\Datasets\IMDB Dataset.csv")
dataset.head()

# retain first 300 rows from the dataset for experimentation
dataset = dataset.head(300)

# Split the dataset into training (80%) and testing (20%) sets
X_train, X_test, y_train, y_test = train_test_split(dataset['review'], 
                                                    dataset['sentiment'], 
                                                    test_size=0.2, 
                                                    random_state=42)

# Create TF-IDF vectorizer
vectorizer = TfidfVectorizer()
X_train_tfidf = vectorizer.fit_transform(X_train)

# Train the Random Forest model
rf_model = RandomForestClassifier(n_estimators  = 500)
rf_model.fit(X_train_tfidf, y_train)

# Transform the test data using the same vectorizer
X_test_tfidf = vectorizer.transform(X_test)

# Predict the sentiment on the test data
y_pred = rf_model.predict(X_test_tfidf)

accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)

Training a base random forest classifier model on 240 records returns an accuracy of 0.65% on the test set.

Text Classification with ChatGPT

Lets now use ChatGPT to make predictions directly on the test set and see what performance we achieve.

We will access the OpenAI API by retrieving the API key from the environment variable "OPENAI_KEY2" and assigning it to the openai.api_key variable.

import os
import openai

api_key = os.getenv('OPENAI_KEY2')
openai.api_key = api_key
print(api_key)

Next, we define a function called find_sentiment(review) that uses the OpenAI GPT-3.5 Turbo language model to determine the sentiment expressed in a given IMDB movie review. Behind the scenes, ChatGPT uses the GPT-3.5 Turbo language model.

import time

def find_sentiment(review):

    content = """What is the sentiment expressed in the following IMDB movie review? 
    Select sentiment value from positive or negative. Return only the sentiment value.
    Movie review: {}""".format(review)

    sentiment = openai.ChatCompletion.create(
      model="gpt-3.5-turbo",
      temperature = 0,
      messages=[
            {"role": "user", "content": content}
        ]
    )

    return sentiment["choices"][0]["message"]["content"]

The find_sentiment(review) function takes the review as input and prompts the model to provide the sentiment value, which should be either "positive" or "negative." By using the OpenAI ChatCompletion API, we interact with the language model to extract the sentiment value from the generated response, and the function returns the sentiment as the output.

Next, we iterate through all the reviews in the test set, pass them to the find_sentiment(review) method, and retrieve their sentiments.

all_sentiments = []

X_test_list = X_test.tolist()

i = 0
while i < len(X_test):

    try:
        review = X_test_list[i]
        sentiment_value = find_sentiment(review)
        all_sentiments.append(sentiment_value)
        i = i + 1
        print(i, sentiment_value)

    except:
        print("===================")
        print("time limit reached")

Finally, we compare the ChatGPT-predicted sentiments with the sentiments in the test set.

accuracy = accuracy_score(all_sentiments, y_test)
print("Accuracy:", accuracy)

I achieved an accuracy of 0.95% using ChatGPT predicted sentiments, which is 30% higher than the accuracy achieved via a base model. This is huge and shows how powerful ChatGPT can be for text classification tasks.

In the next section, I will explain how you can annotate data using ChatGPT and use it to train your text classification model.

Data Annotation with ChatGPT

The approach for data annotation remains similar to label prediction since essentially annotation involves assigning a label to a record. The following script annotates reviews in the training set with positive or negative sentiments.

all_sentiments = []

X_train_list = X_train.tolist()

i = 0
while i < len(X_train):

    try:
        review = X_train_list[i]
        sentiment_value = find_sentiment(review)
        all_sentiments.append(sentiment_value)
        i = i + 1
        print(i, sentiment_value)

    except:
        print("===================")
        print("time limit reached")

Next, we can use the records annotated by ChatGPT to train machine learning model for text classification, as shown in the following script:

# Train the Random Forest model
rf_model = RandomForestClassifier(n_estimators  = 500)
rf_model.fit(X_train_tfidf, all_sentiments)

# Transform the test data using the same vectorizer
X_test_tfidf = vectorizer.transform(X_test)

# Predict the sentiment on the test data
y_pred = rf_model.predict(X_test_tfidf)

accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)

In the output, I achieved an accuracy of 0.6833% which is 3% better than the accuracy achieved via manual annotations.

Conclusion

In conclusion, in the case of small datasets, ChatGPT often performs better than training a machine learning model from scratch. It is further observed that ChatGPT annotated data when used for training machine learning models often performs better than manually annotated data, particularly in the case of smaller datasets.

Text Classificatin Using Data Annotation with ChatGPT

Data annotation for text classification is time-consuming and expensive. In the case of smaller training datasets, pre-trained ChatGPT models might achieve higher classification accuracy on test sets than training classifiers from scratch or fine-tuning existing models. Additionally, ChatGPT can aid in annotating data for fine-tuning text classification models.

In this article, I demonstrate two experiments. First, I make predictions on text data using ChatGPT and compare the results with the test set. Next, I annotate text data using ChatGPT and utilize the annotated data to train a machine learning model. The findings reveal that directly predicting text labels using ChatGPT outperforms data annotation followed by model training. These experiments highlight the practical benefits of using ChatGPT in data annotation and text classification tasks.

Text Classification Using Base Machine Learning Model

To start, I will use a basic machine-learning model to classify text. This will give us a starting point to compare the results later. In the next part of the experiment, we will use ChatGPT to annotate the data and see how it performs compared to the baseline. This way, we can find out if ChatGPT helps improve the classification results.

We'll use the IMDB dataset with labeled movie reviews to train a text classification model. The dataset consists of positive and negative movie reviews. Employing a Random Forest model and TF-IDF features, we'll convert the text data into numerical representations. By splitting the dataset into training and testing sets, we can assess the model's performance using the accuracy score as a metric for sentiment prediction.

Here is the code that trains the text classification model for predicting IMDB movie review sentiments.

# Import necessary libraries
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# Load the IMDb dataset from a CSV file and display the first few rows
dataset = pd.read_csv(r"D:\Datasets\IMDB Dataset.csv")
dataset.head()

# retain first 300 rows from the dataset for experimentation
dataset = dataset.head(300)

# Split the dataset into training (80%) and testing (20%) sets
X_train, X_test, y_train, y_test = train_test_split(dataset['review'], 
                                                    dataset['sentiment'], 
                                                    test_size=0.2, 
                                                    random_state=42)

# Create TF-IDF vectorizer
vectorizer = TfidfVectorizer()
X_train_tfidf = vectorizer.fit_transform(X_train)

# Train the Random Forest model
rf_model = RandomForestClassifier(n_estimators  = 500)
rf_model.fit(X_train_tfidf, y_train)

# Transform the test data using the same vectorizer
X_test_tfidf = vectorizer.transform(X_test)

# Predict the sentiment on the test data
y_pred = rf_model.predict(X_test_tfidf)

accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)

Training a base random forest classifier model on 240 records returns an accuracy of 0.65% on the test set.

Text Classification with ChatGPT

Lets now use ChatGPT to make predictions directly on the test set and see what performance we achieve.

We will access the OpenAI API by retrieving the API key from the environment variable "OPENAI_KEY2" and assigning it to the openai.api_key variable.

import os
import openai

api_key = os.getenv('OPENAI_KEY2')
openai.api_key = api_key
print(api_key)

Next, we define a function called find_sentiment(review) that uses the OpenAI GPT-3.5 Turbo language model to determine the sentiment expressed in a given IMDB movie review. Behind the scenes, ChatGPT uses the GPT-3.5 Turbo language model.

import time

def find_sentiment(review):

    content = """What is the sentiment expressed in the following IMDB movie review? 
    Select sentiment value from positive or negative. Return only the sentiment value.
    Movie review: {}""".format(review)

    sentiment = openai.ChatCompletion.create(
      model="gpt-3.5-turbo",
      temperature = 0,
      messages=[
            {"role": "user", "content": content}
        ]
    )

    return sentiment["choices"][0]["message"]["content"]

The find_sentiment(review) function takes the review as input and prompts the model to provide the sentiment value, which should be either "positive" or "negative." By using the OpenAI ChatCompletion API, we interact with the language model to extract the sentiment value from the generated response, and the function returns the sentiment as the output.

Next, we iterate through all the reviews in the test set, pass them to the find_sentiment(review) method, and retrieve their sentiments.

all_sentiments = []

X_test_list = X_test.tolist()

i = 0
while i < len(X_test):

    try:
        review = X_test_list[i]
        sentiment_value = find_sentiment(review)
        all_sentiments.append(sentiment_value)
        i = i + 1
        print(i, sentiment_value)

    except:
        print("===================")
        print("time limit reached")

Finally, we compare the ChatGPT-predicted sentiments with the sentiments in the test set.

accuracy = accuracy_score(all_sentiments, y_test)
print("Accuracy:", accuracy)

I achieved an accuracy of 0.95% using ChatGPT predicted sentiments, which is 30% higher than the accuracy achieved via a base model. This is huge and shows how powerful ChatGPT can be for text classification tasks.

In the next section, I will explain how you can annotate data using ChatGPT and use it to train your text classification model.

Data Annotation with ChatGPT

The approach for data annotation remains similar to label prediction since essentially annotation involves assigning a label to a record. The following script annotates reviews in the training set with positive or negative sentiments.

all_sentiments = []

X_train_list = X_train.tolist()

i = 0
while i < len(X_train):

    try:
        review = X_train_list[i]
        sentiment_value = find_sentiment(review)
        all_sentiments.append(sentiment_value)
        i = i + 1
        print(i, sentiment_value)

    except:
        print("===================")
        print("time limit reached")

Next, we can use the records annotated by ChatGPT to train machine learning model for text classification, as shown in the following script:

# Train the Random Forest model
rf_model = RandomForestClassifier(n_estimators  = 500)
rf_model.fit(X_train_tfidf, all_sentiments)

# Transform the test data using the same vectorizer
X_test_tfidf = vectorizer.transform(X_test)

# Predict the sentiment on the test data
y_pred = rf_model.predict(X_test_tfidf)

accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)

In the output, I achieved an accuracy of 0.6833% which is 3% better than the accuracy achieved via manual annotations.

Conclusion

In conclusion, in the case of small datasets, ChatGPT often performs better than training a machine learning model from scratch. It is further observed that ChatGPT annotated data when used for training machine learning models often performs better than manually annotated data, particularly in the case of smaller datasets.

Using ChatGPT to Interact with Third-Party Applications in Python

Integrating language models like ChatGPT into third-party applications has become increasingly popular due to their ability to comprehend and generate human-like text. However, it's crucial to acknowledge the limitations of ChatGPT, such as its knowledge cut-off date in September 2021 and its inability to access external sources like Wikipedia or Python directly.

Recognizing this challenge, Harrison Chase, the co-founder, and CEO of LangChain, came up with an innovative solution. He developed the Python LangChain module, which empowers developers to integrate third-party applications with large language models seamlessly. This breakthrough opens up a world of possibilities, allowing developers to harness the power of language models while effectively processing information from external sources.

In this article, we will explore the fascinating concept of using ChatGPT to interact with third-party applications using the Python LangChain module. By the end, you will have a deeper understanding of how to leverage this integration and create even more sophisticated and efficient applications.

Importing ChatGPT from LangChain

The first step is to install the Python LangChain module which you can do with the following pip command.

pip install langchain

Next, you need to import the ChatOpenAI class from the langchain.chat_models module. The ChatOpenAI class allows you to create an instance of ChatGPT. To do so, pass gpt-3.5-turbo model to the model_name attribute of the ChatOpenAI class. The OpenAIs gpt-3.5turbo model powers ChatGPT. You also need to pass your OpenAI API key to the open_api_key attribute.

from langchain.chat_models import ChatOpenAI
import os

api_key = os.getenv('OPENAI_KEY2')
chatgpt = ChatOpenAI(model_name="gpt-3.5-turbo",
                    openai_api_key = api_key)

We are now ready to integrate third-party applications with ChatGPT in Python.

Using ChatGPT to Extract Information from Wikipedia

As earlier said, ChatGPTs knowledge cut-off date is September 2021 and it cannot answer queries beyond that period. For example, if you ask ChatGPT to return the summary of the Wikipedia article on the 2022 Wimbledon Championships, you will get the following answer:

image_1.PNG

LangChain agents allow you to interact with third-party applications. Check out the list of all LangChain agent integrations for more information.

Lets see how to integrate ChatGPT with third-party applications such as Wikipedia with the help of an example code.

You have to import the load_tools, initialize_agent, and AgentType entities from the langchain.agents module.

Next, you should provide the agent type as input to the load_tools class. In the example script below, the agent type specified is wikipedia. The subsequent step involves creating an agent object using the initialize_agent() method. When calling the initialize_agent() method, you will need to pass the tool type, the ChatGPT instance, and the agent type as arguments. If you set the verbose parameter to True, it will display the thought process involved in the agent's task execution.

In the following script, we ask the Wikipedia agent to return the summary of the Wikipedia article on the 2022 Wimbledon Championships.

In the output, you can see the agents though process and the final results that contain the summary of the article.

from langchain.agents import load_tools, initialize_agent, AgentType

tools = load_tools(
    ['wikipedia'], 
)

agent_chain = initialize_agent(
    tools,
    chatgpt,
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True,
)

agent_chain.run(
    "Return the summary of Wikipedia article on the 2022 Wimbledon Championships.",
)

image_2.PNG

Extracting Information from Arxiv

Lets see another example. We will retrieve the title and author names of an article from ArXiv which is a popular open-access repository for scientific research papers, preprints, and other scholarly articles.

The script remains the same except that you have to pass arxiv as the parameter value to the load_tools() method.

tools = load_tools(
    ["arxiv"], 
)

agent_chain = initialize_agent(
    tools,
    chatgpt,
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True,
)

agent_chain.run(
    "Give me the title and the author names of the paper 1301.3781",
)

image_3.PNG

Extracting Information from CSV

LangChain provides methods to directly create agent instances for particular tasks. For instance, the create_csv_agent() method from the langchain.agents module allows you to create a CSV agent that interacts with CSV files.

Lets see an example. The following script imports a dataset containing a companys employee attrition information.

import pandas as pd
dataset = pd.read_csv(r'D:\Datasets\employee_attrition_dataset.csv')
dataset.head()

image_4.PNG

Lets use a CSV agent to get information from this file. We will ask ChatGPT to return the total number of employees from the sales department.

In the output, you can see the process that ChatGPT adopted to return the output.

from langchain.agents import create_csv_agent

agent = create_csv_agent(
    chatgpt,
    r'D:\Datasets\employee_attrition_dataset.csv',
    verbose=True
)

agent.run("Return the total number of employees from the sales department.")

image_5.PNG

Extracting Information from Pandas DataFrame

Similarly, you can extract information from a Pandas dataframe using create_pandas_dataframe_agent() method. In the following script we ask ChatGPT to return the total number of employees from the sales department whose education field is medical.

import pandas as pd
dataset = pd.read_csv(r'D:\Datasets\employee_attrition_dataset.csv')

from langchain.agents import create_pandas_dataframe_agent

agent = create_pandas_dataframe_agent(
    chatgpt,
    dataset,
    verbose=True
)

agent.run("Return the total number of employees from the sales department whose education field is medical.")

image_6.PNG

And with that, I will end this tutorial. I hope you would have liked it. Feel free to share your opinion on this article. I will appreciate if you provide suggestions on how to use LangChain to integrate ChatGPT with other third party applications.

Infrastructure as Code: Exploring Terraform’s Dominance

Infrastructure as Code (IaC) has emerged as a pivotal practice in modern software development, enabling teams to manage infrastructure resources efficiently and consistently through code. This analysis provides an overview of Infrastructure as Code and its significance in cloud computing and DevOps. 

In recent years, Terraform has dominated the Infrastructure as Code domain, driven by its multi-cloud support, declarative syntax, robust resource providers, and active community and state management capabilities. Organizations are encouraged to leverage the strengths of Terraform while remaining aware of emerging IaC solutions tailored to their specific requirements and cloud preferences.

Emojis in Google Sheets

Emojis in Google Sheets

Emojis can be a fun and effective way to add visual interest to your Google Sheets formulas. There are so many different ways to add emojis in Google Sheets but my favorite option is the built-in CHAR function.

You can copy the hex code of any emoji from unicode.org and then use the HEX2DEC function to convert the hexadecimal value into its decimal equivalent. The CHAR function will take this decimal number as input and returns the corresponding emoji symbol.

// Add the 😀 emoji to the active cell
=CHAR(HEX2DEC("1F600"))

// Get the hex value of 😀 emoji
=DEC2HEX(UNICODE("😀"))

Well the purpose of this guide is not to explain how to add emojis in Google Sheets but the problems that emojis may cause in your production workflows related to Google Sheets.

The problem with Emojis in Google Sheets

If you are to convert any Google Sheet to a PDF file programmatically, Apps Script can help. However, if your Google Sheet contains any emoji symbols, the PDF conversion engine will fail with a 500 error. This issue arises due to a known bug (see issue tracker) at Google’s end and there has not been any resolution so far.

Google Sheets PDF 500 error

Replace Emojis in Google Sheets

Google Add-ons like Email Google Sheets and Document Studio internally use Google Drive’s own conversion engine to convert spreadsheets into PDF files. the input sheet contains any emoji symbol, the PDF conversion would always fail owning to the bug.

The only workaround to this problem is to check your spreadsheet file for any emoji symbols and remove them before performating the PDF conversion.


/* 
*  Replace Emoji Symbols in Google Spreadsheet 
*  Written by Amit Agarwal www.labnol.org
*/

const replaceEmojisInGoogleSheet = () => {
  SpreadsheetApp.getActiveSpreadsheet()
    .getSheets()
    .filter((sheet) => sheet.getType() === SpreadsheetApp.SheetType.GRID)
    .filter((sheet) => sheet.isSheetHidden() === false)
    .forEach((sheet) => {
      sheet
        .getDataRange()
        .getValues()
        .forEach((row, rowIndex) => {
          row.forEach((cell, colIndex) => {
            if (typeof cell === "string" && /\p{Emoji_Presentation}/u.test(cell)) {
              sheet.getRange(rowIndex + 1, colIndex + 1)
                   .setValue(cell.replace(/\p{Emoji_Presentation}/gu, " ").trim());
            }
          });
        });
    });

  SpreadsheetApp.flush();
};

The Google Script will now scan your entire sheet, detect any cells containing emojis, and replace those emojis with spaces. After running the script, you can safely convert your sheet to a PDF file without encountering the 500 error caused by emoji symbols.

The \p{Emoji_Presentation} pattern in the regular expression matches emoji characters. The g flag is for a global search (to replace all occurrences) and the u flag is for Unicode mode (to properly handle emoji characters).

Google Sheet Emojis

Using Spring Cloud Gateway and Discovery Service for Seamless Request Routing

Spring Cloud is a versatile framework for building Java applications in various cloud environments. Today, we'll explore how to use two components of the framework - Spring Cloud Gateway and Discovery Service (aka Spring Cloud Netflix) - for easy routing of user requests between your Java microservices.

We'll build two microservices, register them with a Discovery Service instance, and use the Cloud Gateway for routing requests to a specific microservice instance. The cool thing is that the Cloud Gateway will also be registered with the Discovery Service and will use the latter to resolve a microservice name into an actual connection endpoint.

How to Get Quick Feedback on Your Articles in WordPress

Are you looking for a way to get quick feedback on your articles in WordPress?

Feedback will help improve content quality by identifying any areas of improvement for your WordPress articles. It can also help build trust, leading to increased engagement on your website.

In this article, we will show you how to easily get quick feedback on your articles in WordPress.

Getting quick feedback on your articles in WordPress

Why Ask For Quick Feedback on Your WordPress Posts And Pages?

Most WordPress websites encourage users to leave comments and provide feedback on blog posts and articles.

However, fear of judgment, lack of time, or preferring to use social media prevents many users from doing this. Readers may also not know what exactly to say in their comments.

By using ‘was this helpful’ WordPress plugins, you can enable users to easily give their feedback on whether your content was useful to them.

These plugins usually display a thumbs-up/thumbs-down or a Yes/No button at the end of your WordPress post or pages that visitors can use to rate your content.

Was this article helpful? prompt

You can even use WordPress survey plugins to create a quick survey so that visitors can provide feedback.

This helps increase user engagement on your WordPress blog and shows the user that their feedback is valued. It also shows that you, as the website owner, are actively working to improve the user experience.

Using feedback plugins can also improve your website’s SEO rankings by reducing bounce rates, as users are more likely to stay on a website that has useful content.

Having said that, let’s see how you can easily get quick feedback for your WordPress articles. We will cover two methods in this post, and you can use the quick links below to jump to the method you want to use:

Method 1: Add Quick Feedback Using a Plugin (Easy)

If you are looking for an easy way to add quick feedback to your WordPress posts and pages, then this method is for you.

First, you need to install and activate the Was This Helpful? WordPress plugin. For detailed instructions, please see our tutorial on how to install a WordPress plugin.

Upon activation, the plugin will automatically start working out of the box and add a ‘Yes’ and ‘No’ option next to the ‘Was this article helpful?’ question at the end of your posts and pages.

Users visiting your site can now easily rate your content using these buttons.

Was this article helpful? prompt

To view the feedback on your WordPress posts, you will need to visit the Posts » All Posts page from the WordPress admin sidebar.

Once there, you will see the ‘Helpful’ column added to the right side of the screen.

From here, you will be able to monitor the ratings for your content and find out the number of people who have clicked the Yes and No buttons on your posts.

Check feedback from dashboard

Method 2: Add a Survey for Quick Feedback Using UserFeedback (Recommended)

If you want to add a quick survey to your pages and posts to collect feedback, then this method is for you.

First, you need to install and activate the UserFeedback plugin. For more instructions, you may want to see our guide on how to install a WordPress plugin.

Note: The UserFeedback plugin also has a free version that you can install. However, we will be using the Pro version for this tutorial to unlock more features.

Upon activation, you need to head over to the UserFeedback » Surveys page from the WordPress admin sidebar and click the ‘Create New’ button.

Click Create New button on Surveys page

This will take you to the ‘Select a Template’ page.

From here, you can use any of the pre-made templates as surveys on your website, including restaurant menu surveys, eCommerce store surveys, phone lead forms, post-purchase reviews, and more. You can also ‘Start From Scratch’ and design your own survey.

For this tutorial, we will be using the Content Engagement template because we want to add a quick feedback survey on our site asking people how we can improve our content.

Select the Content Engagement template

Just click on the survey template. This will direct you to a new page where you can start creating your survey.

By default, the Content Engagement template already comes with a question asking users if they found the content engaging.

However, you can easily change this by typing a question of your choice, like ‘Did you like this article?’ into the ‘Question Title’ field.

Type question in the Question Title field

Once you have done that, you need to choose an answer mode for users from the ‘Question Type’ dropdown menu.

For instance, if you want users to provide a star rating for your article, then you can choose the ‘Star-Rating’ option.

You can also display checkboxes, radio buttons, email capture options, single text fields, long answers, and Net Promoter Scores as modes of answers.

If you choose the radio button or checkbox, you will have to provide the different answers for the survey question under the ‘Possible Answers’ option.

Choose an answer mode

After that, scroll down to the next question that already comes in the Content Engagement template, asking users the kind of content they’d like to see created.

You can change that question to ask users to provide their feedback on the article they just read. This way, users will first get to rate your content, and then they can provide any feedback that they may have for the article.

For this question, make sure to choose the ‘Long Answer’ option from the ‘Question Type’ dropdown menu so that users can easily provide descriptive feedback on your website.

Create second question for the quick feedback

Once you have done that, just click the ‘Preview’ button at the top to customize the survey widget. From here, you can select the background color, text color, and button color.

Once you are done, you need to click the ‘Next Step: Settings’ button to move forward.

Customize the survey widget to your liking

This will take you to the ‘Settings’ page, where you can start by scrolling down to the ‘Targeting’ section. From here, you can select the device types where the survey will be displayed, including desktop, mobile, and tablet.

After that, you must choose the pages where the survey will be displayed. If you select the ‘All Pages’ option, then the quick feedback survey will be shown across all posts and pages on your website.

Choose the All Pages option to display the quick feedback survey everywhere

However, if you want to limit the survey to certain pages, then you can select the ‘Advanced’ option.

Once you do that, you must specify the conditions for the survey display from the dropdown menu.

For instance, if you want to only display the survey on posts, then you can select the ‘Post type is’ option from the dropdown menu on the left. After that, pick the ‘Post’ option from the dropdown menu on the right.

Now your quick feedback survey will only be displayed on WordPress posts.

Select conditions on choosing the Advanced option

Next, scroll down to the ‘Behavior’ section. Here, you can configure the display timing and length of your survey.

You can even schedule a run time for your survey by selecting the month and date you want the survey to end.

After you are done, simply click the ‘Next Step: Notifications’ button to move ahead.

Configure behavior section

On the next screen, you can start by toggling the ‘Send Email’ switch to active if you want to receive an email notification every time a user provides feedback on your website.

Next, you need to provide the email address where you want to receive the notifications and click the ‘Next Step: Publish’ button.

Toggle send email switch

On the new screen, simply scroll down to the ‘Publish’ section and switch the ‘Survey Status’ to ‘Publish’.

If you want, you can also schedule your survey for later by toggling the ‘Schedule for Later’ switch to active and providing a publication date and time.

Switch the Survey status to Publish

Finally, click the ‘Save and Schedule’ or ‘Save and Publish’ button to store your changes.

Now, you can visit your website to check out the quick feedback survey in action.

Quick feedback survey

Once your survey has been published, you can see its results by visiting the UserFeedback » Results page from the admin sidebar.

From here, you will be able to check out the number of responses, impressions, and all the answers provided by your visitors.

This can help you improve the overall content of your WordPress website.

Survey report

We hope this article helped you learn how to get quick feedback on your articles in WordPress. You may also want to see our tutorial on how to easily add a client feedback form in WordPress and our top picks for the must-have WordPress plugins to grow your site.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

The post How to Get Quick Feedback on Your Articles in WordPress first appeared on WPBeginner.

Simplify Log Aggregation in AWS: A Comprehensive Guide to the Log Aggregation Pattern

Logs are an integral part of any system as they provide valuable insight into its operations. However, with the increasing complexity and scale of modern cloud-based applications, managing logs can become quite challenging. This is where log aggregation in AWS comes into play, offering a simplified and centralized way to handle your application logs. This guide will provide a comprehensive understanding of the log aggregation pattern in AWS, including its benefits, how it works, and how to set it up.

What Is Log Aggregation?

Log aggregation is the process of collecting and centralizing log data from different sources into a single location. It is a crucial aspect of effective log management, enabling developers and system administrators to efficiently analyze and troubleshoot systems. Log aggregation simplifies the process of monitoring and analyzing logs, making it easier to identify and resolve issues.

What is an Email Footer? The Ultimate Guide with Examples

This post is originally published on Designmodo: What is an Email Footer? The Ultimate Guide with Examples

What is Email Footer? The Ultimate Guide with Examples

What makes an email design transparent and intuitive for subscribers? What reinforces an overall impression with strong messaging? What allows companies to send regulation-compliant emails? The answer to these questions is the email footer. An email footer is often overlooked. …

For more information please contact Designmodo

Karpenter  – Simplify Autoscaling for Amazon EKS

Kubernetes has become the de facto standard for container orchestration, enabling developers to manage and deploy containerized applications at scale. Amazon’s Elastic Kubernetes Service (EKS) is a fully managed Kubernetes service that makes it easy to run Kubernetes on AWS. However, as applications grow, managing resources effectively can become a challenge. That’s where Karpenter comes in.

What Is Karpenter?

Karpenter is a just-in-time capacity provisioner for any Kubernetes cluster, not just EKS. It is designed to simplify and optimize resource management in Kubernetes clusters by intelligently provisioning the right resources based on workload requirements. Although Karpenter can be used with any Kubernetes cluster, in this article, we will focus on its benefits and features within the context of AWS and EKS.

Battling Database Performance

Earlier this year, we experienced intermittent timeouts in our application while interacting with our database over a period of two weeks.

Despite our best efforts, we couldn't immediately identify a clear cause; there were no code changes that significantly altered our database usage, no sudden changes in traffic, and nothing alarming in our logs, traces, or dashboards.

How to Design an AI-Based Enterprise Search in AWS

Finding right information at the right moment is a key distinguisher in today's modern organization. This not only saves huge time and effort but boosts customer satisfaction as well as employee productivity. However, in most large organizations, all the contents and information are scattered and not indexed and organized properly. Often employees and customers browse through unrelated links for hours when they look for some urgent information (e.g., product information or process flows or policies, etc.) in the company's portal or intranet. Popular content management (CMS) software or wikis like Confluence or document management repositories like SharePoint lack the perfect intelligent search capabilities resulting in inefficiency as they only use the partial or full-text search based on keyword matching ignoring the semantic meaning of what the user is looking for.

Also, the traditional search doesn't understand if the question is being asked in natural language. It treats all words as search queries and tries to match all documents or contents based on that. For example, if I need to find which floor our IT helpdesk is located in my office building and simply search "Where is the IT Helpdesk located?" in general, CMS or Wiki software powering the company intranet it may bring up all links or texts matching every word of my question including "IT," "Helpdesk" as well as "located.” This would waste employee productivity, time, and morale as he or she would be spending a long time identifying correct info.

Cloud Security in Hybrid and Multi-Cloud

Increasing adoption of SaaS Applications and Web Based solutions created a demand for data and resource sharing. Cloud computing provides a combination of infrastructure, platforms, data storage, and software as services. It has replaced grid computing over the years and changed the way people share access, and store information. As more companies use cloud computing, maintaining cloud security in hybrid and multi-cloud systems becomes more challenging. These methods provide flexibility, scalability, and cost-effectiveness, but they also present special difficulties for data protection and upholding a strong security posture. The difficulties with cloud security that exist in hybrid and multi-cloud setups will be examined in this article, along with methods for successfully reducing the risks involved.

Understanding Hybrid and Multi-Cloud Environments

Organizations can take advantage of both private and public Clouds by combining them in hybrid cloud setups. To disperse workloads and prevent vendor lock-in, multi-cloud setups use several cloud service providers (CSPs). These configurations have advantages, including better geographic distribution, redundancy, and resource allocation. This setup allows a multi-server on-demand system. The combined bandwidth provides increased availability and fast access to resources. The resources are provided on demand allowing maximum utilization. The sharing of hardware resources provides great data storage, i.e., Amazon S3 provides affordable data storage to various platforms and applications. The combination of multiple clouds as one huge visualized system has brought a paradigm shift in the digital world. However, there are many difficulties in maintaining security across various platforms, networks, and providers.